Skip to content

t.throws() with regex not working as expected #1445

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
stevenvachon opened this issue Jul 7, 2017 · 1 comment
Closed

t.throws() with regex not working as expected #1445

stevenvachon opened this issue Jul 7, 2017 · 1 comment
Labels
bug current functionality does not work as desired help wanted scope:assertions

Comments

@stevenvachon
Copy link

stevenvachon commented Jul 7, 2017

This fails:

t.throws(() => { throw new TypeError("Invalid URL: http://") }, /^Invalid URL/)

... yet:

/^Invalid URL/.test("Invalid URL: http://")
//-> true

error can be an error constructor, error message, regex matched against the error message, or validation function.

@sindresorhus
Copy link
Member

sindresorhus commented Jul 10, 2017

Underneath we're using the Node.js core assert.throws method and it seems it uses error.toString() instead of error.message for the regex testing.

Yet another reason we should do our own implementation of throws().

assert.throws(() => {
  throw new Error('foobar');
}, /^foo/);
/*
Error: foobar
    at assert.throws (repl:1:29)
    at _tryBlock (assert.js:489:5)
    at _throws (assert.js:510:12)
    at Function.throws (assert.js:540:3)
    at repl:1:8
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at REPLServer.defaultEval (repl.js:239:29)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:433:10)
*/
assert.throws(() => {
  throw new Error('foobar');
}, /^Error: foo/);
//=> undefined

@sindresorhus sindresorhus added bug current functionality does not work as desired help wanted labels Jul 10, 2017
@sindresorhus sindresorhus changed the title throws() regex not working? t.throws() with regex not working as expected Jul 10, 2017
@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.
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
Labels
bug current functionality does not work as desired help wanted scope:assertions
Projects
None yet
Development

No branches or pull requests

3 participants