Skip to content

Commit

Permalink
patch(vest): Add SuiteWalker
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 12, 2023
1 parent 018188b commit da2cce3
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 128 deletions.
11 changes: 6 additions & 5 deletions packages/vest/src/core/VestBus/VestBus.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SuiteWalker } from 'SuiteWalker';

Check warning on line 1 in packages/vest/src/core/VestBus/VestBus.ts

View workflow job for this annotation

GitHub Actions / build (20)

There should be at least one empty line between import groups

Check warning on line 1 in packages/vest/src/core/VestBus/VestBus.ts

View workflow job for this annotation

GitHub Actions / build (20)

`SuiteWalker` import should occur after import of `SuiteResultTypes`
import { CB, ValueOf } from 'vest-utils';
import { Bus, RuntimeEvents, TIsolate } from 'vestjs-runtime';

Expand Down Expand Up @@ -29,11 +30,6 @@ export function useInitVestBus() {
const { fieldName } = VestTest.getData(testObject);

useRunFieldCallbacks(fieldName);

if (!TestWalker.hasRemainingTests()) {
// When no more tests are running, emit the done event
VestBus.emit(Events.ALL_RUNNING_TESTS_FINISHED);
}
});

on(Events.TEST_RUN_STARTED, () => {
Expand All @@ -54,6 +50,11 @@ export function useInitVestBus() {
}

CommonStateMachine.setDone(isolate);

if (!SuiteWalker.hasPending()) {
// When no more tests are running, emit the done event
VestBus.emit(Events.ALL_RUNNING_TESTS_FINISHED);
}
});

on(Events.DONE_TEST_OMISSION_PASS, () => {
Expand Down
25 changes: 0 additions & 25 deletions packages/vest/src/core/isolate/IsolateTest/TestWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ export class TestWalker {
return !Walker.has(root, VestTest.is);
}

static someIncompleteTests(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
): boolean {
if (!root) return false;
return Walker.some(
root,
isolate => {
VestTest.isX(isolate);

return VestTest.isPending(isolate) && predicate(isolate);
},
VestTest.is
);
}

static someTests(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
Expand Down Expand Up @@ -78,15 +62,6 @@ export class TestWalker {
);
}

static hasRemainingTests(fieldName?: TFieldName): boolean {
return TestWalker.someIncompleteTests(testObject => {
if (fieldName) {
return matchingFieldName(VestTest.getData(testObject), fieldName);
}
return true;
});
}

static pluckTests(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import wait from 'wait';

import { TestWalker } from 'TestWalker';
import { SuiteWalker } from 'SuiteWalker';
import * as vest from 'vest';

describe('TestWalker.hasRemainingTests', () => {
describe('SuiteWalker.hasRemainingTests', () => {
let hasRemaining: boolean | null = null;
let count = 0;

Expand All @@ -15,7 +15,7 @@ describe('TestWalker.hasRemainingTests', () => {
describe('When no remaining tests', () => {
it('should return false', () => {
vest.create(() => {
hasRemaining = TestWalker.hasRemainingTests();
hasRemaining = SuiteWalker.hasRemainingTests();
})();
expect(hasRemaining).toBe(false);
});
Expand All @@ -27,7 +27,7 @@ describe('TestWalker.hasRemainingTests', () => {
vest.test('f1', async () => {
await wait(100);
});
hasRemaining = TestWalker.hasRemainingTests();
hasRemaining = SuiteWalker.hasRemainingTests();
})();

expect(hasRemaining).toBe(true);
Expand All @@ -40,7 +40,7 @@ describe('TestWalker.hasRemainingTests', () => {
await wait(100);
});
count++;
hasRemaining = TestWalker.hasRemainingTests();
hasRemaining = SuiteWalker.hasRemainingTests();
});
suite();
suite();
Expand All @@ -58,7 +58,7 @@ describe('TestWalker.hasRemainingTests', () => {
await wait(100);
});
count++;
hasRemaining = TestWalker.hasRemainingTests();
hasRemaining = SuiteWalker.hasRemainingTests();
});

suite();
Expand All @@ -73,7 +73,7 @@ describe('TestWalker.hasRemainingTests', () => {
describe('When no remaining tests', () => {
it('Should return false', () => {
vest.create(() => {
hasRemaining = TestWalker.hasRemainingTests('f1');
hasRemaining = SuiteWalker.hasRemainingTests('f1');
})();
expect(hasRemaining).toBe(false);
});
Expand All @@ -85,7 +85,7 @@ describe('TestWalker.hasRemainingTests', () => {
vest.test('f1', async () => {
await wait(100);
});
hasRemaining = TestWalker.hasRemainingTests('f1');
hasRemaining = SuiteWalker.hasRemainingTests('f1');
})();
expect(hasRemaining).toBe(true);
});
Expand All @@ -97,7 +97,7 @@ describe('TestWalker.hasRemainingTests', () => {
await wait(100);
});
count++;
hasRemaining = TestWalker.hasRemainingTests('f1');
hasRemaining = SuiteWalker.hasRemainingTests('f1');
});
suite();
suite();
Expand All @@ -116,8 +116,8 @@ describe('TestWalker.hasRemainingTests', () => {
});
count++;
hasRemaining =
TestWalker.hasRemainingTests('f1') &&
TestWalker.hasRemainingTests('f2');
SuiteWalker.hasRemainingTests('f1') &&
SuiteWalker.hasRemainingTests('f2');
});

suite();
Expand Down
10 changes: 10 additions & 0 deletions packages/vest/src/core/test/helpers/matchingFieldName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ export default function matchingFieldName(
): boolean {
return !!(fieldName && WithFieldName.fieldName === fieldName);
}

export function matchesOrHasNoFieldName(
WithFieldName: WithFieldName<TFieldName>,
fieldName?: Maybe<TFieldName>
): boolean {
if (fieldName) {
return matchingFieldName(WithFieldName, fieldName);
}
return true;
}
35 changes: 35 additions & 0 deletions packages/vest/src/suite/SuiteWalker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Predicates, type Predicate } from 'vest-utils';
import { TIsolate, VestRuntime, Walker } from 'vestjs-runtime';

import { CommonStates } from 'CommonStateMachine';
import { TIsolateTest } from 'IsolateTest';
import { TFieldName } from 'SuiteResultTypes';
import { VestTest } from 'VestTest';
import { matchesOrHasNoFieldName } from 'matchingFieldName';

export class SuiteWalker {
static defaultRoot = VestRuntime.useAvailableRoot;

static hasPending(predicate?: Predicate): boolean {
const root = SuiteWalker.defaultRoot();

if (!root) {
return false;
}

return Walker.some(root, (isolate: TIsolate) => {
return (
(isolate.status === CommonStates.PENDING && predicate?.(isolate)) ??
true
);
});
}

static hasRemainingTests(fieldName?: TFieldName): boolean {
return SuiteWalker.hasPending(
Predicates.all(VestTest.is, (testObject: TIsolateTest) => {
return matchesOrHasNoFieldName(VestTest.getData(testObject), fieldName);
})
);
}
}
4 changes: 2 additions & 2 deletions packages/vest/src/suite/runCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isArray, callEach } from 'vest-utils';

import { useDoneCallbacks, useFieldCallbacks } from 'Runtime';
import { TFieldName } from 'SuiteResultTypes';
import { TestWalker } from 'TestWalker';
import { SuiteWalker } from 'SuiteWalker';

/**
* Runs done callback per field when async tests are finished running.
Expand All @@ -12,7 +12,7 @@ export function useRunFieldCallbacks(fieldName?: TFieldName): void {

if (
fieldName &&
!TestWalker.hasRemainingTests(fieldName) &&
!SuiteWalker.hasRemainingTests(fieldName) &&
isArray(fieldCallbacks[fieldName])
) {
callEach(fieldCallbacks[fieldName]);
Expand Down
37 changes: 20 additions & 17 deletions packages/vest/src/suiteResult/selectors/shouldAddValidProperty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SuiteWalker } from 'SuiteWalker';

Check warning on line 1 in packages/vest/src/suiteResult/selectors/shouldAddValidProperty.ts

View workflow job for this annotation

GitHub Actions / build (20)

There should be at least one empty line between import groups

Check warning on line 1 in packages/vest/src/suiteResult/selectors/shouldAddValidProperty.ts

View workflow job for this annotation

GitHub Actions / build (20)

`SuiteWalker` import should occur after import of `SuiteResultTypes`
import { useIsOptionalFieldApplied } from 'optional';
import { Predicates } from 'vest-utils';
import { VestRuntime } from 'vestjs-runtime';

import { SuiteOptionalFields, TIsolateSuite } from 'IsolateSuite';
Expand Down Expand Up @@ -62,30 +64,31 @@ export function useShouldAddValidPropertyInGroup(

// Does the given field have any pending tests that are not optional?
function useHasNonOptionalIncomplete(fieldName?: TFieldName) {
return TestWalker.someIncompleteTests(testObject => {
if (nonMatchingFieldName(VestTest.getData(testObject), fieldName)) {
return false;
}
return !useIsOptionalFieldApplied(fieldName);
});
return SuiteWalker.hasPending(
Predicates.all(
VestTest.is,
(testObject: TIsolateTest) =>
!nonMatchingFieldName(VestTest.getData(testObject), fieldName),
() => !useIsOptionalFieldApplied(fieldName)
)
);
}

// Do the given group/field have any pending tests that are not optional?
function useHasNonOptionalIncompleteByGroup(
groupName: TGroupName,
fieldName: TFieldName
): boolean {
return TestWalker.someIncompleteTests(testObject => {
if (nonMatchingGroupName(testObject, groupName)) {
return false;
}

if (nonMatchingFieldName(VestTest.getData(testObject), fieldName)) {
return false;
}

return !useIsOptionalFieldApplied(fieldName);
});
return SuiteWalker.hasPending(
Predicates.all(
VestTest.is,
(testObject: TIsolateTest) =>
!nonMatchingGroupName(testObject, groupName),
(testObject: TIsolateTest) =>
!nonMatchingFieldName(VestTest.getData(testObject), fieldName),
() => !useIsOptionalFieldApplied(fieldName)
)
);
}

// Did all of the tests for the provided field run/omit?
Expand Down
4 changes: 2 additions & 2 deletions packages/vest/src/suiteResult/suiteRunResult.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SuiteWalker } from 'SuiteWalker';

Check warning on line 1 in packages/vest/src/suiteResult/suiteRunResult.ts

View workflow job for this annotation

GitHub Actions / build (20)

There should be at least one empty line between import groups
import { assign } from 'vest-utils';
import { VestRuntime } from 'vestjs-runtime';

Expand All @@ -7,7 +8,6 @@ import {
TFieldName,
TGroupName,
} from 'SuiteResultTypes';
import { TestWalker } from 'TestWalker';
import { useDeferDoneCallback } from 'deferDoneCallback';
import { shouldSkipDoneRegistration } from 'shouldSkipDoneRegistration';
import { useCreateSuiteResult } from 'suiteResult';
Expand Down Expand Up @@ -43,7 +43,7 @@ function done<F extends TFieldName, G extends TGroupName>(
return output;
}
const useDoneCallback = () => callback(useCreateSuiteResult());
if (!TestWalker.hasRemainingTests(fieldName)) {
if (!SuiteWalker.hasRemainingTests(fieldName)) {
useDoneCallback();
return output;
}
Expand Down
Loading

2 comments on commit da2cce3

@vercel
Copy link

@vercel vercel bot commented on da2cce3 Nov 13, 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-next-git-latest-ealush.vercel.app
vest-website.vercel.app
vest-next-ealush.vercel.app
vest-next.vercel.app

@vercel
Copy link

@vercel vercel bot commented on da2cce3 Nov 13, 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-git-latest-ealush.vercel.app
vest.vercel.app
vestjs.dev
vest-ealush.vercel.app
www.vestjs.dev

Please sign in to comment.