Skip to content

Improve t.throws output for mismatched constructor #1471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sholladay opened this issue Jul 26, 2017 · 3 comments
Closed

Improve t.throws output for mismatched constructor #1471

sholladay opened this issue Jul 26, 2017 · 3 comments

Comments

@sholladay
Copy link

Description

The output for a failed t.throws() assertion should be clearer when the actual and expected constructors do not match.

Currently, it simply says that there was an unexpected exception and shows the error object. It is not immediately clear at first glance that the constructor is what caused the assertion to fail.

Test Source

const foo = async () => {
    throw new TypeError('Uh oh');
};

test('foo() does stuff', async (t) => {
    await t.throws(foo(), RangeError);
});

Output

  Threw unexpected exception:

  TypeError {
    message: 'Uh oh',
  }

Environment

Node 8.1.4
npm 5.3.0
ava 0.21.0
darwin 16.6.0

@sholladay
Copy link
Author

A simple solution would be to replace this:

Threw unexpected exception:

... with something like:

Threw `TypeError` but expected `RangeError`:

@sindresorhus
Copy link
Member

I was first thinking a diff:

  Threw unexpected exception:

- RangeError {
+ TypeError {
    message: 'Uh oh',
  }

But I think your suggestion would be better.

@novemberborn
Copy link
Member

👍 on @sholladay's suggestion.

I suspect we still need to tackle #1047 (comment) first though.

@novemberborn novemberborn added this to the 1.0 milestone Oct 25, 2017
novemberborn added a commit that referenced this issue Feb 12, 2018
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.
novemberborn added a commit that referenced this issue Feb 13, 2018
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.
novemberborn added a commit that referenced this issue Feb 13, 2018
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.
novemberborn added a commit that referenced this issue Feb 13, 2018
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)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants