diff --git a/packages/vest/src/suite/SuiteTypes.ts b/packages/vest/src/suite/SuiteTypes.ts index 1178960f2..602a15fec 100644 --- a/packages/vest/src/suite/SuiteTypes.ts +++ b/packages/vest/src/suite/SuiteTypes.ts @@ -13,16 +13,21 @@ import { SuiteSelectors } from 'suiteSelectors'; export type Suite< F extends TFieldName, G extends TGroupName, - T extends CB = CB -> = ((...args: Parameters) => SuiteRunResult) & SuiteMethods; + T extends CB = CB, +> = ((...args: Parameters) => SuiteRunResult) & SuiteMethods; -export type SuiteMethods = { +export type SuiteMethods< + F extends TFieldName, + G extends TGroupName, + T extends CB, +> = { dump: CB; get: CB>; resume: CB; reset: CB; remove: CB; resetField: CB; + runStatic: CB, Parameters>; subscribe: (cb: CB) => CB; } & TTypedMethods & SuiteSelectors; diff --git a/packages/vest/src/suite/__tests__/staticSuite.test.ts b/packages/vest/src/suite/__tests__/staticSuite.test.ts index 31b29c6bd..3e8727ca2 100644 --- a/packages/vest/src/suite/__tests__/staticSuite.test.ts +++ b/packages/vest/src/suite/__tests__/staticSuite.test.ts @@ -89,18 +89,33 @@ describe('staticSuite', () => { expect(res.dump().children).toHaveLength(3); expect(res.dump().children?.[0]).toHaveProperty( '$type', - VestIsolateType.Test + VestIsolateType.Test, ); expect(res.dump().children?.[1]).toHaveProperty( '$type', - VestIsolateType.Test + VestIsolateType.Test, ); expect(res.dump().children?.[2]).toHaveProperty( '$type', - VestIsolateType.Group + VestIsolateType.Group, ); expect(res.dump()).toMatchSnapshot(); }); }); }); + +describe('runStatic', () => { + it('Should run a static suite', () => { + const suite = vest.create(() => { + vest.test('t1', () => false); + vest.test('t2', () => false); + }); + + const res = suite.runStatic(); + expect(suite.runStatic()).not.toBe(suite.runStatic()); + + expect(res.hasErrors('t1')).toBe(true); + expect(res.hasErrors('t2')).toBe(true); + }); +}); diff --git a/packages/vest/src/suite/createSuite.ts b/packages/vest/src/suite/createSuite.ts index 30d7c43c1..abfa3aef8 100644 --- a/packages/vest/src/suite/createSuite.ts +++ b/packages/vest/src/suite/createSuite.ts @@ -1,6 +1,8 @@ import { assign, CB } from 'vest-utils'; import { Bus, VestRuntime } from 'vestjs-runtime'; +import { TTypedMethods, getTypedMethods } from './getTypedMethods'; + import { Events } from 'BusEvents'; import { IsolateSuite, TIsolateSuite } from 'IsolateSuite'; import { useCreateVestState, useLoadSuite } from 'Runtime'; @@ -14,7 +16,6 @@ import { import { Suite } from 'SuiteTypes'; import { useInitVestBus } from 'VestBus'; import { VestReconciler } from 'VestReconciler'; -import { getTypedMethods } from 'getTypedMethods'; import { useCreateSuiteResult } from 'suiteResult'; import { useSuiteRunResult } from 'suiteRunResult'; import { bindSuiteSelectors } from 'suiteSelectors'; @@ -23,18 +24,19 @@ import { validateSuiteCallback } from 'validateSuiteParams'; function createSuite< F extends TFieldName = string, G extends TGroupName = string, - T extends CB = CB + T extends CB = CB, >(suiteName: SuiteName, suiteCallback: T): Suite; function createSuite< F extends TFieldName = string, G extends TGroupName = string, - T extends CB = CB + T extends CB = CB, >(suiteCallback: T): Suite; // @vx-allow use-use +// eslint-disable-next-line max-lines-per-function function createSuite< F extends TFieldName = string, G extends TGroupName = string, - T extends CB = CB + T extends CB = CB, >( ...args: [suiteName: SuiteName, suiteCallback: T] | [suiteCallback: T] ): Suite { @@ -55,12 +57,14 @@ function createSuite< Bus.useEmit(Events.SUITE_RUN_STARTED); return IsolateSuite( - useRunSuiteCallback(suiteCallback, ...args) + useRunSuiteCallback(suiteCallback, ...args), ); - } + }, ).output; } + const mountedStatic = staticSuite(...(args as [T])); + // Assign methods to the suite // We do this within the VestRuntime so that the suite methods // will be bound to the suite's stateRef and be able to access it. @@ -74,17 +78,18 @@ function createSuite< VestRuntime.persist(suite), { dump: VestRuntime.persist( - () => VestRuntime.useAvailableRoot() as TIsolateSuite + () => VestRuntime.useAvailableRoot() as TIsolateSuite, ), get: VestRuntime.persist(useCreateSuiteResult), remove: Bus.usePrepareEmitter(Events.REMOVE_FIELD), reset: Bus.usePrepareEmitter(Events.RESET_SUITE), resetField: Bus.usePrepareEmitter(Events.RESET_FIELD), resume: VestRuntime.persist(useLoadSuite), + runStatic: (...args: Parameters): SuiteRunResult => mountedStatic(...args) as SuiteRunResult, subscribe: VestBus.subscribe, ...bindSuiteSelectors(VestRuntime.persist(useCreateSuiteResult)), ...getTypedMethods(), - } + }, ); }); } @@ -92,7 +97,7 @@ function createSuite< function useRunSuiteCallback< T extends CB, F extends TFieldName, - G extends TGroupName + G extends TGroupName, >(suiteCallback: T, ...args: Parameters): CB> { const emit = Bus.useEmit(); @@ -103,4 +108,56 @@ function useRunSuiteCallback< }; } -export { createSuite }; +/** + * Creates a static suite for server-side validation. + * + * @param {Function} validationFn - The validation function that defines the suite's tests. + * @returns {Function} - A function that runs the validations defined in the suite. + * + * @example + * import { staticSuite, test, enforce } from 'vest'; + * + * const suite = staticSuite(data => { + * test('username', 'username is required', () => { + * enforce(data.username).isNotEmpty(); + * }); + * }); + * + * suite(data); + */ +function staticSuite< + F extends TFieldName = string, + G extends TGroupName = string, + T extends CB = CB, +>(suiteCallback: T): StaticSuite { + return assign( + (...args: Parameters) => { + const suite = createSuite(suiteCallback); + + const result = suite(...args); + + return Object.freeze( + assign( + { + dump: suite.dump, + }, + result, + ), + ); + }, + { + ...getTypedMethods(), + }, + ); +} + +export type StaticSuite< + F extends TFieldName = string, + G extends TGroupName = string, + T extends CB = CB, +> = ((...args: Parameters) => SuiteRunResult & { + dump: CB; +}) & + TTypedMethods; + +export { createSuite, staticSuite }; diff --git a/packages/vest/src/suite/staticSuite.ts b/packages/vest/src/suite/staticSuite.ts deleted file mode 100644 index 126141789..000000000 --- a/packages/vest/src/suite/staticSuite.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { CB, assign } from 'vest-utils'; - -import { TIsolateSuite } from 'IsolateSuite'; -import { SuiteRunResult, TFieldName, TGroupName } from 'SuiteResultTypes'; -import { createSuite } from 'createSuite'; -import { TTypedMethods, getTypedMethods } from 'getTypedMethods'; - -/** - * Creates a static suite for server-side validation. - * - * @param {Function} validationFn - The validation function that defines the suite's tests. - * @returns {Function} - A function that runs the validations defined in the suite. - * - * @example - * import { staticSuite, test, enforce } from 'vest'; - * - * const suite = staticSuite(data => { - * test('username', 'username is required', () => { - * enforce(data.username).isNotEmpty(); - * }); - * }); - * - * suite(data); - */ -export function staticSuite< - F extends TFieldName = string, - G extends TGroupName = string, - T extends CB = CB, ->(suiteCallback: T): StaticSuite { - return assign( - (...args: Parameters) => { - const suite = createSuite(suiteCallback); - - const result = suite(...args); - - return Object.freeze( - assign( - { - dump: suite.dump, - }, - result, - ), - ); - }, - { - ...getTypedMethods(), - }, - ); -} - -export type StaticSuite< - F extends TFieldName = string, - G extends TGroupName = string, - T extends CB = CB, -> = ((...args: Parameters) => SuiteRunResult & { - dump: CB; -}) & - TTypedMethods; diff --git a/packages/vest/src/vest.ts b/packages/vest/src/vest.ts index e09f318c6..b5413126b 100644 --- a/packages/vest/src/vest.ts +++ b/packages/vest/src/vest.ts @@ -8,7 +8,7 @@ import type { SuiteSummary, } from 'SuiteResultTypes'; import type { Suite } from 'SuiteTypes'; -import { createSuite } from 'createSuite'; +import { createSuite, staticSuite, StaticSuite } from 'createSuite'; import { each } from 'each'; import { skip, only } from 'focused'; import { group } from 'group'; @@ -16,7 +16,6 @@ import { include } from 'include'; import { mode } from 'mode'; import { omitWhen } from 'omitWhen'; import { skipWhen } from 'skipWhen'; -import { staticSuite, StaticSuite } from 'staticSuite'; import { suiteSelectors } from 'suiteSelectors'; import { test } from 'test'; import { warn } from 'warn'; diff --git a/packages/vest/tsconfig.json b/packages/vest/tsconfig.json index 0c62f5a82..3eb4d8cf8 100644 --- a/packages/vest/tsconfig.json +++ b/packages/vest/tsconfig.json @@ -35,7 +35,6 @@ "./src/suiteResult/done/shouldSkipDoneRegistration.ts" ], "deferDoneCallback": ["./src/suiteResult/done/deferDoneCallback.ts"], - "staticSuite": ["./src/suite/staticSuite.ts"], "runCallbacks": ["./src/suite/runCallbacks.ts"], "getTypedMethods": ["./src/suite/getTypedMethods.ts"], "createSuite": ["./src/suite/createSuite.ts"],