From e13b55845c911c699826f8276bdf05f626c0d233 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Mon, 7 Mar 2022 16:30:16 +0200 Subject: [PATCH] patch: use invariant utility --- packages/context/src/context.ts | 11 +++-- packages/n4s/src/exports/compose.ts | 14 ++----- packages/n4s/src/lib/transformResult.ts | 11 +++-- packages/n4s/src/runtime/enforceEager.ts | 19 ++++----- .../shared/src/__tests__/throwError.test.ts | 41 ------------------- packages/shared/src/invariant.ts | 19 +++++++++ packages/shared/src/throwError.ts | 21 +--------- packages/vest/src/__tests__/isolate.test.ts | 14 +++---- .../vest/src/core/isolate/isolates/each.ts | 6 +-- .../vest/src/core/isolate/isolates/group.ts | 14 +++---- packages/vest/src/core/suite/create.ts | 9 ++-- .../produce/getFailures/getFailuresByGroup.ts | 15 ++++--- .../vest/src/core/test/__tests__/key.test.ts | 6 +-- .../merging_of_previous_test_runs.test.ts | 20 ++++----- packages/vest/src/core/test/key.ts | 4 +- .../vest/src/core/test/lib/registerTest.ts | 3 +- .../vest/src/core/test/lib/useTestAtCursor.ts | 8 ++-- packages/vest/src/core/test/test.ts | 24 +++++------ packages/vest/src/core/vestBus.ts | 6 +-- packages/vest/src/exports/classnames.ts | 11 +++-- packages/vest/src/exports/promisify.ts | 9 ++-- packages/vest/src/hooks/warn.ts | 6 +-- packages/vest/testUtils/mockThrowError.ts | 9 ++-- tsconfig.json | 1 + 24 files changed, 118 insertions(+), 183 deletions(-) delete mode 100644 packages/shared/src/__tests__/throwError.test.ts create mode 100644 packages/shared/src/invariant.ts diff --git a/packages/context/src/context.ts b/packages/context/src/context.ts index 4518dc0a1..f6ac2ae30 100644 --- a/packages/context/src/context.ts +++ b/packages/context/src/context.ts @@ -1,7 +1,7 @@ import assign from 'assign'; import defaultTo from 'defaultTo'; +import invariant from 'invariant'; import optionalFunctionValue from 'optionalFunctionValue'; -import throwError from 'throwError'; // eslint-disable-next-line max-lines-per-function export function createContext>( @@ -22,12 +22,11 @@ export function createContext>( }; function useX(errorMessage?: string): T { - return ( - (storage.ctx as T) ?? - throwError( - defaultTo(errorMessage, 'Context was used after it was closed') - ) + invariant( + storage.ctx, + defaultTo(errorMessage, 'Context was used after it was closed') ); + return storage.ctx as T; } function run(ctxRef: Partial, fn: (context: T) => R): R { diff --git a/packages/n4s/src/exports/compose.ts b/packages/n4s/src/exports/compose.ts index b5013332c..f2d7c7b8e 100644 --- a/packages/n4s/src/exports/compose.ts +++ b/packages/n4s/src/exports/compose.ts @@ -1,9 +1,8 @@ +import invariant from 'invariant'; import mapFirst from 'mapFirst'; -import throwError from 'throwError'; +import { ctx } from 'n4s'; import type { TComposeResult, TLazyRuleRunners } from 'genEnforceLazy'; -import { isEmpty } from 'isEmpty'; -import { ctx } from 'n4s'; import { defaultToPassing, TRuleDetailedResult } from 'ruleReturn'; import runLazyRule from 'runLazyRule'; @@ -16,14 +15,7 @@ export default function compose( (value: any) => { const res = run(value); - if (!res.pass) { - if (isEmpty(res.message)) { - throwError(); - } else { - // Explicitly throw a string so that vest.test can pick it up as the validation error message - throw res.message; - } - } + invariant(res.pass, new String(res.message)); }, { run, diff --git a/packages/n4s/src/lib/transformResult.ts b/packages/n4s/src/lib/transformResult.ts index 440c4b749..7cf1a1fcf 100644 --- a/packages/n4s/src/lib/transformResult.ts +++ b/packages/n4s/src/lib/transformResult.ts @@ -1,6 +1,6 @@ +import invariant from 'invariant'; import { isBoolean } from 'isBooleanValue'; import optionalFunctionValue from 'optionalFunctionValue'; -import throwError from 'throwError'; import ruleReturn, { TRuleReturn, TRuleDetailedResult } from 'ruleReturn'; import type { TRuleValue, TArgs } from 'runtimeRules'; @@ -29,9 +29,8 @@ export function transformResult( function validateResult(result: TRuleReturn): void { // if result is boolean, or if result.pass is boolean - if (isBoolean(result) || (result && isBoolean(result.pass))) { - return; - } - - throwError('Incorrect return value for rule: ' + JSON.stringify(result)); + invariant( + isBoolean(result) || (result && isBoolean(result.pass)), + 'Incorrect return value for rule: ' + JSON.stringify(result) + ); } diff --git a/packages/n4s/src/runtime/enforceEager.ts b/packages/n4s/src/runtime/enforceEager.ts index a98cafb63..64f44cac2 100644 --- a/packages/n4s/src/runtime/enforceEager.ts +++ b/packages/n4s/src/runtime/enforceEager.ts @@ -1,4 +1,4 @@ -import throwError from 'throwError'; +import invariant from 'invariant'; import eachEnforceRule from 'eachEnforceRule'; import { ctx } from 'enforceContext'; @@ -45,16 +45,13 @@ export default function enforceEager(value: TRuleValue): IRules { ...args ); - if (!transformedResult.pass) { - if (isEmpty(transformedResult.message)) { - throwError( - `enforce/${ruleName} failed with ${JSON.stringify(value)}` - ); - } else { - // Explicitly throw a string so that vest.test can pick it up as the validation error message - throw transformedResult.message; - } - } + invariant( + transformedResult.pass, + isEmpty(transformedResult.message) + ? `enforce/${ruleName} failed with ${JSON.stringify(value)}` + : new String(transformedResult.message) + ); + return target; }; } diff --git a/packages/shared/src/__tests__/throwError.test.ts b/packages/shared/src/__tests__/throwError.test.ts deleted file mode 100644 index 31f0ba99f..000000000 --- a/packages/shared/src/__tests__/throwError.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import throwError from 'throwError'; - -const message = 'message string'; - -const dev = global.__DEV__; - -describe('throwError', () => { - afterEach(() => { - // reset dev mode after each test - global.__DEV__ = dev; - }); - - it('Should throw with passed message', () => { - expect(() => throwError(message)).toThrow(message); - }); - - describe('When production message passed in dev mode', () => { - it('Should throw with dev message', () => { - global.__DEV__ = true; - expect(() => throwError('dev message!', 'production message!')).toThrow( - 'dev message' - ); - }); - }); - - describe('When production message passed in prod mode', () => { - it('Should throw with prod message', () => { - global.__DEV__ = false; - expect(() => throwError('dev message!', 'production message!')).toThrow( - 'production message!' - ); - }); - }); - - describe('When no production message passed in prod mode', () => { - it('Should throw with dev message', () => { - global.__DEV__ = false; - expect(() => throwError('dev message!')).toThrow('dev message!'); - }); - }); -}); diff --git a/packages/shared/src/invariant.ts b/packages/shared/src/invariant.ts new file mode 100644 index 000000000..17cdb3783 --- /dev/null +++ b/packages/shared/src/invariant.ts @@ -0,0 +1,19 @@ +import optionalFunctionValue from 'optionalFunctionValue'; +import { TStringable } from 'utilityTypes'; + +export default function invariant( + condition: any, + // eslint-disable-next-line @typescript-eslint/ban-types + message?: String | TStringable +): asserts condition { + if (condition) { + return; + } + + // If message is a string object (rather than string literal) + // Throw the value directly as a string + // Alternatively, throw an error with the message + throw message instanceof String + ? message.valueOf() + : new Error(message ? optionalFunctionValue(message) : message); +} diff --git a/packages/shared/src/throwError.ts b/packages/shared/src/throwError.ts index 4dcb8db40..72e28dc3b 100644 --- a/packages/shared/src/throwError.ts +++ b/packages/shared/src/throwError.ts @@ -1,22 +1,5 @@ -import defaultTo from 'defaultTo'; - -/** - * Throws a timed out error. - */ -export default function throwError( - devMessage?: string, - productionMessage?: string -): never { - throw new Error( - __DEV__ ? devMessage : defaultTo(productionMessage, devMessage) - ); -} - -export function throwErrorDeferred( - devMessage?: string, - productionMessage?: string -): void { +export function deferThrow(message?: string): void { setTimeout(() => { - throwError(devMessage, productionMessage); + throw new Error(message); }, 0); } diff --git a/packages/vest/src/__tests__/isolate.test.ts b/packages/vest/src/__tests__/isolate.test.ts index 43457229d..06214d6c8 100644 --- a/packages/vest/src/__tests__/isolate.test.ts +++ b/packages/vest/src/__tests__/isolate.test.ts @@ -5,12 +5,12 @@ import { IsolateTypes } from 'IsolateTypes'; describe('isolate', () => { let firstRun = true; let vest, isolate, skipWhen, dummyTest; - let throwErrorDeferred; + let deferThrow; beforeEach(() => { firstRun = true; const mock = mockThrowError(); - throwErrorDeferred = mock.throwErrorDeferred; + deferThrow = mock.deferThrow; vest = mock.vest; skipWhen = vest.skipWhen; isolate = require('isolate').isolate; @@ -240,10 +240,10 @@ describe('isolate', () => { }); suite(); - expect(throwErrorDeferred).toHaveBeenCalledTimes(0); + expect(deferThrow).toHaveBeenCalledTimes(0); suite(); - expect(throwErrorDeferred).toHaveBeenCalledTimes(1); - expect(throwErrorDeferred).toHaveBeenCalledWith( + expect(deferThrow).toHaveBeenCalledTimes(1); + expect(deferThrow).toHaveBeenCalledWith( expect.stringContaining( 'Vest Critical Error: Tests called in different order than previous run' ) @@ -258,9 +258,9 @@ describe('isolate', () => { }); suite(); - expect(throwErrorDeferred).toHaveBeenCalledTimes(0); + expect(deferThrow).toHaveBeenCalledTimes(0); suite(); - expect(throwErrorDeferred).toHaveBeenCalledTimes(0); + expect(deferThrow).toHaveBeenCalledTimes(0); }); }); }); diff --git a/packages/vest/src/core/isolate/isolates/each.ts b/packages/vest/src/core/isolate/isolates/each.ts index 862d782e4..f8ab7dd7b 100644 --- a/packages/vest/src/core/isolate/isolates/each.ts +++ b/packages/vest/src/core/isolate/isolates/each.ts @@ -1,5 +1,5 @@ +import invariant from 'invariant'; import isFunction from 'isFunction'; -import throwError from 'throwError'; import { IsolateTypes } from 'IsolateTypes'; import { isolate } from 'isolate'; @@ -21,9 +21,7 @@ export default function each( list: T[], callback: (arg: T, index: number) => void ): void { - if (!isFunction(callback)) { - throwError('each callback must be a function'); - } + invariant(isFunction(callback), 'each callback must be a function'); isolate({ type: IsolateTypes.EACH }, () => { list.forEach((arg, index) => { diff --git a/packages/vest/src/core/isolate/isolates/group.ts b/packages/vest/src/core/isolate/isolates/group.ts index 8c4862fc0..ef47c4ee1 100644 --- a/packages/vest/src/core/isolate/isolates/group.ts +++ b/packages/vest/src/core/isolate/isolates/group.ts @@ -1,6 +1,6 @@ +import invariant from 'invariant'; import isFunction from 'isFunction'; import { isStringValue } from 'isStringValue'; -import throwError from 'throwError'; import { IsolateTypes } from 'IsolateTypes'; import context from 'ctx'; @@ -16,13 +16,9 @@ import { isolate } from 'isolate'; * }); */ export default function group(groupName: string, tests: () => void): void { - if (!isStringValue(groupName)) { - throwGroupError('name must be a string'); - } + invariant(isStringValue(groupName), groupErrorMsg('name must be a string')); - if (!isFunction(tests)) { - throwGroupError('callback must be a function'); - } + invariant(isFunction(tests), groupErrorMsg('callback must be a function')); // Running with the context applied isolate({ type: IsolateTypes.GROUP }, () => { @@ -30,6 +26,6 @@ export default function group(groupName: string, tests: () => void): void { }); } -function throwGroupError(error: string) { - throwError(`Wrong arguments passed to group. Group ${error}.`); +function groupErrorMsg(error: string) { + return `Wrong arguments passed to group. Group ${error}.`; } diff --git a/packages/vest/src/core/suite/create.ts b/packages/vest/src/core/suite/create.ts index 9733b9bd5..8fa230919 100644 --- a/packages/vest/src/core/suite/create.ts +++ b/packages/vest/src/core/suite/create.ts @@ -1,7 +1,7 @@ import assign from 'assign'; import genId from 'genId'; +import invariant from 'invariant'; import isFunction from 'isFunction'; -import throwError from 'throwError'; import { createState } from 'vast'; import { IsolateTypes } from 'IsolateTypes'; @@ -47,9 +47,10 @@ function create( ): SuiteReturnType { const [suiteCallback, suiteName] = args.reverse() as [T, string]; - if (!isFunction(suiteCallback)) { - throwError('vest.create: Expected callback to be a function.'); - } + invariant( + isFunction(suiteCallback), + 'vest.create: Expected callback to be a function.' + ); // Event bus initialization const bus = initBus(); diff --git a/packages/vest/src/core/suite/produce/getFailures/getFailuresByGroup.ts b/packages/vest/src/core/suite/produce/getFailures/getFailuresByGroup.ts index 03bc169e4..9dc786701 100644 --- a/packages/vest/src/core/suite/produce/getFailures/getFailuresByGroup.ts +++ b/packages/vest/src/core/suite/produce/getFailures/getFailuresByGroup.ts @@ -1,4 +1,4 @@ -import throwError from 'throwError'; +import invariant from 'invariant'; import { Severity } from 'Severity'; import collectFailureMessages from 'collectFailureMessages'; @@ -41,13 +41,12 @@ function getByGroup( group: string, fieldName?: string ): Record { - if (!group) { - throwError( - `get${severityKey[0].toUpperCase()}${severityKey.slice( - 1 - )}ByGroup requires a group name. Received \`${group}\` instead.` - ); - } + invariant( + group, + `get${severityKey[0].toUpperCase()}${severityKey.slice( + 1 + )}ByGroup requires a group name. Received \`${group}\` instead.` + ); const testObjects = useTestsFlat(); return collectFailureMessages(severityKey, testObjects, { group, diff --git a/packages/vest/src/core/test/__tests__/key.test.ts b/packages/vest/src/core/test/__tests__/key.test.ts index 10b2e0705..65278a7fb 100644 --- a/packages/vest/src/core/test/__tests__/key.test.ts +++ b/packages/vest/src/core/test/__tests__/key.test.ts @@ -147,11 +147,11 @@ describe('key', () => { }); describe('When the same key is encountered twice', () => { - let throwErrorDeferred, vest; + let deferThrow, vest; beforeEach(() => { const mock = mockThrowError(); - throwErrorDeferred = mock.throwErrorDeferred; + deferThrow = mock.deferThrow; vest = mock.vest; }); @@ -166,7 +166,7 @@ describe('key', () => { vest.test('field2', () => false, 'key_1'); }); suite(); - expect(throwErrorDeferred).toHaveBeenCalledWith( + expect(deferThrow).toHaveBeenCalledWith( `Encountered the same test key "key_1" twice. This may lead to tests overriding each other's results, or to tests being unexpectedly omitted.` ); }); diff --git a/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts b/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts index 19ddcde15..bc79a65da 100644 --- a/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts +++ b/packages/vest/src/core/test/__tests__/merging_of_previous_test_runs.test.ts @@ -72,10 +72,10 @@ describe('Merging of previous test runs', () => { }); describe('When tests are passed in a different order between runs', () => { - let throwErrorDeferred, vest; + let deferThrow, vest; beforeEach(() => { const mock = mockThrowError(); - throwErrorDeferred = mock.throwErrorDeferred; + deferThrow = mock.deferThrow; vest = mock.vest; }); @@ -93,11 +93,11 @@ describe('Merging of previous test runs', () => { }); suite(); - expect(throwErrorDeferred).not.toHaveBeenCalled(); + expect(deferThrow).not.toHaveBeenCalled(); suite(); - expect(throwErrorDeferred).toHaveBeenCalledWith( + expect(deferThrow).toHaveBeenCalledWith( expect.stringContaining( 'Vest Critical Error: Tests called in different order than previous run.' ) @@ -142,7 +142,7 @@ describe('Merging of previous test runs', () => { `); const resB = suite(); - expect(resB.tests.f2).not.toBeDefined(); + expect(resB.tests.f2).toBeUndefined(); expect(resB.hasErrors('f1')).toBe(true); expect(resB.hasErrors('f2')).toBe(false); expect(resB.hasErrors('f3')).toBe(true); @@ -234,11 +234,11 @@ describe('Merging of previous test runs', () => { } `); const resB = suite(); - expect(resB.tests.f2).not.toBeDefined(); - expect(resB.tests.f3).not.toBeDefined(); - expect(resB.tests.f5).not.toBeDefined(); - expect(resB.tests.f6).not.toBeDefined(); - expect(resB.tests.f7).not.toBeDefined(); + expect(resB.tests.f2).toBeUndefined(); + expect(resB.tests.f3).toBeUndefined(); + expect(resB.tests.f5).toBeUndefined(); + expect(resB.tests.f6).toBeUndefined(); + expect(resB.tests.f7).toBeUndefined(); expect(resB.hasErrors('f1')).toBe(true); expect(resB.hasErrors('f2')).toBe(false); expect(resB.hasErrors('f3')).toBe(false); diff --git a/packages/vest/src/core/test/key.ts b/packages/vest/src/core/test/key.ts index 6ea28fb4c..6118ceaf4 100644 --- a/packages/vest/src/core/test/key.ts +++ b/packages/vest/src/core/test/key.ts @@ -1,7 +1,7 @@ import asArray from 'asArray'; import { isNullish } from 'isNullish'; import * as nestedArray from 'nestedArray'; -import { throwErrorDeferred } from 'throwError'; +import { deferThrow } from 'throwError'; import VestTest from 'VestTest'; import ctx from 'ctx'; @@ -40,7 +40,7 @@ export function useRetainTestKey(key: string, testObject: VestTest) { if (isNullish(current[key])) { current[key] = testObject; } else { - throwErrorDeferred( + deferThrow( `Encountered the same test key "${key}" twice. This may lead to tests overriding each other's results, or to tests being unexpectedly omitted.` ); } diff --git a/packages/vest/src/core/test/lib/registerTest.ts b/packages/vest/src/core/test/lib/registerTest.ts index f50e5677b..c5ad8ca54 100644 --- a/packages/vest/src/core/test/lib/registerTest.ts +++ b/packages/vest/src/core/test/lib/registerTest.ts @@ -1,5 +1,4 @@ import isPromise from 'isPromise'; -import throwError from 'throwError'; import VestTest from 'VestTest'; import runAsyncTest from 'runAsyncTest'; @@ -27,7 +26,7 @@ export default function registerTest(testObject: VestTest): void { bus.emit(Events.TEST_COMPLETED, testObject); } } catch (e) { - throwError( + throw new Error( `Unexpected error encountered during test registration. Test Object: ${JSON.stringify(testObject)}. Error: ${e}.` diff --git a/packages/vest/src/core/test/lib/useTestAtCursor.ts b/packages/vest/src/core/test/lib/useTestAtCursor.ts index 87feb1352..54a0d411c 100644 --- a/packages/vest/src/core/test/lib/useTestAtCursor.ts +++ b/packages/vest/src/core/test/lib/useTestAtCursor.ts @@ -3,7 +3,7 @@ import { isEmpty, isNotEmpty } from 'isEmpty'; import { isNullish } from 'isNullish'; import * as nestedArray from 'nestedArray'; import type { NestedArray } from 'nestedArray'; -import { throwErrorDeferred } from 'throwError'; +import { deferThrow } from 'throwError'; import VestTest from 'VestTest'; import isSameProfileTest from 'isSameProfileTest'; @@ -42,7 +42,7 @@ export function useTestAtCursor(newTestObject: VestTest): VestTest { return nextTest; } - if (shouldPurgePrevTest(prevTest, newTestObject)) { + if (testReorderDetected(prevTest, newTestObject)) { throwTestOrderError(prevTest, newTestObject); removeAllNextTestsInIsolate(); @@ -85,7 +85,7 @@ function useGetTestAtCursor(tests: NestedArray): VestTest { return nestedArray.valueAtPath(tests, cursorPath) as VestTest; } -function shouldPurgePrevTest(prevTest: VestTest, newTest: VestTest): boolean { +function testReorderDetected(prevTest: VestTest, newTest: VestTest): boolean { return isNotEmpty(prevTest) && !isSameProfileTest(prevTest, newTest); } @@ -97,7 +97,7 @@ function throwTestOrderError( return; } - throwErrorDeferred(`Vest Critical Error: Tests called in different order than previous run. + deferThrow(`Vest Critical Error: Tests called in different order than previous run. expected: ${prevTest.fieldName} received: ${newTestObject.fieldName} This can happen on one of two reasons: diff --git a/packages/vest/src/core/test/test.ts b/packages/vest/src/core/test/test.ts index 3ca952dd9..8f80f097b 100644 --- a/packages/vest/src/core/test/test.ts +++ b/packages/vest/src/core/test/test.ts @@ -1,7 +1,7 @@ import assign from 'assign'; +import invariant from 'invariant'; import isFunction from 'isFunction'; -import { isNotString } from 'isString'; -import throwError from 'throwError'; +import { isString } from 'isString'; import VestTest, { TTestFn } from 'VestTest'; import ctx from 'ctx'; @@ -29,13 +29,15 @@ function testBase( isFunction(args[1]) ? args : [undefined, ...args] ) as [string | undefined, TTestFn, string | undefined]; - if (isNotString(fieldName)) { - throwIncompatibleParamsError('fieldName', 'string'); - } + invariant( + isString(fieldName), + incompatibleParamsError('fieldName', 'string') + ); - if (!isFunction(testFn)) { - throwIncompatibleParamsError('Test callback', 'function'); - } + invariant( + isFunction(testFn), + incompatibleParamsError('Test callback', 'function') + ); const context = ctx.useX(); const testObject = new VestTest(fieldName, testFn, { @@ -62,8 +64,6 @@ export const test = assign(testBase, { export type TTestBase = typeof testBase; -function throwIncompatibleParamsError(name: string, expected: string) { - throwError( - `Incompatible params passed to test function. ${name} must be a ${expected}` - ); +function incompatibleParamsError(name: string, expected: string) { + return `Incompatible params passed to test function. ${name} must be a ${expected}`; } diff --git a/packages/vest/src/core/vestBus.ts b/packages/vest/src/core/vestBus.ts index 01cf3bc54..102449bc9 100644 --- a/packages/vest/src/core/vestBus.ts +++ b/packages/vest/src/core/vestBus.ts @@ -1,5 +1,5 @@ import { createBus } from 'bus'; -import throwError from 'throwError'; +import invariant from 'invariant'; import VestTest from 'VestTest'; import ctx from 'ctx'; @@ -68,9 +68,7 @@ export function initBus() { export function useBus() { const context = ctx.useX(); - if (!context.bus) { - throwError(); - } + invariant(context.bus); return context.bus; } diff --git a/packages/vest/src/exports/classnames.ts b/packages/vest/src/exports/classnames.ts index 42b9beb60..bb0f8fb8d 100644 --- a/packages/vest/src/exports/classnames.ts +++ b/packages/vest/src/exports/classnames.ts @@ -1,5 +1,5 @@ +import invariant from 'invariant'; import isFunction from 'isFunction'; -import throwError from 'throwError'; import { parse } from 'parser'; import type { SuiteResult } from 'produceSuiteResult'; @@ -12,11 +12,10 @@ export default function classnames( res: SuiteRunResult | SuiteResult, classes: TSupportedClasses = {} ): (fieldName: string) => string { - if (!res || !isFunction(res.hasErrors)) { - throwError( - "classnames: Expected first argument to be Vest's result object." - ); - } + invariant( + res && isFunction(res.hasErrors), + "classnames: Expected first argument to be Vest's result object." + ); const selectors = parse(res); diff --git a/packages/vest/src/exports/promisify.ts b/packages/vest/src/exports/promisify.ts index 57dc44b88..c1d732db3 100644 --- a/packages/vest/src/exports/promisify.ts +++ b/packages/vest/src/exports/promisify.ts @@ -1,5 +1,5 @@ +import invariant from 'invariant'; import isFunction from 'isFunction'; -import throwError from 'throwError'; import { SuiteResult } from 'produceSuiteResult'; import { SuiteRunResult } from 'produceSuiteRunResult'; @@ -7,9 +7,10 @@ import { SuiteRunResult } from 'produceSuiteRunResult'; const promisify = (validatorFn: (...args: any[]) => SuiteRunResult) => (...args: any[]): Promise => { - if (!isFunction(validatorFn)) { - throwError('promisify: Expected validatorFn to be a function.'); - } + invariant( + isFunction(validatorFn), + 'promisify: Expected validatorFn to be a function.' + ); return new Promise(resolve => validatorFn(...args).done(resolve)); }; diff --git a/packages/vest/src/hooks/warn.ts b/packages/vest/src/hooks/warn.ts index c989a7b51..c41227364 100644 --- a/packages/vest/src/hooks/warn.ts +++ b/packages/vest/src/hooks/warn.ts @@ -1,4 +1,4 @@ -import throwError from 'throwError'; +import invariant from 'invariant'; import context from 'ctx'; import { ERROR_HOOK_CALLED_OUTSIDE } from 'hookErrors'; @@ -13,9 +13,7 @@ const ERROR_OUTSIDE_OF_TEST = __DEV__ export default function warn(): void { const ctx = context.useX('warn ' + ERROR_HOOK_CALLED_OUTSIDE); - if (!ctx.currentTest) { - throwError(ERROR_OUTSIDE_OF_TEST); - } + invariant(ctx.currentTest, ERROR_OUTSIDE_OF_TEST); ctx.currentTest.warn(); } diff --git a/packages/vest/testUtils/mockThrowError.ts b/packages/vest/testUtils/mockThrowError.ts index 25081f690..55e7d846d 100644 --- a/packages/vest/testUtils/mockThrowError.ts +++ b/packages/vest/testUtils/mockThrowError.ts @@ -1,16 +1,13 @@ export default function mockThrowError() { - const throwErrorDeferred = jest.fn(); - const throwError = jest.fn(); + const deferThrow = jest.fn(); jest.resetModules(); jest.mock('throwError', () => ({ - throwErrorDeferred, - default: throwError, + deferThrow, })); const vest = require('vest'); return { - throwErrorDeferred, - throwError, + deferThrow, vest, }; } diff --git a/tsconfig.json b/tsconfig.json index 66a7f1a9e..d133947a4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -102,6 +102,7 @@ "genId": ["./packages/shared/src/genId.ts"], "globals.d": ["./packages/shared/src/globals.d.ts"], "hasOwnProperty": ["./packages/shared/src/hasOwnProperty.ts"], + "invariant": ["./packages/shared/src/invariant.ts"], "isBooleanValue": ["./packages/shared/src/isBooleanValue.ts"], "isFunction": ["./packages/shared/src/isFunction.ts"], "isNullish": ["./packages/shared/src/isNullish.ts"],