Asymmetric matchers expect.stringMatches and expect.stringContains throw Error and let (otherwise successful) tests fail
See original GitHub issue🐛 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:
const foo = jest.fn();
foo(42);
foo('I am a valid string');
expect(foo).toHaveBeenCalledWith(expect.stringContaining('valid'));
It results in this error being thrown:
Actual is not a string
1 | foo('I am a valid string');
2 |
> 3 | expect(foo).toHaveBeenCalledWith(expect.stringContaining('valid'));
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:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Binaries:
Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.1.0 - ~/dev/webchannel-tracking-service/node_modules/.bin/npm
npmPackages:
jest: ^23.5.0 => 23.5.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
@rickhanlonii @mattphillips thoughts on ^? You’re way better at APIs than I am
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. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.