Skip to content
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

Jest errors out when expecting a mock to be called with a string matcher #5021

Closed
mrm007 opened this issue Dec 6, 2017 · 2 comments · Fixed by #5069
Closed

Jest errors out when expecting a mock to be called with a string matcher #5021

mrm007 opened this issue Dec 6, 2017 · 2 comments · Fixed by #5069

Comments

@mrm007
Copy link

mrm007 commented Dec 6, 2017

Repository: https://repl.it/repls/PleasedSubmissiveAfricanbushviper

function doWork() {
  const dispatch = jest.fn();
  dispatch(null);
  dispatch({});
  dispatch('blah');
  dispatch(() => {});
  return dispatch;
}

test('passes with exact string', () => {
  const dispatch = doWork();
  expect(dispatch).toBeCalledWith('blah');
});

test('fails with matcher', () => {
  const dispatch = doWork();
  expect(dispatch).toBeCalledWith(expect.stringContaining('bla'));
});

Output:

FAIL  ./add-test.js
   fails with matcher

    TypeError: Cannot read property 'includes' of null
      
      at Object.<anonymous>.test (add-test.js:17:20)
          at Promise (<anonymous>)
      at Promise.resolve.then.el (../../usr/local/share/.config/yarn/global/node_modules/p-map/index.js:42:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:169:7)

  ✓ passes with exact string (6ms)
  ✕ fails with matcher (2ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total

Jest should skip the values which don't match and only fail if none match. In my example, the second test should pass because expect.stringMatching('bla') passes on the third call with 'blah'. Basically, the same behaviour as asserting with an exact value.

Tested with
Jest v20.0.4 node v7.4.0 linux/amd64 (repl.it)
Jest v21.2.1 node v8.9.1 macOS X 10.13.1 (locally)

@rogeliog
Copy link
Contributor

rogeliog commented Dec 12, 2017

@mrm007 thanks for reporting, I opened a PR for it 😄

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants