Jest: [New matcher request] toHaveBeenCalled{Last}MatchingSnapshot

Created on 6 Feb 2017  路  11Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
The matcher doesn't exist. 馃槶

The current snapshot testing is amazing, but I'd also like to be able to use it for checking stubs without going through extracting .mock.calls etc.

I started on a patch for this, but if you don't want the feature I don't want to waste time on it. If you _do_ want it, I can provide a PR for it. 馃槃

New API proposal

Most helpful comment

We've made a serializer for mocks in the next version of Jest, so just do expect(myMock).toMatchSnapshot().

See this for examples on how it looks: https://github.com/facebook/jest/blob/4883838c16471fd3447c5fea1ab261140d37f7c6/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap

Available in jest@test (release coming soonish - #4948)

All 11 comments

whoa this is a long name for a function!

Is that a :-1: or :+1:? :grinning:

Frankly I don't know, but I personally don't like such long names. Could you please provide a sample usage? It would be easier to think about this.

The name itself was just to match the existing ones, I'm sure it can be shortened 馃槃

Usage sample:

test('some component', () => {
  const someObject = { method: jest.fn() };

  someFunction(someObject);

  expect(someObject.method).toHaveBeenCalledWith({
    prop: 'foo',
    prop2: 'bar',
    baz: 'foobar',
  }, true);

  // or, if I know which invocation
  expect(someObject.method.mock.calls[0]).toMatchSnapshot();

  // or, if this feature is implemented
  expect(someObject.method).toHaveBeenCalledMatchingSnapshot();
});

The middle one is OK, but I have to extract the correct call if there are multiple. or just do expect(someObject.method.mock.calls).toMatchSnapshot();, but then I assert on every single call. Might be fine, though? Reaching into the mock object feels kinda dirty though, even though it's public API.

As an alternative to:
toHaveBeenCalledWith(jasmine.objectContaining({}))

We wrote the following extension:

toBeCalledWithSnapshot(received) {
  return require('jest-snapshot').toMatchSnapshot.call(this, received.mock.calls);
}

I don't think this is something we'd like to support inside of Jest directly at this point. Maybe you can build a snapshot extension library using expect.extend? :)

Will do! Thanks

@SimenB Would you mind providing a copy-pastable snippet or a link to a library?

(Seems fairly easy to piece together, but always nice to have something packaged if it's convenient for you).

We've made a serializer for mocks in the next version of Jest, so just do expect(myMock).toMatchSnapshot().

See this for examples on how it looks: https://github.com/facebook/jest/blob/4883838c16471fd3447c5fea1ab261140d37f7c6/packages/jest-snapshot/src/__tests__/__snapshots__/mock_serializer.test.js.snap

Available in jest@test (release coming soonish - #4948)

Awesome!

great thread! I've found what I wanted! expect(myMock).toMatchSnapshot()

Was this page helpful?
0 / 5 - 0 ratings