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

Expect to throw force string equality instead of inclusion #436

Closed
totty90 opened this issue Apr 30, 2015 · 7 comments
Closed

Expect to throw force string equality instead of inclusion #436

totty90 opened this issue Apr 30, 2015 · 7 comments

Comments

@totty90
Copy link

totty90 commented Apr 30, 2015

expect(fn).to.throw(Error, "test")

will pass even if the error thrown in like this new Error('test2'). I would expect to don't pass as "test" is not equal to "test2".

In your docs you say:

string inclusion test for the error's message.

So it passes if the string is included. Is there any way to change the behaviour of this?

@keithamus
Copy link
Member

@totty90 you can easily new up an error and achieve your desired result, something like this should work:

expect(fn).to.throw(new Error("test"));

Having said that - I do think our throw() assertion needs some work, see also #430. I'm going to devote some time to this soon, to try and change it for 3.0.0

@niftylettuce
Copy link

Same bug.

@niftylettuce
Copy link

See #551

@gurdiga
Copy link
Contributor

gurdiga commented Jan 10, 2016

@totty90 you can easily new up an error and achieve your desired result, something like this should work:

expect(fn).to.throw(new Error("test"));

It seems like this doesn’t work. =/

It looks like the expected and actual errors are compared strictly, and the result is that the assertion fails even if the error constructor and messages correspond. I have prepared a quick jsfiddle demo with chai v.3.4.1:

function throwingFunction() {
    throw new Error('A specific error message');
}

chai.expect(throwingFunction).to.throw(new Error('A specific error message'));

Check the browser console to find the assertion error message:

Uncaught AssertionError: expected [Function: throwingFunction] to throw 'Error: A specific error message' but 'Error: A specific error message' was thrown

I’m wondering if there I’m missing something. 8-|

@keithamus
Copy link
Member

Hey @gurdiga - yes I was actually wrong above, passing two error instances will check for referential equality. 4.0.0 will change this (among other things). But we have a few outstanding PRs that I want to get merged in before I cut a release.

@gurdiga
Copy link
Contributor

gurdiga commented Jan 10, 2016

Sounds like a plan! Thank you! 8-)

@lucasfcosta
Copy link
Member

Since we've got #683 merged, this has been solved.
Thanks everyone for your input 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants