From 0626dcebe7b428cada04e95cb20a24e9b8ab4065 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Mon, 13 Nov 2023 21:27:06 +0200 Subject: [PATCH] patch(vest): hasRemainingTest with or without fieldName --- .../__tests__/hasRemainingTests.test.ts | 20 ++++++------ packages/vest/src/suite/SuiteWalker.ts | 32 ++++++++++++------- packages/vest/src/suite/runCallbacks.ts | 2 +- .../vest/src/suiteResult/suiteRunResult.ts | 4 +-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/vest/src/core/isolate/IsolateTest/__tests__/hasRemainingTests.test.ts b/packages/vest/src/core/isolate/IsolateTest/__tests__/hasRemainingTests.test.ts index 0ac2de068..22680eb95 100644 --- a/packages/vest/src/core/isolate/IsolateTest/__tests__/hasRemainingTests.test.ts +++ b/packages/vest/src/core/isolate/IsolateTest/__tests__/hasRemainingTests.test.ts @@ -3,7 +3,7 @@ import wait from 'wait'; import { SuiteWalker } from 'SuiteWalker'; import * as vest from 'vest'; -describe('SuiteWalker.hasRemainingTests', () => { +describe('SuiteWalker.hasRemainingWithTestNameMatching', () => { let hasRemaining: boolean | null = null; let count = 0; @@ -15,7 +15,7 @@ describe('SuiteWalker.hasRemainingTests', () => { describe('When no remaining tests', () => { it('should return false', () => { vest.create(() => { - hasRemaining = SuiteWalker.hasRemainingTests(); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching(); })(); expect(hasRemaining).toBe(false); }); @@ -27,7 +27,7 @@ describe('SuiteWalker.hasRemainingTests', () => { vest.test('f1', async () => { await wait(100); }); - hasRemaining = SuiteWalker.hasRemainingTests(); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching(); })(); expect(hasRemaining).toBe(true); @@ -40,7 +40,7 @@ describe('SuiteWalker.hasRemainingTests', () => { await wait(100); }); count++; - hasRemaining = SuiteWalker.hasRemainingTests(); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching(); }); suite(); suite(); @@ -58,7 +58,7 @@ describe('SuiteWalker.hasRemainingTests', () => { await wait(100); }); count++; - hasRemaining = SuiteWalker.hasRemainingTests(); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching(); }); suite(); @@ -73,7 +73,7 @@ describe('SuiteWalker.hasRemainingTests', () => { describe('When no remaining tests', () => { it('Should return false', () => { vest.create(() => { - hasRemaining = SuiteWalker.hasRemainingTests('f1'); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching('f1'); })(); expect(hasRemaining).toBe(false); }); @@ -85,7 +85,7 @@ describe('SuiteWalker.hasRemainingTests', () => { vest.test('f1', async () => { await wait(100); }); - hasRemaining = SuiteWalker.hasRemainingTests('f1'); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching('f1'); })(); expect(hasRemaining).toBe(true); }); @@ -97,7 +97,7 @@ describe('SuiteWalker.hasRemainingTests', () => { await wait(100); }); count++; - hasRemaining = SuiteWalker.hasRemainingTests('f1'); + hasRemaining = SuiteWalker.hasRemainingWithTestNameMatching('f1'); }); suite(); suite(); @@ -116,8 +116,8 @@ describe('SuiteWalker.hasRemainingTests', () => { }); count++; hasRemaining = - SuiteWalker.hasRemainingTests('f1') && - SuiteWalker.hasRemainingTests('f2'); + SuiteWalker.hasRemainingWithTestNameMatching('f1') && + SuiteWalker.hasRemainingWithTestNameMatching('f2'); }); suite(); diff --git a/packages/vest/src/suite/SuiteWalker.ts b/packages/vest/src/suite/SuiteWalker.ts index 53d1df400..b244557db 100644 --- a/packages/vest/src/suite/SuiteWalker.ts +++ b/packages/vest/src/suite/SuiteWalker.ts @@ -1,4 +1,4 @@ -import { Predicates, type Predicate } from 'vest-utils'; +import { Predicates, type Predicate, isNullish } from 'vest-utils'; import { TIsolate, VestRuntime, Walker } from 'vestjs-runtime'; import { CommonStates } from 'CommonStateMachine'; @@ -17,19 +17,29 @@ export class SuiteWalker { return false; } - return Walker.some(root, (isolate: TIsolate) => { - return ( - (isolate.status === CommonStates.PENDING && predicate?.(isolate)) ?? - true - ); - }); + return Walker.some( + root, + Predicates.all(isPendingStatus, predicate ?? true) + ); } - static hasRemainingTests(fieldName?: TFieldName): boolean { + // Checks whether there are pending isolates in the tree. + // If a fieldname is provided, will only check tests with a matching fieldname. + static hasRemainingWithTestNameMatching(fieldName?: TFieldName): boolean { return SuiteWalker.hasPending( - Predicates.all(VestTest.is, (testObject: TIsolateTest) => { - return matchesOrHasNoFieldName(VestTest.getData(testObject), fieldName); - }) + Predicates.any( + isNullish(fieldName), + Predicates.all(VestTest.is, (testObject: TIsolateTest) => { + return matchesOrHasNoFieldName( + VestTest.getData(testObject), + fieldName + ); + }) + ) ); } } + +function isPendingStatus(isolate: TIsolate) { + return isolate.status === CommonStates.PENDING; +} diff --git a/packages/vest/src/suite/runCallbacks.ts b/packages/vest/src/suite/runCallbacks.ts index 06b5d131f..474a4107d 100644 --- a/packages/vest/src/suite/runCallbacks.ts +++ b/packages/vest/src/suite/runCallbacks.ts @@ -12,7 +12,7 @@ export function useRunFieldCallbacks(fieldName?: TFieldName): void { if ( fieldName && - !SuiteWalker.hasRemainingTests(fieldName) && + !SuiteWalker.hasRemainingWithTestNameMatching(fieldName) && isArray(fieldCallbacks[fieldName]) ) { callEach(fieldCallbacks[fieldName]); diff --git a/packages/vest/src/suiteResult/suiteRunResult.ts b/packages/vest/src/suiteResult/suiteRunResult.ts index 2246fb009..393350e4d 100644 --- a/packages/vest/src/suiteResult/suiteRunResult.ts +++ b/packages/vest/src/suiteResult/suiteRunResult.ts @@ -1,4 +1,3 @@ -import { SuiteWalker } from 'SuiteWalker'; import { assign } from 'vest-utils'; import { VestRuntime } from 'vestjs-runtime'; @@ -8,6 +7,7 @@ import { TFieldName, TGroupName, } from 'SuiteResultTypes'; +import { SuiteWalker } from 'SuiteWalker'; import { useDeferDoneCallback } from 'deferDoneCallback'; import { shouldSkipDoneRegistration } from 'shouldSkipDoneRegistration'; import { useCreateSuiteResult } from 'suiteResult'; @@ -43,7 +43,7 @@ function done( return output; } const useDoneCallback = () => callback(useCreateSuiteResult()); - if (!SuiteWalker.hasRemainingTests(fieldName)) { + if (!SuiteWalker.hasRemainingWithTestNameMatching(fieldName)) { useDoneCallback(); return output; }