From 31cd18e5e78b814ea204758ee7ccb6216158824e Mon Sep 17 00:00:00 2001 From: Evyatar Date: Sun, 12 Nov 2023 20:37:23 +0200 Subject: [PATCH] patch(vest): Unify VestTest Identity functions --- packages/vest/src/core/VestBus/VestBus.ts | 5 ++-- .../IsolateTest/IsolateTestReconciler.ts | 13 ++++---- .../core/isolate/IsolateTest/TestWalker.ts | 23 +++++++------- .../src/core/isolate/IsolateTest/VestTest.ts | 23 +++++++++++++- .../core/isolate/IsolateTest/isIsolateTest.ts | 30 ------------------- .../core/test/__tests__/runAsyncTest.test.ts | 10 +++---- .../vest/src/hooks/__tests__/warn.test.ts | 6 ++-- packages/vest/src/testUtils/asVestTest.ts | 10 ------- packages/vest/tsconfig.json | 2 -- 9 files changed, 47 insertions(+), 75 deletions(-) delete mode 100644 packages/vest/src/core/isolate/IsolateTest/isIsolateTest.ts delete mode 100644 packages/vest/src/testUtils/asVestTest.ts diff --git a/packages/vest/src/core/VestBus/VestBus.ts b/packages/vest/src/core/VestBus/VestBus.ts index 62ac0bfb0..03b7e7aac 100644 --- a/packages/vest/src/core/VestBus/VestBus.ts +++ b/packages/vest/src/core/VestBus/VestBus.ts @@ -12,7 +12,6 @@ import { import { TFieldName } from 'SuiteResultTypes'; import { TestWalker } from 'TestWalker'; import { VestTest } from 'VestTest'; -import { isIsolateTest } from 'isIsolateTest'; import { useOmitOptionalFields } from 'omitOptionalFields'; import { useRunDoneCallbacks, useRunFieldCallbacks } from 'runCallbacks'; @@ -42,7 +41,7 @@ export function useInitVestBus() { }); on(RuntimeEvents.ISOLATE_PENDING, (isolate: TIsolate) => { - if (isIsolateTest(isolate)) { + if (VestTest.is(isolate)) { VestTest.setPending(isolate); } @@ -50,7 +49,7 @@ export function useInitVestBus() { }); on(RuntimeEvents.ISOLATE_DONE, (isolate: TIsolate) => { - if (isIsolateTest(isolate)) { + if (VestTest.is(isolate)) { VestBus.emit(Events.TEST_COMPLETED, isolate); } diff --git a/packages/vest/src/core/isolate/IsolateTest/IsolateTestReconciler.ts b/packages/vest/src/core/isolate/IsolateTest/IsolateTestReconciler.ts index d0c510632..355a03639 100644 --- a/packages/vest/src/core/isolate/IsolateTest/IsolateTestReconciler.ts +++ b/packages/vest/src/core/isolate/IsolateTest/IsolateTestReconciler.ts @@ -7,13 +7,12 @@ import { IsolateReconciler } from 'IsolateReconciler'; import type { TIsolateTest } from 'IsolateTest'; import { VestTest } from 'VestTest'; import cancelOverriddenPendingTest from 'cancelOverriddenPendingTest'; -import { castIsolateTest, isIsolateTest } from 'isIsolateTest'; import { isSameProfileTest } from 'isSameProfileTest'; import { useVerifyTestRun } from 'verifyTestRun'; export class IsolateTestReconciler extends IsolateReconciler { static match(currentNode: TIsolate, historyNode: TIsolate): boolean { - return isIsolateTest(currentNode) && isIsolateTest(historyNode); + return VestTest.is(currentNode) && VestTest.is(historyNode); } static reconcile( @@ -46,7 +45,7 @@ function handleCollision( prevNode?: TIsolate ): TIsolateTest { if (IsolateInspector.usesKey(newNode)) { - return castIsolateTest(Reconciler.handleIsolateNodeWithKey(newNode)); + return VestTest.cast(Reconciler.handleIsolateNodeWithKey(newNode)); } if ( @@ -56,7 +55,7 @@ function handleCollision( return newNode; } - if (!isIsolateTest(prevNode)) { + if (!VestTest.is(prevNode)) { // I believe we cannot actually reach this point. // Because it should already be handled by nodeReorderDetected. /* istanbul ignore next */ @@ -81,7 +80,7 @@ function cancelOverriddenPendingTestOnTestReRun( currentNode: TIsolate, prevTestObject: TIsolateTest ) { - if (nextNode === currentNode && isIsolateTest(currentNode)) { + if (nextNode === currentNode && VestTest.is(currentNode)) { cancelOverriddenPendingTest(prevTestObject, currentNode); } } @@ -90,7 +89,7 @@ function nodeReorderDetected( newNode: TIsolateTest, prevNode: Maybe ): boolean { - return isIsolateTest(prevNode) && !isSameProfileTest(prevNode, newNode); + return VestTest.is(prevNode) && !isSameProfileTest(prevNode, newNode); } function throwTestOrderError( @@ -104,7 +103,7 @@ function throwTestOrderError( deferThrow( text(ErrorStrings.TESTS_CALLED_IN_DIFFERENT_ORDER, { fieldName: VestTest.getData(newNode).fieldName, - prevName: isIsolateTest(prevNode) + prevName: VestTest.is(prevNode) ? VestTest.getData(prevNode).fieldName : undefined, }) diff --git a/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts b/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts index e9bad9284..e410d95cb 100644 --- a/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts +++ b/packages/vest/src/core/isolate/IsolateTest/TestWalker.ts @@ -4,7 +4,6 @@ import { Walker, VestRuntime, TIsolate } from 'vestjs-runtime'; import { TIsolateTest } from 'IsolateTest'; import { TFieldName, TGroupName } from 'SuiteResultTypes'; import { VestTest } from 'VestTest'; -import { castIsolateTest, isIsolateTest, isIsolateTestX } from 'isIsolateTest'; import matchingFieldName from 'matchingFieldName'; type MaybeRoot = Nullable; @@ -14,7 +13,7 @@ export class TestWalker { static hasNoTests(root: MaybeRoot = TestWalker.defaultRoot()): boolean { if (!root) return true; - return !Walker.has(root, isIsolateTest); + return !Walker.has(root, VestTest.is); } static someIncompleteTests( @@ -25,11 +24,11 @@ export class TestWalker { return Walker.some( root, isolate => { - isIsolateTestX(isolate); + VestTest.isX(isolate); return VestTest.isPending(isolate) && predicate(isolate); }, - isIsolateTest + VestTest.is ); } @@ -41,11 +40,11 @@ export class TestWalker { return Walker.some( root, isolate => { - isIsolateTestX(isolate); + VestTest.isX(isolate); return predicate(isolate); }, - isIsolateTest + VestTest.is ); } @@ -57,11 +56,11 @@ export class TestWalker { return Walker.every( root, isolate => { - isIsolateTestX(isolate); + VestTest.isX(isolate); return predicate(isolate); }, - isIsolateTest + VestTest.is ); } @@ -73,9 +72,9 @@ export class TestWalker { Walker.walk( root, (isolate, breakout) => { - callback(castIsolateTest(isolate), breakout); + callback(VestTest.cast(isolate), breakout); }, - isIsolateTest + VestTest.is ); } @@ -96,11 +95,11 @@ export class TestWalker { Walker.pluck( root, isolate => { - isIsolateTestX(isolate); + VestTest.isX(isolate); return predicate(isolate); }, - isIsolateTest + VestTest.is ); } diff --git a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts index 61f5f61a1..d3057b928 100644 --- a/packages/vest/src/core/isolate/IsolateTest/VestTest.ts +++ b/packages/vest/src/core/isolate/IsolateTest/VestTest.ts @@ -1,5 +1,7 @@ -import { invariant, isPromise, optionalFunctionValue } from 'vest-utils'; +import { Maybe, invariant, isPromise, optionalFunctionValue } from 'vest-utils'; +import { IsolateSelectors, TIsolate } from 'vestjs-runtime'; +import { ErrorStrings } from 'ErrorStrings'; import type { TIsolateTest } from 'IsolateTest'; import { IsolateTestStateMachine, @@ -9,6 +11,7 @@ import { } from 'IsolateTestStateMachine'; import { TestSeverity } from 'Severity'; import { TFieldName, TGroupName } from 'SuiteResultTypes'; +import { VestIsolateType } from 'VestIsolateType'; export class VestTest { // Read @@ -21,6 +24,24 @@ export class VestTest { return test.data; } + static is(isolate?: Maybe): isolate is TIsolateTest { + return IsolateSelectors.isIsolateType( + isolate, + VestIsolateType.Test + ); + } + + static isX(isolate?: Maybe): asserts isolate is TIsolateTest { + invariant(VestTest.is(isolate), ErrorStrings.EXPECTED_VEST_TEST); + } + + static cast( + isolate?: Maybe + ): TIsolateTest { + VestTest.isX(isolate); + return isolate as TIsolateTest; + } + static warns(test: TIsolateTest): boolean { return VestTest.getData(test).severity === TestSeverity.Warning; } diff --git a/packages/vest/src/core/isolate/IsolateTest/isIsolateTest.ts b/packages/vest/src/core/isolate/IsolateTest/isIsolateTest.ts deleted file mode 100644 index 33aae087b..000000000 --- a/packages/vest/src/core/isolate/IsolateTest/isIsolateTest.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Maybe, invariant } from 'vest-utils'; -import { TIsolate, IsolateSelectors } from 'vestjs-runtime'; - -import type { TIsolateTest } from 'IsolateTest'; -import { TFieldName, TGroupName } from 'SuiteResultTypes'; -import { VestIsolateType } from 'VestIsolateType'; - -export function isIsolateTest( - isolate?: Maybe -): isolate is TIsolateTest { - return IsolateSelectors.isIsolateType( - isolate, - VestIsolateType.Test - ); -} - -export function isIsolateTestX( - isolate: TIsolate -): asserts isolate is TIsolateTest { - invariant(isIsolateTest(isolate)); -} - -export function castIsolateTest< - F extends TFieldName = TFieldName, - G extends TGroupName = TGroupName ->(isolate: TIsolate): TIsolateTest { - isIsolateTestX(isolate); - - return isolate as TIsolateTest; -} diff --git a/packages/vest/src/core/test/__tests__/runAsyncTest.test.ts b/packages/vest/src/core/test/__tests__/runAsyncTest.test.ts index 59705a8c1..575ee890e 100644 --- a/packages/vest/src/core/test/__tests__/runAsyncTest.test.ts +++ b/packages/vest/src/core/test/__tests__/runAsyncTest.test.ts @@ -1,7 +1,5 @@ import wait from 'wait'; -import { asVestTest } from '../../../testUtils/asVestTest'; - import { TIsolateTest } from 'IsolateTest'; import { VestTest } from 'VestTest'; import * as vest from 'vest'; @@ -17,7 +15,7 @@ describe('runAsyncTest', () => { }); suite(); - testObject = asVestTest(testObject); + testObject = VestTest.cast(testObject); expect(VestTest.isPending(testObject)).toBe(true); await wait(100); @@ -132,7 +130,7 @@ describe('runAsyncTest', () => { }); suite(); - testObject = asVestTest(testObject); + testObject = VestTest.cast(testObject); expect(VestTest.isPassing(testObject)).toBe(false); await wait(100); @@ -149,7 +147,7 @@ describe('runAsyncTest', () => { }); suite(); - testObject = asVestTest(testObject); + testObject = VestTest.cast(testObject); expect(VestTest.isFailing(testObject)).toBe(false); await wait(100); @@ -167,7 +165,7 @@ describe('runAsyncTest', () => { }); suite(); - testObject = asVestTest(testObject); + testObject = VestTest.cast(testObject); expect(VestTest.isWarning(testObject)).toBe(false); await wait(100); diff --git a/packages/vest/src/hooks/__tests__/warn.test.ts b/packages/vest/src/hooks/__tests__/warn.test.ts index 5bac845c6..523b08ed8 100644 --- a/packages/vest/src/hooks/__tests__/warn.test.ts +++ b/packages/vest/src/hooks/__tests__/warn.test.ts @@ -1,9 +1,7 @@ import { faker } from '@faker-js/faker'; -import { VestTest } from 'VestTest'; - -import { asVestTest } from '../../testUtils/asVestTest'; import { ErrorStrings } from 'ErrorStrings'; +import { VestTest } from 'VestTest'; import * as vest from 'vest'; const { create, test, warn } = vest; @@ -18,7 +16,7 @@ describe('warn hook', () => { }); })(); - expect(VestTest.warns(asVestTest(t))).toBe(true); + expect(VestTest.warns(VestTest.cast(t))).toBe(true); }); }); diff --git a/packages/vest/src/testUtils/asVestTest.ts b/packages/vest/src/testUtils/asVestTest.ts deleted file mode 100644 index 46053a332..000000000 --- a/packages/vest/src/testUtils/asVestTest.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { invariant } from 'vest-utils'; - -import { ErrorStrings } from 'ErrorStrings'; -import { TIsolateTest } from 'IsolateTest'; -import { isIsolateTest } from 'isIsolateTest'; - -export function asVestTest(value: any): TIsolateTest { - invariant(isIsolateTest(value), ErrorStrings.EXPECTED_VEST_TEST); - return value; -} diff --git a/packages/vest/tsconfig.json b/packages/vest/tsconfig.json index 3b4038049..b57f400af 100644 --- a/packages/vest/tsconfig.json +++ b/packages/vest/tsconfig.json @@ -14,7 +14,6 @@ "suiteDummy": ["./src/testUtils/suiteDummy.ts"], "partition": ["./src/testUtils/partition.ts"], "mockThrowError": ["./src/testUtils/mockThrowError.ts"], - "asVestTest": ["./src/testUtils/asVestTest.ts"], "TVestMock": ["./src/testUtils/TVestMock.ts"], "suiteRunResult": ["./src/suiteResult/suiteRunResult.ts"], "suiteResult": ["./src/suiteResult/suiteResult.ts"], @@ -92,7 +91,6 @@ "isSameProfileTest": [ "./src/core/isolate/IsolateTest/isSameProfileTest.ts" ], - "isIsolateTest": ["./src/core/isolate/IsolateTest/isIsolateTest.ts"], "cancelOverriddenPendingTest": [ "./src/core/isolate/IsolateTest/cancelOverriddenPendingTest.ts" ],