From 056875ba535bd601f04e4365914064583aa52a07 Mon Sep 17 00:00:00 2001 From: xobotyi Date: Thu, 17 Oct 2019 10:47:44 +0300 Subject: [PATCH] fix: error throw tests; fix: better throw already `new Error`; --- src/__tests__/useMultiStateValidator.ts | 14 +++++++------- src/useMultiStateValidator.ts | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/__tests__/useMultiStateValidator.ts b/src/__tests__/useMultiStateValidator.ts index 392af53d1d..cf191ca697 100644 --- a/src/__tests__/useMultiStateValidator.ts +++ b/src/__tests__/useMultiStateValidator.ts @@ -68,14 +68,14 @@ describe('useMultiStateValidator', () => { }); it('should throw if states is not an object', () => { - try { + expect(() => { // @ts-ignore - getHook(defaultStatesValidator, 123); - } catch (err) { - expect(err).toBeDefined(); - expect(err instanceof Error).toBe(true); - expect(err.message).toBe('states expected to be an object or array, got number'); - } + const [, hook] = getHook(defaultStatesValidator, 123); + + if (hook.result.error) { + throw hook.result.error; + } + }).toThrowError('states expected to be an object or array, got number'); }); it('first returned element should represent current validity state', () => { diff --git a/src/useMultiStateValidator.ts b/src/useMultiStateValidator.ts index 19ed9f2108..29cb12b338 100644 --- a/src/useMultiStateValidator.ts +++ b/src/useMultiStateValidator.ts @@ -17,7 +17,7 @@ export function useMultiStateValidator< S extends MultiStateValidatorStates = MultiStateValidatorStates >(states: S, validator: MultiStateValidator, initialValidity: V = [undefined] as V): UseValidatorReturn { if (typeof states !== 'object') { - throw Error('states expected to be an object or array, got ' + typeof states); + throw new Error('states expected to be an object or array, got ' + typeof states); } const validatorFn = useRef(validator);