From 3c30c735ebff3f66d0729fe1d6b3729d6ea94931 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Sat, 26 Feb 2022 17:58:30 +0200 Subject: [PATCH] types(vest): export core types --- .../src/core/isolate/isolates/omitWhen.ts | 6 +- .../src/core/isolate/isolates/skipWhen.ts | 6 +- .../vest/src/core/state/createStateRef.ts | 6 +- packages/vest/src/core/state/stateHooks.ts | 6 +- packages/vest/src/core/suite/create.ts | 14 +- .../suite/produce/__tests__/produce.test.ts | 12 +- .../getFailures/__tests__/getFailures.test.ts | 8 +- .../__tests__/getFailuresByGroup.test.ts | 8 +- .../hasFailures/__tests__/hasFailures.test.ts | 6 +- .../vest/src/core/suite/produce/isValid.ts | 6 +- ...{produceDraft.ts => produceSuiteResult.ts} | 6 +- .../{produce.ts => produceSuiteRunResult.ts} | 24 +- packages/vest/src/exports/classnames.ts | 6 +- packages/vest/src/exports/parser.ts | 6 +- packages/vest/src/exports/promisify.ts | 8 +- packages/vest/src/hooks/include.ts | 8 +- packages/vest/src/typings/vest.d.ts | 254 ------------------ packages/vest/src/vest.ts | 5 + packages/vest/testUtils/suiteDummy.ts | 10 +- tsconfig.json | 9 +- 20 files changed, 84 insertions(+), 330 deletions(-) rename packages/vest/src/core/suite/produce/{produceDraft.ts => produceSuiteResult.ts} (91%) rename packages/vest/src/core/suite/produce/{produce.ts => produceSuiteRunResult.ts} (77%) delete mode 100644 packages/vest/src/typings/vest.d.ts diff --git a/packages/vest/src/core/isolate/isolates/omitWhen.ts b/packages/vest/src/core/isolate/isolates/omitWhen.ts index 1a9f99e8d..373927fe6 100644 --- a/packages/vest/src/core/isolate/isolates/omitWhen.ts +++ b/packages/vest/src/core/isolate/isolates/omitWhen.ts @@ -3,7 +3,7 @@ import optionalFunctionValue from 'optionalFunctionValue'; import { IsolateTypes } from 'IsolateTypes'; import ctx from 'ctx'; import { isolate } from 'isolate'; -import { produceDraft, TDraftResult } from 'produceDraft'; +import { produceSuiteResult, SuiteResult } from 'produceSuiteResult'; /** * Conditionally omits tests from the suite. @@ -15,7 +15,7 @@ import { produceDraft, TDraftResult } from 'produceDraft'; * }); */ export default function omitWhen( - conditional: boolean | ((draft: TDraftResult) => boolean), + conditional: boolean | ((draft: SuiteResult) => boolean), callback: (...args: any[]) => void ): void { isolate({ type: IsolateTypes.OMIT_WHEN }, () => { @@ -25,7 +25,7 @@ export default function omitWhen( isOmitted() || optionalFunctionValue( conditional, - optionalFunctionValue(produceDraft) + optionalFunctionValue(produceSuiteResult) ), }, () => callback() diff --git a/packages/vest/src/core/isolate/isolates/skipWhen.ts b/packages/vest/src/core/isolate/isolates/skipWhen.ts index 15a17b738..553d82f98 100644 --- a/packages/vest/src/core/isolate/isolates/skipWhen.ts +++ b/packages/vest/src/core/isolate/isolates/skipWhen.ts @@ -3,7 +3,7 @@ import optionalFunctionValue from 'optionalFunctionValue'; import { IsolateTypes } from 'IsolateTypes'; import ctx from 'ctx'; import { isolate } from 'isolate'; -import { produceDraft, TDraftResult } from 'produceDraft'; +import { produceSuiteResult, SuiteResult } from 'produceSuiteResult'; /** * Conditionally skips running tests within the callback. @@ -15,7 +15,7 @@ import { produceDraft, TDraftResult } from 'produceDraft'; * }); */ export default function skipWhen( - conditional: boolean | ((draft: TDraftResult) => boolean), + conditional: boolean | ((draft: SuiteResult) => boolean), callback: (...args: any[]) => void ): void { isolate({ type: IsolateTypes.SKIP_WHEN }, () => { @@ -28,7 +28,7 @@ export default function skipWhen( // Otherwise, we should skip the test if the conditional is true. optionalFunctionValue( conditional, - optionalFunctionValue(produceDraft) + optionalFunctionValue(produceSuiteResult) ), }, () => callback() diff --git a/packages/vest/src/core/state/createStateRef.ts b/packages/vest/src/core/state/createStateRef.ts index 9d0cce539..d748a81d9 100644 --- a/packages/vest/src/core/state/createStateRef.ts +++ b/packages/vest/src/core/state/createStateRef.ts @@ -2,7 +2,7 @@ import type { NestedArray } from 'nestedArray'; import type { TState } from 'vast'; import VestTest from 'VestTest'; -import type { TDraftResult } from 'produceDraft'; +import type { SuiteResult } from 'produceSuiteResult'; export default function createStateRef( state: TState, @@ -15,8 +15,8 @@ export default function createStateRef( suiteId: state.registerStateKey(suiteId), suiteName: state.registerStateKey(suiteName), testCallbacks: state.registerStateKey<{ - fieldCallbacks: Record void>>; - doneCallbacks: Array<(res: TDraftResult) => void>; + fieldCallbacks: Record void>>; + doneCallbacks: Array<(res: SuiteResult) => void>; }>(() => ({ fieldCallbacks: {}, doneCallbacks: [], diff --git a/packages/vest/src/core/state/stateHooks.ts b/packages/vest/src/core/state/stateHooks.ts index d5f38dec5..3dbdc096e 100644 --- a/packages/vest/src/core/state/stateHooks.ts +++ b/packages/vest/src/core/state/stateHooks.ts @@ -7,7 +7,7 @@ import type { TStateHandlerReturn } from 'vast'; import VestTest from 'VestTest'; import type { TStateRef } from 'createStateRef'; import ctx from 'ctx'; -import type { TDraftResult } from 'produceDraft'; +import type { SuiteResult } from 'produceSuiteResult'; // STATE REF export function useStateRef(): Exclude { @@ -23,8 +23,8 @@ export function useSuiteName(): string | void { return useStateRef().suiteName()[0]; } export function useTestCallbacks(): TStateHandlerReturn<{ - fieldCallbacks: Record void)[]>; - doneCallbacks: ((res: TDraftResult) => void)[]; + fieldCallbacks: Record void)[]>; + doneCallbacks: ((res: SuiteResult) => void)[]; }> { return useStateRef().testCallbacks(); } diff --git a/packages/vest/src/core/suite/create.ts b/packages/vest/src/core/suite/create.ts index e5dc3e8dd..9733b9bd5 100644 --- a/packages/vest/src/core/suite/create.ts +++ b/packages/vest/src/core/suite/create.ts @@ -8,12 +8,12 @@ import { IsolateTypes } from 'IsolateTypes'; import createStateRef from 'createStateRef'; import context from 'ctx'; import { isolate } from 'isolate'; -import { IVestResult, produceFullResult } from 'produce'; -import { produceDraft, TDraftResult } from 'produceDraft'; +import { produceSuiteResult, SuiteResult } from 'produceSuiteResult'; +import { SuiteRunResult, produceFullResult } from 'produceSuiteRunResult'; import { initBus, Events } from 'vestBus'; type CreateProperties = { - get: () => TDraftResult; + get: () => SuiteResult; reset: () => void; resetField: (fieldName: string) => void; remove: (fieldName: string) => void; @@ -22,7 +22,7 @@ type CreateProperties = { type CB = (...args: any[]) => void; type SuiteReturnType = { - (...args: Parameters): IVestResult; + (...args: Parameters): SuiteRunResult; } & CreateProperties; /** @@ -61,9 +61,9 @@ function create( const stateRef = createStateRef(state, { suiteId: genId(), suiteName }); interface IVestSuite { - (...args: Parameters): IVestResult; + (...args: Parameters): SuiteRunResult; - get: () => TDraftResult; + get: () => SuiteResult; reset: () => void; resetField: (fieldName: string) => void; remove: (fieldName: string) => void; @@ -92,7 +92,7 @@ function create( return produceFullResult(); }), { - get: context.bind(ctxRef, produceDraft), + get: context.bind(ctxRef, produceSuiteResult), remove: context.bind(ctxRef, (fieldName: string) => { bus.emit(Events.REMOVE_FIELD, fieldName); }), diff --git a/packages/vest/src/core/suite/produce/__tests__/produce.test.ts b/packages/vest/src/core/suite/produce/__tests__/produce.test.ts index ecaac2080..ce6306721 100644 --- a/packages/vest/src/core/suite/produce/__tests__/produce.test.ts +++ b/packages/vest/src/core/suite/produce/__tests__/produce.test.ts @@ -1,12 +1,12 @@ -import { produceFullResult } from 'produce'; -import { produceDraft } from 'produceDraft'; - import itWithContext from '../../../../../testUtils/itWithContext'; import { dummyTest } from '../../../../../testUtils/testDummy'; import { setTestObjects } from '../../../../../testUtils/testObjects'; +import { produceSuiteResult } from 'produceSuiteResult'; +import { produceFullResult } from 'produceSuiteRunResult'; + const methods = { - produceDraft, + produceSuiteResult, produceFullResult, }; @@ -103,10 +103,10 @@ describe.each(Object.keys(methods))('produce method: %s', methodName => { }); }); -describe('produceDraft', () => { +describe('produceSuiteResult', () => { describe('exposed methods', () => { itWithContext('Should have all exposed methods', () => { - expect(produceDraft()).toMatchInlineSnapshot(` + expect(produceSuiteResult()).toMatchInlineSnapshot(` Object { "errorCount": 0, "getErrors": [Function], diff --git a/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailures.test.ts b/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailures.test.ts index 9ff205292..b5aff5e03 100644 --- a/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailures.test.ts +++ b/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailures.test.ts @@ -1,12 +1,12 @@ -import { produceFullResult } from 'produce'; -import { produceDraft } from 'produceDraft'; - import itWithContext from '../../../../../../testUtils/itWithContext'; import { dummyTest } from '../../../../../../testUtils/testDummy'; import { setTestObjects } from '../../../../../../testUtils/testObjects'; +import { produceSuiteResult } from 'produceSuiteResult'; +import { produceFullResult } from 'produceSuiteRunResult'; + const methods = { - produceDraft, + produceSuiteResult, produceFullResult, }; diff --git a/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailuresByGroup.test.ts b/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailuresByGroup.test.ts index 3acdd4599..13acfbfe2 100644 --- a/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailuresByGroup.test.ts +++ b/packages/vest/src/core/suite/produce/getFailures/__tests__/getFailuresByGroup.test.ts @@ -1,12 +1,12 @@ -import { produceFullResult } from 'produce'; -import { produceDraft } from 'produceDraft'; - import itWithContext from '../../../../../../testUtils/itWithContext'; import { dummyTest } from '../../../../../../testUtils/testDummy'; import { setTestObjects } from '../../../../../../testUtils/testObjects'; +import { produceSuiteResult } from 'produceSuiteResult'; +import { produceFullResult } from 'produceSuiteRunResult'; + const methods = { - produceDraft, + produceSuiteResult, produceFullResult, }; diff --git a/packages/vest/src/core/suite/produce/hasFailures/__tests__/hasFailures.test.ts b/packages/vest/src/core/suite/produce/hasFailures/__tests__/hasFailures.test.ts index 2f69ff8a7..0d86bde56 100644 --- a/packages/vest/src/core/suite/produce/hasFailures/__tests__/hasFailures.test.ts +++ b/packages/vest/src/core/suite/produce/hasFailures/__tests__/hasFailures.test.ts @@ -7,11 +7,11 @@ import { emptyTestObjects, } from '../../../../../../testUtils/testObjects'; -import { produceFullResult } from 'produce'; -import { produceDraft } from 'produceDraft'; +import { produceSuiteResult } from 'produceSuiteResult'; +import { produceFullResult } from 'produceSuiteRunResult'; const methods = { - produceDraft, + produceSuiteResult, produceFullResult, }; diff --git a/packages/vest/src/core/suite/produce/isValid.ts b/packages/vest/src/core/suite/produce/isValid.ts index 6c612b571..d47a1ebf0 100644 --- a/packages/vest/src/core/suite/produce/isValid.ts +++ b/packages/vest/src/core/suite/produce/isValid.ts @@ -1,7 +1,7 @@ import { isNotEmpty, isEmpty } from 'isEmpty'; import { nonMatchingFieldName } from 'matchingFieldName'; -import type { TDraftResult } from 'produceDraft'; +import type { SuiteResult } from 'produceSuiteResult'; import { useTestsFlat, useAllIncomplete, @@ -10,7 +10,7 @@ import { } from 'stateHooks'; // eslint-disable-next-line max-statements, complexity -export function isValid(result: TDraftResult, fieldName?: string): boolean { +export function isValid(result: SuiteResult, fieldName?: string): boolean { if (fieldIsOmitted(fieldName)) { return true; } @@ -59,7 +59,7 @@ function hasNonOptionalIncomplete(fieldName?: string) { ); } -function fieldDoesNotExist(result: TDraftResult, fieldName?: string): boolean { +function fieldDoesNotExist(result: SuiteResult, fieldName?: string): boolean { return !!fieldName && isEmpty(result.tests[fieldName]); } diff --git a/packages/vest/src/core/suite/produce/produceDraft.ts b/packages/vest/src/core/suite/produce/produceSuiteResult.ts similarity index 91% rename from packages/vest/src/core/suite/produce/produceDraft.ts rename to packages/vest/src/core/suite/produce/produceSuiteResult.ts index 583095c01..5b5dcc86f 100644 --- a/packages/vest/src/core/suite/produce/produceDraft.ts +++ b/packages/vest/src/core/suite/produce/produceSuiteResult.ts @@ -12,7 +12,7 @@ import { useStateRef, useTestsFlat, useSuiteName } from 'stateHooks'; const cache = createCache(20); -export function produceDraft(): TDraftResult { +export function produceSuiteResult(): SuiteResult { const testObjects = useTestsFlat(); const ctxRef = { stateRef: useStateRef() }; @@ -31,7 +31,7 @@ export function produceDraft(): TDraftResult { hasWarnings: ctx.bind(ctxRef, hasWarnings), hasWarningsByGroup: ctx.bind(ctxRef, hasWarningsByGroup), isValid: ctx.bind(ctxRef, (fieldName?: string) => - isValid(produceDraft(), fieldName) + isValid(produceSuiteResult(), fieldName) ), suiteName, }); @@ -39,7 +39,7 @@ export function produceDraft(): TDraftResult { ); } -export type TDraftResult = ReturnType & { +export type SuiteResult = ReturnType & { /** * Returns whether the suite as a whole is valid. * Determined if there are no errors, and if no diff --git a/packages/vest/src/core/suite/produce/produce.ts b/packages/vest/src/core/suite/produce/produceSuiteRunResult.ts similarity index 77% rename from packages/vest/src/core/suite/produce/produce.ts rename to packages/vest/src/core/suite/produce/produceSuiteRunResult.ts index cbc65fabf..807c788e1 100644 --- a/packages/vest/src/core/suite/produce/produce.ts +++ b/packages/vest/src/core/suite/produce/produceSuiteRunResult.ts @@ -5,18 +5,18 @@ import isFunction from 'isFunction'; import ctx from 'ctx'; import hasRemainingTests from 'hasRemainingTests'; -import { produceDraft, TDraftResult } from 'produceDraft'; +import { produceSuiteResult, SuiteResult } from 'produceSuiteResult'; import { useStateRef, useTestCallbacks, useTestsFlat } from 'stateHooks'; const cache = createCache(20); -export function produceFullResult(): IVestResult { +export function produceFullResult(): SuiteRunResult { const testObjects = useTestsFlat(); const ctxRef = { stateRef: useStateRef() }; return cache( [testObjects], ctx.bind(ctxRef, () => - assign({}, produceDraft(), { + assign({}, produceSuiteResult(), { done: ctx.bind(ctxRef, done), }) ) @@ -28,10 +28,10 @@ export function produceFullResult(): IVestResult { */ function shouldSkipDoneRegistration( - callback: (res: TDraftResult) => void, + callback: (res: SuiteResult) => void, fieldName: string | undefined, - output: IVestResult + output: SuiteRunResult ): boolean { // If we do not have any test runs for the current field return !!( @@ -54,9 +54,9 @@ function shouldRunDoneCallback(fieldName?: string): boolean { * Registers done callbacks. * @register {Object} Vest output object. */ -const done: IDone = function done(...args): IVestResult { +const done: IDone = function done(...args): SuiteRunResult { const [callback, fieldName] = args.reverse() as [ - (res: TDraftResult) => void, + (res: SuiteResult) => void, string ]; @@ -66,7 +66,7 @@ const done: IDone = function done(...args): IVestResult { return output; } - const doneCallback = () => callback(produceDraft()); + const doneCallback = () => callback(produceSuiteResult()); if (shouldRunDoneCallback(fieldName)) { doneCallback(); @@ -93,9 +93,11 @@ function deferDoneCallback(doneCallback: () => void, fieldName?: string): void { }); } -export type IVestResult = TDraftResult & { done: IDone }; +export type SuiteRunResult = SuiteResult & { done: IDone }; interface IDone { - (...args: [cb: (res: TDraftResult) => void]): IVestResult; - (...args: [fieldName: string, cb: (res: TDraftResult) => void]): IVestResult; + (...args: [cb: (res: SuiteResult) => void]): SuiteRunResult; + ( + ...args: [fieldName: string, cb: (res: SuiteResult) => void] + ): SuiteRunResult; } diff --git a/packages/vest/src/exports/classnames.ts b/packages/vest/src/exports/classnames.ts index 3a0067f96..42b9beb60 100644 --- a/packages/vest/src/exports/classnames.ts +++ b/packages/vest/src/exports/classnames.ts @@ -2,14 +2,14 @@ import isFunction from 'isFunction'; import throwError from 'throwError'; import { parse } from 'parser'; -import type { IVestResult } from 'produce'; -import type { TDraftResult } from 'produceDraft'; +import type { SuiteResult } from 'produceSuiteResult'; +import type { SuiteRunResult } from 'produceSuiteRunResult'; /** * Creates a function that returns class names that match the validation result */ export default function classnames( - res: IVestResult | TDraftResult, + res: SuiteRunResult | SuiteResult, classes: TSupportedClasses = {} ): (fieldName: string) => string { if (!res || !isFunction(res.hasErrors)) { diff --git a/packages/vest/src/exports/parser.ts b/packages/vest/src/exports/parser.ts index d530ed13c..20497723e 100644 --- a/packages/vest/src/exports/parser.ts +++ b/packages/vest/src/exports/parser.ts @@ -1,10 +1,10 @@ import { greaterThan } from 'greaterThan'; import hasOwnProperty from 'hasOwnProperty'; -import type { IVestResult } from 'produce'; -import type { TDraftResult } from 'produceDraft'; +import type { SuiteResult } from 'produceSuiteResult'; +import type { SuiteRunResult } from 'produceSuiteRunResult'; -export function parse(res: IVestResult | TDraftResult): { +export function parse(res: SuiteRunResult | SuiteResult): { valid: (fieldName?: string) => boolean; tested: (fieldName?: string) => boolean; invalid: (fieldName?: string) => boolean; diff --git a/packages/vest/src/exports/promisify.ts b/packages/vest/src/exports/promisify.ts index 39657da34..57dc44b88 100644 --- a/packages/vest/src/exports/promisify.ts +++ b/packages/vest/src/exports/promisify.ts @@ -1,12 +1,12 @@ import isFunction from 'isFunction'; import throwError from 'throwError'; -import { IVestResult } from 'produce'; -import { TDraftResult } from 'produceDraft'; +import { SuiteResult } from 'produceSuiteResult'; +import { SuiteRunResult } from 'produceSuiteRunResult'; const promisify = - (validatorFn: (...args: any[]) => IVestResult) => - (...args: any[]): Promise => { + (validatorFn: (...args: any[]) => SuiteRunResult) => + (...args: any[]): Promise => { if (!isFunction(validatorFn)) { throwError('promisify: Expected validatorFn to be a function.'); } diff --git a/packages/vest/src/hooks/include.ts b/packages/vest/src/hooks/include.ts index 95cb99220..532547e62 100644 --- a/packages/vest/src/hooks/include.ts +++ b/packages/vest/src/hooks/include.ts @@ -4,11 +4,11 @@ import { isStringValue } from 'isStringValue'; import optionalFunctionValue from 'optionalFunctionValue'; import ctx from 'ctx'; -import { produceDraft, TDraftResult } from 'produceDraft'; +import { produceSuiteResult, SuiteResult } from 'produceSuiteResult'; export default function include(fieldName: string): { when: ( - condition: string | boolean | ((draft: TDraftResult) => boolean) + condition: string | boolean | ((draft: SuiteResult) => boolean) ) => void; } { const context = ctx.useX(); @@ -23,7 +23,7 @@ export default function include(fieldName: string): { return { when }; function when( - condition: string | ((draft: TDraftResult) => boolean) | boolean + condition: string | ((draft: SuiteResult) => boolean) | boolean ): void { const context = ctx.useX(); const { inclusion, exclusion } = context; @@ -39,7 +39,7 @@ export default function include(fieldName: string): { return optionalFunctionValue( condition, - optionalFunctionValue(produceDraft) + optionalFunctionValue(produceSuiteResult) ); }; } diff --git a/packages/vest/src/typings/vest.d.ts b/packages/vest/src/typings/vest.d.ts deleted file mode 100644 index 21e729de4..000000000 --- a/packages/vest/src/typings/vest.d.ts +++ /dev/null @@ -1,254 +0,0 @@ -import { TEnforce } from './enforce'; -import { IVestResult, DraftResult } from './vestResult'; - -type TestCB = () => void | Promise | false; -type TestArgsCB = (...args: any[]) => void | Promise | false; -type MessageFunc = (...args: any[]) => string; -type ExclusionArg = string | string[] | void; - -export = vest; -export as namespace vest; - -interface VestTest { - failed: boolean; - fieldName: string; - id: string; - isWarning: boolean; - statement: string; - testFn: TestCB; - asyncTest?: Promise; -} - -interface ITestEach { - /** - * Run multiple tests using a parameter table - * @param {String} fieldName Name of the field to test. - * @param {String|function} message The message returned in case of a failure. Follows printf syntax. - * @param {function} testFn The actual test callback. - * - * @example - * - * test.each([[1,2,3],[2,1,3]])('test', (a, b, c) => `${a} + ${b} does not equal ${c}`, (a, b, c) => enforce(a + b).equals(c)); - */ - ( - fieldName: string | MessageFunc, - message: string | MessageFunc, - testFn: TestArgsCB - ): VestTest[]; - /** - * Run multiple tests using a parameter table - * @param {String} fieldName Name of the field to test. - * @param {function} testFn The actual test callback. - * - * @example - * - * test.each([[1,2,3],[2,1,3]])('test', (a, b, c) => enforce(a + b).equals(c)); - */ - (fieldName: string | MessageFunc, testFn: TestArgsCB): VestTest[]; -} - -interface ITest { - /** - * Runs a single test - * @param fieldName The name of the field being validated - * @param message Descriptive message to display on failure - * @param testFn Test body - * - * @example - * - * test('username', 'Username is required', () => { - * enforce(username).isNotEmpty(); - * }); - */ - (fieldName: string, message: string, testFn: TestCB): VestTest; - /** - * Runs a single test - * @param fieldName The name of the field being validated - * @param testFn Test body - * - * @example - * - * test('username', () => { - * enforce(username).isNotEmpty(); - * }); - */ - (fieldName: string, testFn: TestCB): VestTest; - /** - * Runs a single test. Memoizes the result based on the dependency array. - * @param fieldName The name of the field being validated - * @param message Descriptive message to display on failure - * @param testFn Test body - * @param dependencies An array of values to be checked for strict equality ('===') - * - * @example - * - * test.memo('username', 'Username already exists', () => doesUserExist(username), [username]) - */ - memo( - fieldName: string, - message: string, - testFn: TestCB, - dependencies: any[] - ): VestTest; - /** - * Runs a single test. Memoizes the result based on the dependency array. - * @param fieldName The name of the field being validated - * @param testFn Test body - * @param dependencies An array of values to be checked for strict equality ('===') - * - * @example - * - * test.memo('username', () => doesUserExist(username), [username]) - */ - memo(fieldName: string, testFn: TestCB, dependencies: any[]): VestTest; - /** - * Create test.each with table of parameters - * @param {any[]} table Array of arrays with params for each run, although it - * will accept 1d-array and treat every item as size one array (e.g. [1,2,3] -> [[1],[2],[3]]) - * @example - * - * test.each([[1,2,3],[2,1,3]])('test', 'failed', (a, b, c) => enforce(a + b).equals(c)); - */ - each(table: any[]): ITestEach; -} - -interface ISkip { - /** - * Skips provided field from current run. - * When undefined, the expression is ignored. - * @param [fieldName] Field name or a list of fields to exclude - * - * @example - * - * vest.skip('username'); - * vest.skip(['username', 'password']); - */ - (fieldNames?: ExclusionArg): void; - - /** - * Skips provided group from current run. - * When undefined, the expression is ignored. - * @param [groupName] group name or a list of group to exclude - * - * @example - * - * vest.skip.group('section_1'); - * vest.skip.group(['section_1', 'section_2']); - */ - group(groupName?: ExclusionArg): void; -} - -interface IOnly { - /** - * Singles out a field name to be tested. - * When fieldName is undefined, the expression is ignored. - * @param [fieldName] Field name or a list of fields to include - * - * @example - * - * vest.only('username'); - * vest.only(['username', 'password']); - */ - (fieldName?: ExclusionArg): void; - /** - * Singles out a group name to be tested. - * When groupName is undefined, the expression is ignored. - * @param [groupName] Field name or a list of groups to include. - * - * @example - * - * vest.only.group('section_1'); - * vest.only.group(['section_1', 'section_2']); - */ - group(groupName?: ExclusionArg): void; -} - -interface ISubscribePayload { - type: string; - suiteState: Record; - key?: string; - value?: any; -} - -interface ICreateResult { - (...args: any[]): IVestResult; - get: () => DraftResult; - reset: () => void; - remove: (fieldName: string) => void; - - subscribe: (payload: ISubscribePayload) => void; -} - -interface ISkipWhen { - (conditional: boolean | (() => boolean), callback: () => void): void; -} - -declare namespace vest { - const enforce: TEnforce; - const test: ITest; - const only: IOnly; - const skip: ISkip; - const skipWhen: ISkipWhen; - - function optional(optionalFields: string | string[]): boolean; - - /** - * Runs a stateful validation suite. - * @param [suiteName] Unique suite name. - * @param tests Suite body. - * - * @example - * - * const validate = vest.create('form_name', (data = {}) => { - * // your tests go here - * }); - * - * const res = validate({username: 'example'}); - */ - function create( - suiteName: string, - tests: (...args: any[]) => void - ): ICreateResult; - - /** - * Runs a stateful validation suite. - * @param tests Suite body. - * - * @example - * - * const validate = vest.create((data = {}) => { - * // your tests go here - * }); - * - * const res = validate({username: 'example'}); - */ - function create(tests: (...args: any[]) => void): ICreateResult; - /** - * Allows grouping tests so you can handle them together - * @param groupName Group name. - * @param testFn Group body. - * - * @example - * - * group('sign_up', () => { - * // your tests go here - * }); - */ - function group(groupName: string, testFn: () => void): void; - - /** - * Marks a test as warning. - * Needs to run within a test body. - * - * @example - * - * test('password', 'Consider using a longer password.', () => { - * vest.warn(); - * - * enforce(password).longerThan(10); - * }); - */ - function warn(): void; - - const VERSION: string; -} diff --git a/packages/vest/src/vest.ts b/packages/vest/src/vest.ts index 35aff2cae..3c0217f4f 100644 --- a/packages/vest/src/vest.ts +++ b/packages/vest/src/vest.ts @@ -1,5 +1,6 @@ import { enforce } from 'n4s'; +import VestTest from 'VestTest'; import create from 'create'; import context from 'ctx'; import each from 'each'; @@ -9,6 +10,8 @@ import include from 'include'; import { eager } from 'mode'; import omitWhen from 'omitWhen'; import optional from 'optionalTests'; +import type { SuiteResult } from 'produceSuiteResult'; +import type { SuiteRunResult } from 'produceSuiteRunResult'; import skipWhen from 'skipWhen'; import { test } from 'test'; import warn from 'warn'; @@ -31,3 +34,5 @@ export { include, eager, }; + +export type { SuiteResult, SuiteRunResult, VestTest }; diff --git a/packages/vest/testUtils/suiteDummy.ts b/packages/vest/testUtils/suiteDummy.ts index dd5d545d2..54517b6e1 100644 --- a/packages/vest/testUtils/suiteDummy.ts +++ b/packages/vest/testUtils/suiteDummy.ts @@ -5,25 +5,25 @@ import { dummyTest } from './testDummy'; import { optional, create, skip } from 'vest'; export function failing(failingFields?: string | string[]) { - return createSuiteResult(failingFields, fieldName => { + return createSuiteRunResult(failingFields, fieldName => { dummyTest.failing(fieldName); }); } export function warning(failingFields?: string | string[]) { - return createSuiteResult(failingFields, fieldName => { + return createSuiteRunResult(failingFields, fieldName => { dummyTest.failingWarning(fieldName); }); } export function failingAsync(failingFields?: string | string[]) { - return createSuiteResult(failingFields, fieldName => { + return createSuiteRunResult(failingFields, fieldName => { dummyTest.failingAsync(fieldName); }); } export function passing(fields?: string | string[]) { - return createSuiteResult(fields, fieldName => { + return createSuiteRunResult(fields, fieldName => { dummyTest.passing(fieldName); }); } @@ -87,7 +87,7 @@ export function untested(fields?: string | string[]) { return suite.get(); } -function createSuiteResult( +function createSuiteRunResult( fieldNames: string[] | string | undefined, callback: (fieldName?: string) => void // eslint-disable-line no-unused-vars ) { diff --git a/tsconfig.json b/tsconfig.json index be7415c81..66a7f1a9e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -162,9 +162,11 @@ "nonMatchingSeverityProfile": [ "./packages/vest/src/core/suite/produce/nonMatchingSeverityProfile.ts" ], - "produce": ["./packages/vest/src/core/suite/produce/produce.ts"], - "produceDraft": [ - "./packages/vest/src/core/suite/produce/produceDraft.ts" + "produceSuiteResult": [ + "./packages/vest/src/core/suite/produce/produceSuiteResult.ts" + ], + "produceSuiteRunResult": [ + "./packages/vest/src/core/suite/produce/produceSuiteRunResult.ts" ], "runCallbacks": [ "./packages/vest/src/core/suite/produce/runCallbacks.ts" @@ -212,7 +214,6 @@ "Modes": ["./packages/vest/src/hooks/mode/Modes.ts"], "optionalTests": ["./packages/vest/src/hooks/optionalTests.ts"], "warn": ["./packages/vest/src/hooks/warn.ts"], - "vest.d": ["./packages/vest/src/typings/vest.d.ts"], "vest": ["./packages/vest/src/vest.ts"] } },