Skip to content

Commit

Permalink
patch(vest): general cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Feb 17, 2022
1 parent f8a934c commit 4745f94
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 85 deletions.
22 changes: 10 additions & 12 deletions packages/shared/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,21 @@ export default function createCache(maxSize = 1): {

// invalidate an item in the cache by its dependencies
cache.invalidate = (deps: any[]): void => {
const index = cacheStorage.findIndex(
([cachedDeps]) =>
lengthEquals(deps, cachedDeps.length) &&
deps.every((dep, i) => dep === cachedDeps[i])
);
const index = findIndex(deps);
if (index > -1) cacheStorage.splice(index, 1);
};

// Retrieves an item from the cache.
cache.get = (deps: unknown[]): [unknown[], any] | null =>
cacheStorage[
cacheStorage.findIndex(
([cachedDeps]) =>
lengthEquals(deps, cachedDeps.length) &&
deps.every((dep, i) => dep === cachedDeps[i])
)
] || null;
cacheStorage[findIndex(deps)] || null;

return cache;

function findIndex(deps: unknown[]): number {
return cacheStorage.findIndex(
([cachedDeps]) =>
lengthEquals(deps, cachedDeps.length) &&
deps.every((dep, i) => dep === cachedDeps[i])
);
}
}
6 changes: 3 additions & 3 deletions packages/shared/src/nestedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import last from 'last';

export type NestedArray<T> = Array<NestedArray<T> | T>;

// This is sort of a map/filter in one function.
// Normally, behaves like a nested-array map
// Returning `null` will drop the element from the array
// This is kind of a map/filter in one function.
// Normally, behaves like a nested-array map,
// but returning `null` will drop the element from the array
export function transform<T>(
array: NestedArray<T>,
cb: (value: T) => NestedArray<T> | T | null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`each When callback is not a function should throw 1`] = `"callback must be a function"`;
exports[`each When callback is not a function should throw 1`] = `"each callback must be a function"`;
2 changes: 1 addition & 1 deletion packages/vest/src/core/isolate/isolates/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function each<T>(
callback: (arg: T, index: number) => void
): void {
if (!isFunction(callback)) {
throwError('callback must be a function');
throwError('each callback must be a function');
}

isolate({ type: IsolateTypes.EACH }, () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/vest/src/core/suite/produce/Severity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Severity {
WARNINGS = 'warnings',
ERRORS = 'errors',
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nonMatchingSeverityProfile from 'nonMatchingSeverityProfile';

import { Severity } from 'Severity';
import VestTest from 'VestTest';
import nonMatchingSeverityProfile from 'nonMatchingSeverityProfile';

describe('nonMatchingSeverityProfile', () => {
let testObject: VestTest;
Expand All @@ -12,13 +12,17 @@ describe('nonMatchingSeverityProfile', () => {
describe('When both are warning', () => {
it('should return false', () => {
testObject.warn();
expect(nonMatchingSeverityProfile('warnings', testObject)).toBe(false);
expect(nonMatchingSeverityProfile(Severity.WARNINGS, testObject)).toBe(
false
);
});
});

describe('When both are not warning', () => {
it('should return false', () => {
expect(nonMatchingSeverityProfile('errors', testObject)).toBe(false);
expect(nonMatchingSeverityProfile(Severity.ERRORS, testObject)).toBe(
false
);
});
});
});
Expand All @@ -27,13 +31,17 @@ describe('nonMatchingSeverityProfile', () => {
describe('When test is warning', () => {
it('should return true', () => {
testObject.warn();
expect(nonMatchingSeverityProfile('errors', testObject)).toBe(true);
expect(nonMatchingSeverityProfile(Severity.ERRORS, testObject)).toBe(
true
);
});
});

describe('When severity is warning', () => {
it('should return true', () => {
expect(nonMatchingSeverityProfile('warnings', testObject)).toBe(true);
expect(nonMatchingSeverityProfile(Severity.WARNINGS, testObject)).toBe(
true
);
});
});
});
Expand Down
34 changes: 19 additions & 15 deletions packages/vest/src/core/suite/produce/genTestsSummary.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import assign from 'assign';

import { Severity } from 'Severity';
import VestTest from 'VestTest';
import { useTestsFlat } from 'stateHooks';
import type { TSeverity } from 'vestTypes';

/**
* Reads the testObjects list and gets full validation result from it.
*/
export default function genTestsSummary(): TTestSummary {
const testObjects = useTestsFlat();

const summary: TTestSummary = {
errorCount: 0,
const summary: TTestSummary = assign(baseStats(), {
groups: {},
testCount: 0,
tests: {},
warnCount: 0,
};
});

appendSummary(testObjects);

Expand Down Expand Up @@ -56,11 +55,7 @@ function genTestObject(
): TSingleTestSummary {
const { fieldName, message } = testObject;

summaryKey[fieldName] = summaryKey[fieldName] || {
errorCount: 0,
warnCount: 0,
testCount: 0,
};
summaryKey[fieldName] = summaryKey[fieldName] || baseStats();

const testKey = summaryKey[fieldName];

Expand All @@ -69,22 +64,31 @@ function genTestObject(
summaryKey[fieldName].testCount++;

// Adds to severity group
function addTo(countKey: 'warnCount' | 'errorCount', group: TSeverity) {
function addTo(severity: Severity) {
const countKey = severity === Severity.ERRORS ? 'errorCount' : 'warnCount';
testKey[countKey]++;
if (message) {
testKey[group] = (testKey[group] || []).concat(message);
testKey[severity] = (testKey[severity] || []).concat(message);
}
}

if (testObject.isFailing()) {
addTo('errorCount', 'errors');
addTo(Severity.ERRORS);
} else if (testObject.isWarning()) {
addTo('warnCount', 'warnings');
addTo(Severity.WARNINGS);
}

return testKey;
}

function baseStats() {
return {
errorCount: 0,
warnCount: 0,
testCount: 0,
};
}

type TTestSummary = {
groups: Record<string, TTestGroup>;
tests: TTestGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Severity } from 'Severity';
import VestTest from 'VestTest';
import collectFailureMessages from 'collectFailureMessages';

describe('collectFailureMessages', () => {
let testObjects: VestTest[] = [];

it('Should return an object containing just the requested field', () => {
const result = collectFailureMessages('errors', testObjects, {
const result = collectFailureMessages(Severity.ERRORS, testObjects, {
fieldName: 'field_1',
});
expect(Object.keys(result)).toEqual(['field_1']);
});

test('Result has an array of matching error messages', () => {
const result = collectFailureMessages('errors', testObjects, {
const result = collectFailureMessages(Severity.ERRORS, testObjects, {
fieldName: 'field_1',
});
expect(result.field_1).toEqual([
Expand All @@ -22,7 +23,7 @@ describe('collectFailureMessages', () => {
});

it('Should return filtered messages by the selected group', () => {
const result = collectFailureMessages('errors', testObjects, {
const result = collectFailureMessages(Severity.ERRORS, testObjects, {
group: 'group1',
});

Expand All @@ -31,7 +32,7 @@ describe('collectFailureMessages', () => {

it('Should return an empty object when no options and no failures', () => {
expect(
collectFailureMessages('errors', [
collectFailureMessages(Severity.ERRORS, [
new VestTest('field_1', jest.fn(), { message: 'error_message' }),
])
).toEqual({});
Expand All @@ -40,15 +41,14 @@ describe('collectFailureMessages', () => {
it('Should return an object with an empty array when selected field has no errors', () => {
expect(
collectFailureMessages(
'errors',
Severity.ERRORS,
[new VestTest('field_1', jest.fn(), { message: 'error_message' })],
{ fieldName: 'field_1' }
)
).toEqual({ field_1: [] });
});

// @ts-expect-error - it can't properly infer the severity
['errors', 'warnings'].forEach((severity: 'errors' | 'warnings') => {
[Severity.ERRORS, Severity.WARNINGS].forEach((severity: Severity) => {
describe('Snapshot tests. severity: ' + severity, () => {
describe('When no options passed', () => {
it('should match snapshot', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Severity } from 'Severity';
import VestTest from 'VestTest';
import { nonMatchingFieldName } from 'matchingFieldName';
import nonMatchingSeverityProfile from 'nonMatchingSeverityProfile';
import type { TSeverity } from 'vestTypes';

export default function collectFailureMessages(
severity: TSeverity,
severity: Severity,
testObjects: VestTest[],
options: { group?: string; fieldName?: string } = {}
): Record<string, string[]> {
Expand Down Expand Up @@ -38,7 +38,7 @@ function noGroupMatch(

function noMatch(
testObject: VestTest,
severity: TSeverity,
severity: Severity,
group: void | string,
fieldName: void | string
): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Severity } from 'Severity';
import collectFailureMessages from 'collectFailureMessages';
import getFailuresArrayOrObject from 'getFailuresArrayOrObject';
import { useTestsFlat } from 'stateHooks';
import type { TSeverity } from 'vestTypes';

export function getErrors(): Record<string, string[]>;
export function getErrors(fieldName?: string): string[];
export function getErrors(
fieldName?: string
): string[] | Record<string, string[]> {
return getFailures('errors', fieldName);
return getFailures(Severity.ERRORS, fieldName);
}

export function getWarnings(): Record<string, string[]>;
export function getWarnings(fieldName?: string): string[];
export function getWarnings(
fieldName?: string
): string[] | Record<string, string[]> {
return getFailures('warnings', fieldName);
return getFailures(Severity.WARNINGS, fieldName);
}

/**
* @returns suite or field's errors or warnings.
*/
function getFailures(severityKey: TSeverity, fieldName?: string) {
function getFailures(severityKey: Severity, fieldName?: string) {
const testObjects = useTestsFlat();
const failureMessages = collectFailureMessages(severityKey, testObjects, {
fieldName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import throwError from 'throwError';

import { Severity } from 'Severity';
import collectFailureMessages from 'collectFailureMessages';
import getFailuresArrayOrObject from 'getFailuresArrayOrObject';
import { useTestsFlat } from 'stateHooks';
import type { TSeverity } from 'vestTypes';

export function getErrorsByGroup(groupName: string): Record<string, string[]>;
export function getErrorsByGroup(
Expand All @@ -14,7 +14,7 @@ export function getErrorsByGroup(
groupName: string,
fieldName?: string
): string[] | Record<string, string[]> {
const errors = getByGroup('errors', groupName, fieldName);
const errors = getByGroup(Severity.ERRORS, groupName, fieldName);

return getFailuresArrayOrObject(errors, fieldName);
}
Expand All @@ -28,7 +28,7 @@ export function getWarningsByGroup(
groupName: string,
fieldName?: string
): string[] | Record<string, string[]> {
const warnings = getByGroup('warnings', groupName, fieldName);
const warnings = getByGroup(Severity.WARNINGS, groupName, fieldName);

return getFailuresArrayOrObject(warnings, fieldName);
}
Expand All @@ -37,7 +37,7 @@ export function getWarningsByGroup(
* Gets failure messages by group.
*/
function getByGroup(
severityKey: TSeverity,
severityKey: Severity,
group: string,
fieldName?: string
): Record<string, string[]> {
Expand Down
Loading

1 comment on commit 4745f94

@vercel
Copy link

@vercel vercel bot commented on 4745f94 Feb 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-git-latest-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app

Please sign in to comment.