Skip to content

Commit 3c13c33

Browse files
authored
Reimplement t.throws() and t.notThrows() (#1704)
Remove `core-assert` dependency. When passed a function as the second argument, `t.throws()` now assumes its a constructor. This removes support for a validation function, which may or may not have worked. Refs #1047. `t.throws()` now fails if the exception is not an error. Fixes #1440. Regular expressions are now matched against the error message, not the result of casting the error to a string. Fixes #1445. Validate second argument to `t.throws()`. Refs #1676. Assertion failures now display how AVA arrived at the exception. Constructors are printed when the error is not a correct instance. Fixes #1471. Support expectation object in t.throws() Fixes #1047. Fixes #1676. A combination of the following expectations is supported: ```js t.throws(fn, {instanceOf: SyntaxError}) // err instanceof SyntaxError t.throws(fn, {name: 'SyntaxError'}) // err.name === 'SyntaxError' t.throws(fn, {is: expectedErrorInstance}) // err === expectedErrorInstance t.throws(fn, {message: 'expected error message'}) // err.message === 'expected error message' t.throws(fn, {message: /expected error message/}) // /expected error message/.test(err.message) ```
1 parent ac300c1 commit 3c13c33

File tree

9 files changed

+622
-486
lines changed

9 files changed

+622
-486
lines changed

index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ export interface ObservableLike {
22
subscribe(observer: (value: any) => void): void;
33
}
44

5-
export type ThrowsErrorValidator = (new (...args: Array<any>) => any) | RegExp | string | ((error: any) => boolean);
5+
export type Constructor = (new (...args: Array<any>) => any);
6+
7+
export type ThrowsExpectation = {
8+
instanceOf?: Constructor;
9+
is?: Error;
10+
message?: string | RegExp;
11+
name?: string;
12+
};
13+
14+
export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;
615

716
export interface SnapshotOptions {
817
id?: string;

index.js.flow

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ export interface ObservableLike {
77
subscribe(observer: (value: any) => void): void;
88
}
99

10-
export type ThrowsErrorValidator = Class<{constructor(...args: Array<any>): any}> | RegExp | string | ((error: any) => boolean);
10+
export type Constructor = Class<{constructor(...args: Array<any>): any}>;
11+
12+
export type ThrowsExpectation = {
13+
instanceOf?: Constructor;
14+
is?: Error;
15+
message?: string | RegExp;
16+
name?: string;
17+
};
18+
19+
export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;
1120

1221
export interface SnapshotOptions {
1322
id?: string;

0 commit comments

Comments
 (0)