Skip to content

Commit

Permalink
patch(vest): Unify VestTest Identity functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 12, 2023
1 parent fe45064 commit 31cd18e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 75 deletions.
5 changes: 2 additions & 3 deletions packages/vest/src/core/VestBus/VestBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -42,15 +41,15 @@ export function useInitVestBus() {
});

on(RuntimeEvents.ISOLATE_PENDING, (isolate: TIsolate) => {
if (isIsolateTest(isolate)) {
if (VestTest.is(isolate)) {
VestTest.setPending(isolate);
}

CommonStateMachine.setPending(isolate);
});

on(RuntimeEvents.ISOLATE_DONE, (isolate: TIsolate) => {
if (isIsolateTest(isolate)) {
if (VestTest.is(isolate)) {
VestBus.emit(Events.TEST_COMPLETED, isolate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 (
Expand All @@ -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 */
Expand All @@ -81,7 +80,7 @@ function cancelOverriddenPendingTestOnTestReRun(
currentNode: TIsolate,
prevTestObject: TIsolateTest
) {
if (nextNode === currentNode && isIsolateTest(currentNode)) {
if (nextNode === currentNode && VestTest.is(currentNode)) {
cancelOverriddenPendingTest(prevTestObject, currentNode);
}
}
Expand All @@ -90,7 +89,7 @@ function nodeReorderDetected(
newNode: TIsolateTest,
prevNode: Maybe<TIsolate>
): boolean {
return isIsolateTest(prevNode) && !isSameProfileTest(prevNode, newNode);
return VestTest.is(prevNode) && !isSameProfileTest(prevNode, newNode);
}

function throwTestOrderError(
Expand All @@ -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,
})
Expand Down
23 changes: 11 additions & 12 deletions packages/vest/src/core/isolate/IsolateTest/TestWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TIsolate>;
Expand All @@ -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(
Expand All @@ -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
);
}

Expand All @@ -41,11 +40,11 @@ export class TestWalker {
return Walker.some(
root,
isolate => {
isIsolateTestX(isolate);
VestTest.isX(isolate);

return predicate(isolate);
},
isIsolateTest
VestTest.is
);
}

Expand All @@ -57,11 +56,11 @@ export class TestWalker {
return Walker.every(
root,
isolate => {
isIsolateTestX(isolate);
VestTest.isX(isolate);

return predicate(isolate);
},
isIsolateTest
VestTest.is
);
}

Expand All @@ -73,9 +72,9 @@ export class TestWalker {
Walker.walk(
root,
(isolate, breakout) => {
callback(castIsolateTest(isolate), breakout);
callback(VestTest.cast<F, G>(isolate), breakout);
},
isIsolateTest
VestTest.is
);
}

Expand All @@ -96,11 +95,11 @@ export class TestWalker {
Walker.pluck(
root,
isolate => {
isIsolateTestX(isolate);
VestTest.isX(isolate);

return predicate(isolate);
},
isIsolateTest
VestTest.is
);
}

Expand Down
23 changes: 22 additions & 1 deletion packages/vest/src/core/isolate/IsolateTest/VestTest.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,6 +11,7 @@ import {
} from 'IsolateTestStateMachine';
import { TestSeverity } from 'Severity';
import { TFieldName, TGroupName } from 'SuiteResultTypes';
import { VestIsolateType } from 'VestIsolateType';

export class VestTest {
// Read
Expand All @@ -21,6 +24,24 @@ export class VestTest {
return test.data;
}

static is(isolate?: Maybe<TIsolate>): isolate is TIsolateTest {
return IsolateSelectors.isIsolateType<TIsolateTest>(
isolate,
VestIsolateType.Test
);
}

static isX(isolate?: Maybe<TIsolate>): asserts isolate is TIsolateTest {
invariant(VestTest.is(isolate), ErrorStrings.EXPECTED_VEST_TEST);
}

static cast<F extends TFieldName = string, G extends TGroupName = string>(
isolate?: Maybe<TIsolate>
): TIsolateTest<F, G> {
VestTest.isX(isolate);
return isolate as TIsolateTest<F, G>;
}

static warns(test: TIsolateTest): boolean {
return VestTest.getData(test).severity === TestSeverity.Warning;
}
Expand Down
30 changes: 0 additions & 30 deletions packages/vest/src/core/isolate/IsolateTest/isIsolateTest.ts

This file was deleted.

10 changes: 4 additions & 6 deletions packages/vest/src/core/test/__tests__/runAsyncTest.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -17,7 +15,7 @@ describe('runAsyncTest', () => {
});
suite();

testObject = asVestTest(testObject);
testObject = VestTest.cast(testObject);

expect(VestTest.isPending(testObject)).toBe(true);
await wait(100);
Expand Down Expand Up @@ -132,7 +130,7 @@ describe('runAsyncTest', () => {
});
suite();

testObject = asVestTest(testObject);
testObject = VestTest.cast(testObject);

expect(VestTest.isPassing(testObject)).toBe(false);
await wait(100);
Expand All @@ -149,7 +147,7 @@ describe('runAsyncTest', () => {
});
suite();

testObject = asVestTest(testObject);
testObject = VestTest.cast(testObject);

expect(VestTest.isFailing(testObject)).toBe(false);
await wait(100);
Expand All @@ -167,7 +165,7 @@ describe('runAsyncTest', () => {
});
suite();

testObject = asVestTest(testObject);
testObject = VestTest.cast(testObject);

expect(VestTest.isWarning(testObject)).toBe(false);
await wait(100);
Expand Down
6 changes: 2 additions & 4 deletions packages/vest/src/hooks/__tests__/warn.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +16,7 @@ describe('warn hook', () => {
});
})();

expect(VestTest.warns(asVestTest(t))).toBe(true);
expect(VestTest.warns(VestTest.cast(t))).toBe(true);
});
});

Expand Down
10 changes: 0 additions & 10 deletions packages/vest/src/testUtils/asVestTest.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/vest/tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 comments on commit 31cd18e

@vercel
Copy link

@vercel vercel bot commented on 31cd18e Nov 12, 2023

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

@vercel
Copy link

@vercel vercel bot commented on 31cd18e Nov 12, 2023

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
vest-git-latest-ealush.vercel.app
www.vestjs.dev
vestjs.dev

Please sign in to comment.