Skip to content

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

@mrm007

Description

@mrm007

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)

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