-
-
Notifications
You must be signed in to change notification settings - Fork 702
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 does not work with new Errors #430
Comments
Looks like the docs explicitly mention that you can pass in new errors and expect them to work - so yes I'd say this is a bug. The Throw method is quite a big one, its in assertions.js#L1195-1310. Upon some inspection it looks like when you pass in a I think the solution would be to check if If you need any more help, please let me know and I'll be happy to assist. |
In fact, looking through the code, it might be worthwhile ditching the whole |
Maybe as well, add some documentation so that if people really want to test the error by value then they can do |
@keithamus I was looking into this issue, and I attempted to implement the change you suggested. I checked if However, I am getting this test failure:
I speculate that it's because the |
@jluchiji, this issue predates our efforts to pull out the error checking code into a new module, and so I'd tread carefully when implementing code here. Have a read of chai-as-promised/issues#47 and #470 and #457. I'd say for now, the best place to move the error assertions forward is in those areas - then we can take a look at behaviours like this. |
Removing the |
Any update on this issue? is there a workaround for tests like this? |
Same bug. |
See #551 |
i have a |
Hey @brutalcrozt. You would be better off asking in our chat forums, either on Gitter or Slack. Alternatively you could try raising an issue with sinon-chai - but I'd recommend making sure you have a reduced test case before filing an issue. |
@keithamus The behavior of checking for both the error and the message when they exist has been fixed, but we took the design decision of doing strict ( We did this in order to enable users to choose between both behaviors (strict comparisons and a comparison using the constructor and/or message). |
Same bug here. My code: 'use strict'
module.exports = (obj) => {
if(obj instanceof Object)
if(Object.isFrozen(obj)) return obj
else return Object.freeze(obj)
else throw new TypeError('You need to send an OBJECT!')
} test: describe('An Immutable', () => {
it(' cannot accept a value different than OBJECT', () => {
expect(require('./iammutable')(1)).to.throw(TypeError, 'You need to send an OBJECT!')
})
}) output:
|
Hi @suissa. What you're running into is something different, and not a bug. You need to pass a function to Working example: describe('An Immutable', () => {
it(' cannot accept a value different than OBJECT', () => {
expect(() => require('../lib/iammutable')(1)).to.throw(TypeError, 'You need to send an OBJECT!')
})
}) |
this is my test: |
@farkhandaAbbas |
It seems this is still an issue. This is the current logic for testing https://github.com/chaijs/chai/blob/main/test/expect.js#L3028
var specificError = new RangeError('boo');
var goodFn = function () { 1==1; }
, badFn = function () { throw new Error('testing'); }
, refErrFn = function () { throw new ReferenceError('hello'); }
, ickyErrFn = function () { throw new PoorlyConstructedError(); }
, specificErrFn = function () { throw specificError; }
, customErrFn = function() { throw new CustomError('foo'); }
, emptyErrFn = function () { throw new Error(); }
, emptyStringErrFn = function () { throw new Error(''); }; ... expect(specificErrFn).to.throw(specificError); However, if you change the code to use a different instance of the error thrown like so: var specificError = new RangeError('boo');
var specificError2 = new RangeError('boo'); ... expect(specificErrFn).to.throw(specificError2); the following error and diff are shown
|
You are comparing two entirely different error instances, which happens to hold the same message. But still, they are different error objects, therefore the tests with chai assertion won't pass. Why?
That's why when you actually compare the same objects, they do pass the test: it('should throw a RangeError when the end date is before the start date', function () {
var dateRange = {
start: '2015-04-01',
end: '2014-04-29',
};
var error = new RangeError('Invalid date range');
var test = function () {
throw error;
};
expect(test).to.throw(error);
}); And also, if you compare only the error messages, they will pass as well: it('should throw a RangeError when the end date is before the start date', function () {
var dateRange = {
start: '2015-04-01',
end: '2014-04-29',
};
var error = new RangeError('Invalid date range');
var test = function () {
throw error;
};
expect(test).to.throw(new RangeError('Invalid date range').message);
}); Which is the same as: it('should throw a RangeError when the end date is before the start date', function () {
var dateRange = {
start: '2015-04-01',
end: '2014-04-29',
};
var error = new RangeError('Invalid date range');
var test = function () {
throw error;
};
expect(test).to.throw('Invalid date range');
}); Why?
What I do is to simply compare the error's message. As long as it's the same, then it's ok. That's it. |
This is my test:
which fails with the message:
I couldn't find any explanations / solutions / workarounds for this, so I think it's a bug with the
expect(x).to.throw
assertion. As a redundancy check, I tried:and that passed. Any thoughts?
The text was updated successfully, but these errors were encountered: