question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Sep 30, 2018

@rickhanlonii @mattphillips thoughts on ^? You’re way better at APIs than I am

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" ......
Read more >
Expect · Jest
Expect. When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of...
Read more >
Jest Expect - w3resource
Your code will still work if you mix them up, but the error messages that you get on failing tests will look strange....
Read more >
How can I check if a function throws an error which contains a ...
1 Answer 1 · regular expression: error message matches the pattern · string: error message includes the substring · error object: error message...
Read more >
API Reference | Vitest
If the promise is rejected, the test will fail. ... { test('composed of non-numbers to throw error', () => { expect(() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found