From c9f505a1a6a89fc6dfb72c146b57532f44531fcc Mon Sep 17 00:00:00 2001 From: Evyatar Alush Date: Thu, 19 Dec 2024 00:16:40 +0200 Subject: [PATCH] fix(vest): clear cache before each test --- packages/vest/src/core/VestBus/VestBus.ts | 15 +++++++++++- .../testLevelFlowControl/verifyTestRun.ts | 1 + .../src/suite/__tests__/subscribe.test.ts | 23 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/vest/src/core/VestBus/VestBus.ts b/packages/vest/src/core/VestBus/VestBus.ts index 0714264b9..41be227bd 100644 --- a/packages/vest/src/core/VestBus/VestBus.ts +++ b/packages/vest/src/core/VestBus/VestBus.ts @@ -21,7 +21,20 @@ export function useInitVestBus() { const VestBus = Bus.useBus(); on('TEST_COMPLETED', () => {}); - // on("TEST_RUN_STARTED", () => {}); + + on('TEST_RUN_STARTED', () => { + // Bringin this back due to https://github.com/ealush/vest/issues/1157 + // This is a very pecluiar bug in which we're seeing vest behaving differently between + // runs when suite.get() is called. + // In the bug we experienced that failing tests were skipped in the second run. + // The reason: suite.get() built the failures cache. Calling suite.get() before the test run + // made Vest think that the field already had failing tests (even though it was the same test!) + // and it skipped the test. + // A better solution is to be able to identify each failure to its actual position in the suite + // but this requires some rearchitecting within Vest. + // This is an easy enough solution - we just reset the cache before the test run, let's hope we don't see + // any performance issues. + }); VestBus.on(RuntimeEvents.ISOLATE_PENDING, (isolate: TIsolate) => { if (VestTest.is(isolate)) { diff --git a/packages/vest/src/core/test/testLevelFlowControl/verifyTestRun.ts b/packages/vest/src/core/test/testLevelFlowControl/verifyTestRun.ts index 585ee6615..b591c6eb4 100644 --- a/packages/vest/src/core/test/testLevelFlowControl/verifyTestRun.ts +++ b/packages/vest/src/core/test/testLevelFlowControl/verifyTestRun.ts @@ -8,6 +8,7 @@ import { useWithinActiveOmitWhen } from 'omitWhen'; import { useIsExcludedIndividually } from 'skipWhen'; import { useIsExcluded } from 'useIsExcluded'; +// eslint-disable-next-line complexity export function useVerifyTestRun( testObject: TIsolateTest, collisionResult: TIsolateTest = testObject, diff --git a/packages/vest/src/suite/__tests__/subscribe.test.ts b/packages/vest/src/suite/__tests__/subscribe.test.ts index 78a707ce4..1ac458eab 100644 --- a/packages/vest/src/suite/__tests__/subscribe.test.ts +++ b/packages/vest/src/suite/__tests__/subscribe.test.ts @@ -98,3 +98,26 @@ describe('suite.subscribe', () => { }); }); }); + +describe('#1157 (@codrin-iftimie) suite.get() in subscribe() skips the first validation of the field', () => { + it('Should fail for the first field in both runs', () => { + const suite = vest.create(data => { + vest.test('a', 'Enter a value', () => { + enforce(data.a).isNotEmpty(); + }); + + vest.test('a', 'Enter a value 2', () => { + enforce(data.a).isNotEmpty(); + }); + }); + + suite.subscribe(() => { + suite.get(); + }); + + suite({ a: '' }); + expect(suite.getErrors('a')).toEqual(['Enter a value']); + suite({ a: '' }); + expect(suite.getErrors('a')).toEqual(['Enter a value']); + }); +});