Skip to content

Commit 056875b

Browse files
committed
fix: error throw tests;
fix: better throw already `new Error`;
1 parent 8c7f7f5 commit 056875b

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/__tests__/useMultiStateValidator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ describe('useMultiStateValidator', () => {
6868
});
6969

7070
it('should throw if states is not an object', () => {
71-
try {
71+
expect(() => {
7272
// @ts-ignore
73-
getHook(defaultStatesValidator, 123);
74-
} catch (err) {
75-
expect(err).toBeDefined();
76-
expect(err instanceof Error).toBe(true);
77-
expect(err.message).toBe('states expected to be an object or array, got number');
78-
}
73+
const [, hook] = getHook(defaultStatesValidator, 123);
74+
75+
if (hook.result.error) {
76+
throw hook.result.error;
77+
}
78+
}).toThrowError('states expected to be an object or array, got number');
7979
});
8080

8181
it('first returned element should represent current validity state', () => {

src/useMultiStateValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function useMultiStateValidator<
1717
S extends MultiStateValidatorStates = MultiStateValidatorStates
1818
>(states: S, validator: MultiStateValidator<V, S>, initialValidity: V = [undefined] as V): UseValidatorReturn<V> {
1919
if (typeof states !== 'object') {
20-
throw Error('states expected to be an object or array, got ' + typeof states);
20+
throw new Error('states expected to be an object or array, got ' + typeof states);
2121
}
2222

2323
const validatorFn = useRef(validator);

0 commit comments

Comments
 (0)