-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Description
🐛 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