Skip to content
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

t.throws fails with incorrect output for "actual" #182

Closed
yangmillstheory opened this issue Aug 23, 2015 · 2 comments
Closed

t.throws fails with incorrect output for "actual" #182

yangmillstheory opened this issue Aug 23, 2015 · 2 comments

Comments

@yangmillstheory
Copy link

Change the following test in test/throws.js as below:

function fn() {
    throw new TypeError('RegExp');
}

test('throws (RegExp match)', function (t) {
    t.throws(fn, /^RegExp$/);
    t.end();
});

This will fail, as the match is against the stringified TypeError, not just the message, as outlined here.

The TAP output fails with:

not ok test/throws.js ................................... 2/4
Command: "/usr/local/bin/node throws.js"
TAP version 13
ok 1 should throw
not ok 2 should throw

---
  operator: throws
expected:
  "'/^RegExp$/'":
actual:
  "{ [TypeError": |
RegExp] message: 'RegExp' }
...
ok 3 should throw
not ok 4 test/throws.js

---
  exit:    1
command: "/usr/local/bin/node throws.js"
  ...

The actual parameter, if we look in lib/test.js, is the raw Error object, but in the case of a RegExp match, I think it should be the stringified error.

Making the obvious change (making actual be String(caught && caught.error) in this case doesn't seem to work:

not ok test/throws.js ................................... 2/4
Command: "/usr/local/bin/node throws.js"
TAP version 13
ok 1 should throw
not ok 2 should throw

---
  operator: throws
expected:
  "'/^RegExp$/'":
actual:
  "'TypeError": RegExp'
  ...
  ok 3 should throw
not ok 4 test/throws.js

---
  exit:    1
command: "/usr/local/bin/node throws.js"
  ...

since the quotes aren't balanced. However, the test passes with:

test('throws (RegExp match)', function (t) {
    t.throws(fn, /^TypeError: RegExp$/);
    t.end();
});

I think the actual output should be the stringified error, which is an easy enough change, but why are the quotes unbalanced?

@ljharb
Copy link
Collaborator

ljharb commented Aug 23, 2015

That's a good question - and it seems like something worth fixing.

@ljharb
Copy link
Collaborator

ljharb commented Mar 6, 2016

@yangmillstheory .test stringifies its value, so it is already the stringified error :-) I added some tests to close #269 that illustrate how it works.

As for the unbalanced quotes - I think that's something specific to your machine - the output in your second code block has some weirdness too. What OS and node version are you running?

I'm going to close this for now, but we can reopen if there's something we need to do here.

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

2 participants