Skip to content

Asymmetric matchers expect.stringMatches and expect.stringContains throw Error and let (otherwise successful) tests fail #7055

@ryx

Description

@ryx

🐛 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions