-
-
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
Asymmetric matchers expect.stringMatches and expect.stringContains throw Error and let (otherwise successful) tests fail #7055
Comments
Of course one could argue that it is bad practice to provide arguments with varying types to the same function. But I'd argue that it is not the test framework's responsibility to enforce strict type checking on function parameters. |
I think it makes sense to throw in the asymmetric matcher, but we could catch that here: https://github.com/facebook/jest/blob/728ca114c06daa1c980d2d114142c8d6d3dd0dfb/packages/expect/src/spy_matchers.js#L185-L187 |
Ouch, #5021 still fails like this with the changes in #5069 that supposedly fixed it. The original problem was calling I agree with @ryx about returning false: Before we make the change, we need to think if it also makes sense for inverse matchers. |
Forgot to include link to similar matcher in Jasmine which doesn’t throw, but now Jest does: |
For example, the following test throws an error now, but would pass according to proposed change: test('.not.stringContaining', () => {
const foo = jest.fn();
foo(42);
foo('I am a valid string');
expect(foo).toHaveBeenCalledWith(expect.not.stringContaining('valid'));
}); EDITED P.S. This test would pass with assertions for both:
|
@rickhanlonii @mattphillips thoughts on ^? You're way better at APIs than I am |
Is behavior of string asymmetric matchers when composed with function call and return matchers worth a new e2e-integration test? |
Unless you wanna assert that the code frame is correct (which might be a good idea?) unit tests should suffice |
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. |
🐛 Bug Report
The asymmetric string matching throws an error on non-string values, which leads to tests that throw and fail before they can succeed.
To Reproduce
Take this example:
It results in this error being thrown:
Expected behavior
I would expect the test to succeed because the second call matches the expectation. If I use
toHaveBeenLastCalled
in the previous code then the test is green, so the matching as such seems to be accurate.Link to repl or repo (highly encouraged)
I don't have a public link but the problem is related to this line: https://github.com/facebook/jest/blob/master/packages/expect/src/asymmetric_matchers.js#L221 ... In my opinion it is wrong to throw an Error here because it breaks the test in any case. Instead it should just return false and let the match fail for this single evaluation.
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: