diff --git a/packages/vest/src/isolates/omitWhen.ts b/packages/vest/src/isolates/omitWhen.ts index f99cfa7b3..a32085cb4 100644 --- a/packages/vest/src/isolates/omitWhen.ts +++ b/packages/vest/src/isolates/omitWhen.ts @@ -6,7 +6,7 @@ import { SuiteContext, useOmitted } from 'SuiteContext'; import { TFieldName, TGroupName } from 'SuiteResultTypes'; import { VestIsolateType } from 'VestIsolateType'; import { TDraftCondition } from 'getTypedMethods'; -import { useCreateSuiteResult } from 'suiteResult'; +import { LazyDraft } from 'LazyDraft'; /** * Conditionally omits tests from the suite. @@ -27,10 +27,7 @@ export function omitWhen( { omitted: useWithinActiveOmitWhen() || - optionalFunctionValue( - conditional, - optionalFunctionValue(useCreateSuiteResult), - ), + optionalFunctionValue(conditional, LazyDraft()), }, callback, ); diff --git a/packages/vest/src/isolates/skipWhen.ts b/packages/vest/src/isolates/skipWhen.ts index 37ff4cd18..bd56bf047 100644 --- a/packages/vest/src/isolates/skipWhen.ts +++ b/packages/vest/src/isolates/skipWhen.ts @@ -5,7 +5,7 @@ import { SuiteContext, useSkipped } from 'SuiteContext'; import { TFieldName, TGroupName } from 'SuiteResultTypes'; import { VestIsolateType } from 'VestIsolateType'; import { TDraftCondition } from 'getTypedMethods'; -import { useCreateSuiteResult } from 'suiteResult'; +import { LazyDraft } from 'LazyDraft'; /** * Conditionally skips running tests within the callback. @@ -29,10 +29,7 @@ export function skipWhen( // we should skip the test if the parent conditional is true. useIsExcludedIndividually() || // Otherwise, we should skip the test if the conditional is true. - optionalFunctionValue( - condition, - optionalFunctionValue(useCreateSuiteResult), - ), + optionalFunctionValue(condition, LazyDraft()), }, callback, ); diff --git a/packages/vest/src/suiteResult/selectors/LazyDraft.ts b/packages/vest/src/suiteResult/selectors/LazyDraft.ts new file mode 100644 index 000000000..e5a82407c --- /dev/null +++ b/packages/vest/src/suiteResult/selectors/LazyDraft.ts @@ -0,0 +1,22 @@ +import { constructSuiteResultObject, useCreateSuiteResult } from 'suiteResult'; +import { + SuiteResult, + SuiteSummary, + TFieldName, + TGroupName, +} from 'SuiteResultTypes'; + +export function LazyDraft< + F extends TFieldName, + G extends TGroupName, +>(): SuiteResult { + const emptySummary = constructSuiteResultObject(new SuiteSummary()); + + return new Proxy(emptySummary, { + get: (_, prop) => { + const result = useCreateSuiteResult(); + + return result[prop as keyof SuiteResult]; + }, + }) as SuiteResult; +} diff --git a/packages/vest/src/suiteResult/suiteResult.ts b/packages/vest/src/suiteResult/suiteResult.ts index 76086110d..897675254 100644 --- a/packages/vest/src/suiteResult/suiteResult.ts +++ b/packages/vest/src/suiteResult/suiteResult.ts @@ -1,7 +1,12 @@ -import { freezeAssign } from 'vest-utils'; +import { assign, Maybe } from 'vest-utils'; import { useSuiteName, useSuiteResultCache } from 'Runtime'; -import { SuiteResult, TFieldName, TGroupName } from 'SuiteResultTypes'; +import { + SuiteResult, + SuiteSummary, + TFieldName, + TGroupName, +} from 'SuiteResultTypes'; import { suiteSelectors } from 'suiteSelectors'; import { useProduceSuiteSummary } from 'useProduceSuiteSummary'; @@ -15,12 +20,16 @@ export function useCreateSuiteResult< // @vx-allow use-use const suiteName = useSuiteName(); - return freezeAssign>( - summary, - suiteSelectors(summary), - { - suiteName, - }, - ) as SuiteResult; + + return Object.freeze(constructSuiteResultObject(summary, suiteName)); }); } + +export function constructSuiteResultObject< + F extends TFieldName, + G extends TGroupName, +>(summary: SuiteSummary, suiteName?: Maybe): SuiteResult { + return assign(summary, suiteSelectors(summary), { + suiteName, + }) as SuiteResult; +} diff --git a/packages/vest/tsconfig.json b/packages/vest/tsconfig.json index 8feccd6d6..7c311562d 100644 --- a/packages/vest/tsconfig.json +++ b/packages/vest/tsconfig.json @@ -30,6 +30,7 @@ "./src/suiteResult/selectors/hasFailuresByTestObjects.ts" ], "collectFailures": ["./src/suiteResult/selectors/collectFailures.ts"], + "LazyDraft": ["./src/suiteResult/selectors/LazyDraft.ts"], "shouldSkipDoneRegistration": [ "./src/suiteResult/done/shouldSkipDoneRegistration.ts" ],