Skip to content

Commit

Permalink
fix(vest): eager mode works inside of sub-isolates (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush authored Jan 16, 2024
1 parent 97662a7 commit b6400af
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 58 deletions.
8 changes: 4 additions & 4 deletions packages/n4s/src/plugins/schema/__tests__/shape&loose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
}).run({ username: 'ealush', age: 31 })
}).run({ username: 'ealush', age: 31 }),
).toEqual(ruleReturn.passing());
});

Expand All @@ -23,7 +23,7 @@ describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
enforce[methodName]({
username: enforce.isString(),
age: enforce.isNumber().gt(18),
}).run({ username: null, age: 0 })
}).run({ username: null, age: 0 }),
).toEqual(ruleReturn.failing());
});

Expand All @@ -48,7 +48,7 @@ describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
state: 'NY',
zip: 12345,
},
})
}),
).toEqual(ruleReturn.passing());
});
it('Should return a failing return when tests are invalid', () => {
Expand All @@ -69,7 +69,7 @@ describe.each(['loose', 'shape'])('enforce.%s', (methodName: string) => {
street: '123 Main St',
city: null,
},
})
}),
).toEqual(ruleReturn.failing());
});
});
Expand Down
5 changes: 3 additions & 2 deletions packages/vest/src/core/StateMachines/CommonStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { TIsolate } from 'vestjs-runtime';

export const CommonStates = {
PENDING: 'PENDING',
INITIAL: 'INITIAL',
};

export const State = {
const State = {
[CommonStates.PENDING]: CommonStates.PENDING,
INITIAL: 'INITIAL',
[CommonStates.INITIAL]: CommonStates.INITIAL,
DONE: 'DONE',
};

Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/VestBus/VestBus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SuiteWalker } from 'SuiteWalker';
import { CB, ValueOf } from 'vest-utils';
import { Bus, RuntimeEvents, TIsolate } from 'vestjs-runtime';

Expand All @@ -11,6 +10,7 @@ import {
useResetSuite,
} from 'Runtime';
import { TFieldName } from 'SuiteResultTypes';
import { SuiteWalker } from 'SuiteWalker';
import { TestWalker } from 'TestWalker';
import { VestTest } from 'VestTest';
import { useOmitOptionalFields } from 'omitOptionalFields';
Expand Down
1 change: 1 addition & 0 deletions packages/vest/src/core/__tests__/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('useLoadSuite', () => {

function genDump() {
const suite = vest.create(() => {
vest.mode(vest.Modes.ALL);
vest.skip('t5');

vest.test('t1', () => false);
Expand Down
6 changes: 3 additions & 3 deletions packages/vest/src/core/isolate/VestIsolate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nonnullish, TStateMachineApi } from 'vest-utils';
import { TStateMachineApi } from 'vest-utils';
import { TIsolate } from 'vestjs-runtime';

import { CommonStateMachine, CommonStates } from 'CommonStateMachine';
Expand All @@ -7,14 +7,14 @@ export class VestIsolate {
static stateMachine: TStateMachineApi = CommonStateMachine;

static getStatus(isolate: TIsolate): string {
return nonnullish(isolate.status);
return isolate.status ?? CommonStates.INITIAL;
}

static setStatus(isolate: TIsolate, status: string, payload?: any): void {
isolate.status = this.stateMachine.staticTransition(
VestIsolate.getStatus(isolate),
status,
payload
payload,
);
}

Expand Down
23 changes: 23 additions & 0 deletions packages/vest/src/hooks/__tests__/mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ describe('mode', () => {
expect(suite.get().getErrors('field_1')).toEqual([]);
});
});

describe('When in a nested block', () => {
it('Should follow the same behavior as if it was not nested', () => {
const suite = create(() => {
group('group_1', () => {
dummyTest.failing('field_1', 'first-of-field_1');
dummyTest.failing('field_1', 'second-of-field_1');
dummyTest.failing('field_2', 'first-of-field_2');
dummyTest.failing('field_2', 'second-of-field_2');
dummyTest.failing('field_3', 'first-of-field_3');
dummyTest.failing('field_3', 'second-of-field_3');
});
});
expect(suite.get().testCount).toBe(0); // sanity
suite();

expect(suite.get().testCount).toBe(3);
expect(suite.get().errorCount).toBe(3);
expect(suite.get().getErrors('field_1')).toEqual(['first-of-field_1']);
expect(suite.get().getErrors('field_2')).toEqual(['first-of-field_2']);
expect(suite.get().getErrors('field_3')).toEqual(['first-of-field_3']);
});
});
});

describe('All', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/vest/src/hooks/focused/useIsExcluded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useHasOnliedTests } from 'useHasOnliedTests';
//Checks whether a certain test profile excluded by any of the exclusion groups.

function useClosestMatchingFocus(
testObject: TIsolateTest
testObject: TIsolateTest,
): Nullable<TIsolateFocused> {
return Walker.findClosest(testObject, (child: TIsolate) => {
if (!FocusSelectors.isIsolateFocused(child)) return false;
Expand All @@ -21,7 +21,6 @@ function useClosestMatchingFocus(
});
}

// eslint-disable-next-line complexity, max-statements
export function useIsExcluded(testObject: TIsolateTest): boolean {
const { fieldName } = VestTest.getData(testObject);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { faker } from '@faker-js/faker';
import { VestTest } from 'VestTest';

import { TIsolateTest } from 'IsolateTest';
import { Severity } from 'Severity';
import { VestTest } from 'VestTest';
import { hasFailuresByTestObject } from 'hasFailuresByTestObjects';
import { mockIsolateTest } from 'vestMocks';

Expand All @@ -22,10 +22,10 @@ describe('hasFailuresByTestObject', () => {
it('Should return false', () => {
expect(hasFailuresByTestObject(testObject, Severity.ERRORS)).toBe(false);
expect(hasFailuresByTestObject(testObject, Severity.WARNINGS)).toBe(
false
false,
);
expect(
hasFailuresByTestObject(testObject, Severity.ERRORS, fieldName)
hasFailuresByTestObject(testObject, Severity.ERRORS, fieldName),
).toBe(false);
});
});
Expand All @@ -38,23 +38,23 @@ describe('hasFailuresByTestObject', () => {
describe('When non matching severity profile', () => {
it('should return false', () => {
expect(hasFailuresByTestObject(testObject, Severity.WARNINGS)).toBe(
false
false,
);
VestTest.warn(testObject);
expect(hasFailuresByTestObject(testObject, Severity.ERRORS)).toBe(
false
false,
);
});
});

describe('When matching severity profile', () => {
it('Should return true', () => {
expect(hasFailuresByTestObject(testObject, Severity.ERRORS)).toBe(
true
true,
);
VestTest.warn(testObject);
expect(hasFailuresByTestObject(testObject, Severity.WARNINGS)).toBe(
true
true,
);
});
});
Expand All @@ -63,23 +63,27 @@ describe('hasFailuresByTestObject', () => {
describe('When field name matches', () => {
it('should return false', () => {
expect(
hasFailuresByTestObject(testObject, Severity.ERRORS, 'non_matching')
hasFailuresByTestObject(
testObject,
Severity.ERRORS,
'non_matching',
),
).toBe(false);
});
});

describe('When field name matches', () => {
it('Should continue with normal flow', () => {
expect(hasFailuresByTestObject(testObject, Severity.WARNINGS)).toBe(
false
false,
);
VestTest.warn(testObject);
expect(hasFailuresByTestObject(testObject, Severity.ERRORS)).toBe(
false
false,
);
VestTest.fail(testObject);
expect(hasFailuresByTestObject(testObject, Severity.WARNINGS)).toBe(
true
true,
);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TTestSuite } from 'testUtils/TVestMock';

import * as vest from 'vest';

describe('summaryFailures', () => {
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('summaryFailures', () => {

test('Should add the test group into the error object', () => {
suite = vest.create(() => {
vest.mode(vest.Modes.ALL);
vest.group('user', () => {
vest.test('username', 'uxsername is required', () => false);
vest.test('username', 'username is too short', () => false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SuiteWalker } from 'SuiteWalker';
import { useIsOptionalFieldApplied } from 'optional';
import { Predicates } from 'vest-utils';
import { VestRuntime } from 'vestjs-runtime';
Expand All @@ -8,6 +7,7 @@ import { TIsolateTest } from 'IsolateTest';
import { OptionalFieldTypes } from 'OptionalTypes';
import { Severity } from 'Severity';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { SuiteWalker } from 'SuiteWalker';
import { TestWalker } from 'TestWalker';
import { VestTest } from 'VestTest';
import {
Expand Down
22 changes: 11 additions & 11 deletions packages/vest/src/suiteResult/selectors/useProduceSuiteSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

export function useProduceSuiteSummary<
F extends TFieldName,
G extends TGroupName
G extends TGroupName,
>(): SuiteSummary<F, G> {
const summary: SuiteSummary<F, G> = new SuiteSummary();

Expand All @@ -31,12 +31,12 @@ export function useProduceSuiteSummary<
summary.errors = appendFailures(
Severity.ERRORS,
summary.errors,
testObject
testObject,
);
summary.warnings = appendFailures(
Severity.WARNINGS,
summary.warnings,
testObject
testObject,
);
});

Expand All @@ -48,7 +48,7 @@ export function useProduceSuiteSummary<
function appendFailures<F extends TFieldName, G extends TGroupName>(
key: Severity,
failures: SummaryFailure<F, G>[],
testObject: TIsolateTest<F, G>
testObject: TIsolateTest<F, G>,
): SummaryFailure<F, G>[] {
if (VestTest.isOmitted(testObject)) {
return failures;
Expand All @@ -67,7 +67,7 @@ function appendFailures<F extends TFieldName, G extends TGroupName>(

function useAppendToTest<F extends TFieldName>(
tests: Tests<F>,
testObject: TIsolateTest<F>
testObject: TIsolateTest<F>,
): Tests<F> {
const fieldName = VestTest.getData<F>(testObject).fieldName;

Expand All @@ -90,7 +90,7 @@ function useAppendToTest<F extends TFieldName>(
*/
function useAppendToGroup(
groups: Groups<TGroupName, TFieldName>,
testObject: TIsolateTest
testObject: TIsolateTest,
): Groups<TGroupName, TFieldName> {
const { groupName, fieldName } = VestTest.getData(testObject);

Expand All @@ -105,7 +105,7 @@ function useAppendToGroup(
newGroups[groupName] = newGroups[groupName] || {};
newGroups[groupName][fieldName] = appendTestObject(
newGroups[groupName][fieldName],
testObject
testObject,
);

newGroups[groupName][fieldName].valid =
Expand All @@ -120,7 +120,7 @@ function useAppendToGroup(
* Counts the failed tests and adds global counters
*/
function countOverallStates<F extends TFieldName, G extends TGroupName>(
summary: SuiteSummary<F, G>
summary: SuiteSummary<F, G>,
): SuiteSummary<F, G> {
for (const test in summary.tests) {
summary.errorCount += summary.tests[test].errorCount;
Expand All @@ -137,14 +137,14 @@ function countOverallStates<F extends TFieldName, G extends TGroupName>(
// eslint-disable-next-line max-statements, complexity
function appendTestObject(
summaryKey: Maybe<SingleTestSummary>,
testObject: TIsolateTest
testObject: TIsolateTest,
): SingleTestSummary {
const { message } = VestTest.getData(testObject);

// Let's first create a new object, so we don't mutate the original.
const nextSummaryKey = defaultTo<SingleTestSummary>(
summaryKey ? { ...summaryKey } : null,
baseTestStats
baseTestStats,
);

// If the test is not actionable, we don't need to append it to the summary.
Expand Down Expand Up @@ -174,7 +174,7 @@ function appendTestObject(
nextSummaryKey[countKey]++;
if (message) {
nextSummaryKey[severity] = (nextSummaryKey[severity] || []).concat(
message
message,
);
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/vest/src/vest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { enforce } from 'n4s';

// eslint-disable-next-line import/order -- will handle this circular dep issue later.
import { optional } from 'optional';

import { Modes } from 'Modes';
import type {
SuiteResult,
Expand Down
Loading

2 comments on commit b6400af

@vercel
Copy link

@vercel vercel bot commented on b6400af Jan 16, 2024

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-website.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-next.vercel.app
vest-next-ealush.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b6400af Jan 16, 2024

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 – ./website

vest-ealush.vercel.app
vest.vercel.app
vestjs.dev
www.vestjs.dev
vest-git-latest-ealush.vercel.app

Please sign in to comment.