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

isRejected not behaving as assert.throw #47

Closed
liebrand opened this issue Mar 26, 2014 · 8 comments
Closed

isRejected not behaving as assert.throw #47

liebrand opened this issue Mar 26, 2014 · 8 comments
Labels

Comments

@liebrand
Copy link

assert.throw accepts a string/regex to compare against the thrown error.message

eg:
assert.throw(function() {
throw new Error('error message');
}, 'this is not the error message', 'my msg');

fails with:
AssertionError: expected [Function] to throw error including 'this is not the error message' but got 'error message'

but:
var promise = new Promise(function(resolve, reject) {
reject(new Error('error message'));
});
return assert.isRejected(promise, 'this is not the error message', "optional msg");

does not fail

@domenic
Copy link
Collaborator

domenic commented Mar 27, 2014

The best way to set this issue to rest once and for all, I think, will be to expose Chai's exception-testing as a Chai util that plugins can use. That is, pulling out the guts of https://github.com/chaijs/chai/blob/master/lib/chai/core/assertions.js#L1029-L1144 into something like checkError(error, assertionArgs) that returns a few properties like these https://github.com/chaijs/chai/blob/master/lib/chai/core/assertions.js#L1136-L1140

/cc @logicalparadox do you agree this would be a valuable addition to Chai? I am not sure on the exact interface yet but the idea would be for plugins to be able to reuse Chai's logic. If you agree I'll try to find time to pull-request up such an extraction from Chai.

@logicalparadox
Copy link
Member

Yes, I like this idea a lot. Overall, the more tools we give plugins the better.

@vikki
Copy link

vikki commented Jun 11, 2015

Hi @domenic, @logicalparadox - I had a go at refactoring out checkError from assertions into its own utility. Its not quite finished (needs some more tests and improved docs on the checkError utility) but I wanted to see if you might take something like this, or if another approach would be better etc. etc. ?
Chai is at https://github.com/vikki/chai/tree/addCheckError and chai-as-promised at https://github.com/vikki/chai-as-promised/tree/useCheckError

@keithamus
Copy link
Member

@vikki you should submit this as a Pull Request to chai's repo, and we can get the ball rolling on implementing this proper 😄.

@domenic
Copy link
Collaborator

domenic commented Aug 5, 2016

I'm rolling up all bugs in the category "isRejected/rejectedWith doesn't behave like Chai's error rejectors" into #166.

@domenic domenic closed this as completed Aug 5, 2016
lddubeau added a commit to lddubeau/chai-as-promised that referenced this issue Sep 18, 2016
The problem is that expect().to.be.rejected/rejectedWith (and its
negation) does not mirror how expect().to.throw (and its negation)
behave.

Same issue with assert.isRejected in relation to assert.throws.
lddubeau added a commit to lddubeau/chai-as-promised that referenced this issue Sep 18, 2016
The problem with issue chaijs#47 is that chai-as-promised somtimes treats the
final string parameter as a custom error message. We want to test that
it does not do this anymore so we check to make sure that such string is
not used as a custom error message.
lddubeau added a commit to lddubeau/chai-as-promised that referenced this issue Sep 18, 2016
chai-as-promised's `.rejectedWith` and `assert.isRejected` now mirror
how Chai's `.throws` works. Fixes issue chaijs#47.
domenic pushed a commit that referenced this issue Sep 27, 2016
The problem is that expect().to.be.rejected/rejectedWith (and its negation) does not mirror how expect().to.throw (and its negation) behave.

Same issue with assert.isRejected in relation to assert.throws.
domenic pushed a commit that referenced this issue Sep 27, 2016
The problem with issue #47 is that chai-as-promised somtimes treats the final string parameter as a custom error message. We want to test that it does not do this anymore so we check to make sure that such string is not used as a custom error message.
domenic pushed a commit that referenced this issue Sep 27, 2016
chai-as-promised's `.rejectedWith` and `assert.isRejected` now mirror how Chai's `.throws` works. Fixes #47.
@JustinLivi
Copy link

Hi all! I'm excited about the direction that you're taking chai and chai-as-promised with breaking error checking out into a more modular system. I'm just wondering where the best place to track progress on this would be? I wasn't able to locate the appropriate issue in the chai.js repo. Would this be the best issue to subscribe to? Thanks!

@meeber
Copy link
Contributor

meeber commented Oct 3, 2016

@JustinLivi The relevant repo is here: https://github.com/chaijs/check-error

It'll be included in the upcoming Chai v4 alpha release which will be available via npm.

Right now the check-error module only contains helper functions used by Chai's throw assertion. In the future, it's possible more logic will be moved into it from Chai.

@JustinLivi
Copy link

@meeber Great! Thanks so much

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

No branches or pull requests

7 participants