-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
s should fail a test even if it's inside a try/catch.
#3917
Comments
For real world use cases see React repo that tests lots of behavior that is expected to throw but is also expected to issue console warnings, or not issue console warnings. |
The current behavior also cover up error like this one: it('should fail this too', () => {
function returnTrue() {
return false;
}
function callback() {
expect(returnTrue()).toBe(true);
if (false) {
throw new Error();
}
}
expect(callback).toThrow();
}); There are two errors here, but they cancel out each other so it looks like it passes but it should fail. |
Another case of bugs being covered up by this facebook/react#10331 |
we already have a state where keep all the information about our should we make it configureable, or should we always fail the test if any of the expect's throw? |
It might be nice to make it configurable as an upgrade path but I don't see why you'd expect anything else. |
I agree this needs fixing, I don't agree this needs a configuration. I'm fine with the breaking change here, it exposes bugs. |
I am facing the same problem with jest that expects are not working as they should inside try-catch blocks.
The jest test above oddly passes. |
cc @bvaughn |
Experiencing same behaviour on Below is a minimal working example of the unexpected behaviour (without). The test("It should FAIL, it reports PASS and logs error to console.", () => {
expect.assertions(1);
try {
expect(false).toEqual(true);
} catch (error) {
console.error('😭😭😭', error);
}
});
// Expect reports the below matcherResult
// matcherResult: {
// actual: false,
// expected: true,
// message: [Function],
// name: 'toEqual',
// pass: false } } Interestingly test("It should FAIL, it expects two assertions and it FAILS for receiving only one assertion.", () => {
try {
expect.assertions(2);
expect(true).toEqual(true);
} catch (error) {
console.error('😭😭😭', error);
}
}); Hopefully this will be helpful to someone. 😄 |
- due to jestjs/jest#3917, Jest assertions weren't working from within Stream event handlers - miscellaneous fixes
We also ran into this. As a workaround we added a separate global function https://gist.github.com/cvle/e4496b733889c137e9f19dddf861fdd4 You could also replace |
That's clever, thanks for sharing! |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Currently
expect
helpers throw if there's an error. This lets them be caught in various global error handlers. This makes it difficult to test environment that has expected thrown behavior or explicitly tests custom error handling behavior.Failing assertions should set a global flag that fails the test even if there's a try/catch involved.
For example, these tests should fail:
The text was updated successfully, but these errors were encountered: