-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Using jest with node assert got wrong error type. #7547
Comments
This is weird. Running the snippet above behaves differently in node and through jest. I added const assert = require('assert');
const err = new Error('error message');
try {
assert.ok(false, err);
} catch (e) {
console.log(e.stack);
} Jest:
Node:
@thymikee @rickhanlonii any ideas? |
IIRC we re-throw Node assert errors with custom AssertionError: https://github.com/facebook/jest/blob/master/packages/jest-circus/src/formatNodeAssertErrors.js |
Yeah, but that shouldn't happen when I catch it locally, unless we inject a fake |
This is a use case for us to consider when deciding on a solution for #4724 For more info about the error class see: https://nodejs.org/dist/latest-v10.x/docs/api/assert.html#assert_class_assert_assertionerror |
I confirm this behavior. This is a burden in one of our use cases as we use |
I find it quite worrying that when I am running the code in the test environment it is being handled differently than it would in production. I should be able to rely on the fact my code will run the same in both environments otherwise the tests can't really be trusted. |
Are there any news on this issue? I just ran into the same problem and I'd hate to write my own assert function just to work around a longstanding bug in Jest. |
Same thing here, I'm using the node's assert function to do some assertions unrelated to unit testing and this behaviour causes the custom Error class to be substituted with the AssertionError. Because of this, all custom fields get lost. |
Does anybody have a workaround for this problem ? (One that does not involve using a different assertion library in the base code) |
It took me 30 minutes of googling just to find this thread. Why would the library override default |
👍 Also keen on a solution here. |
Any news on that? |
same issue |
This is a fairly significant issue with our codebase as we use |
@maybesmurf if you are really doing this all over the project, you should consider creating your own custom matcher that will do this unwrapping for you |
posting in case anyone else stumbles on this, adding this mock to my test files fixes the issue but to other people's points it would be nice if this followed the NodeJS spec more closely. // normally we would not need to mock nodejs internals but in this case Jest globals are incorrect
// relevant github issue: https://github.com/jestjs/jest/issues/7547
jest.mock('assert', () => ({
__esModule: true,
default: (statement: unknown, err: Error) => {
if (!statement) {
throw err
}
}
})) |
Checking in on this thread I ended up completely abandoning trying to mock This is an unfortunate case where the easiest solution is to use a third party module instead of node built-ins due to how Jest decides to override node defaults. |
🐛 Bug Report
The assert module in nodejs will throw error when assertion fail. It will throw a AssertionError If the second parameter is a string. But if the second parameter is an instance of Error, it will throw it instead of AssertionError.
I've tried this code and got the expected error.
But in my test case, i got an AssertionError.
To Reproduce
You may run this test case, it will fail:
The failed error message is:
Expected behavior
Pass the test case. I know i could use something like
.toThrow(err.message)
to pass it, but i want to check the error type also.Link to repl or repo (highly encouraged)
Just run the case above. :)
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: