Skip to content

Commit

Permalink
Reimplement t.throws() and t.notThrows() (#1704)
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
novemberborn authored Feb 13, 2018
1 parent ac300c1 commit 3c13c33
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 486 deletions.
11 changes: 10 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ export interface ObservableLike {
subscribe(observer: (value: any) => void): void;
}

export type ThrowsErrorValidator = (new (...args: Array<any>) => any) | RegExp | string | ((error: any) => boolean);
export type Constructor = (new (...args: Array<any>) => any);

export type ThrowsExpectation = {
instanceOf?: Constructor;
is?: Error;
message?: string | RegExp;
name?: string;
};

export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;

export interface SnapshotOptions {
id?: string;
Expand Down
11 changes: 10 additions & 1 deletion index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ export interface ObservableLike {
subscribe(observer: (value: any) => void): void;
}

export type ThrowsErrorValidator = Class<{constructor(...args: Array<any>): any}> | RegExp | string | ((error: any) => boolean);
export type Constructor = Class<{constructor(...args: Array<any>): any}>;

export type ThrowsExpectation = {
instanceOf?: Constructor;
is?: Error;
message?: string | RegExp;
name?: string;
};

export type ThrowsErrorValidator = Constructor | RegExp | string | ThrowsExpectation;

export interface SnapshotOptions {
id?: string;
Expand Down
Loading

0 comments on commit 3c13c33

Please sign in to comment.