jest-serializer-no-mock-function

Prevent jest serializing mock functions in snapshot testing.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
jest-serializer-no-mock-function
301.0.05 years ago5 years agoMinified + gzip package size for jest-serializer-no-mock-function in KB

Readme

Jest snapshot serializer that prevents serializing mocked functions
Upon upgrading to Jest 22 I've ran into snapshot size increase due to serialization of mocked functions. This pacakge makes it serialize to a normal function.

Install

Add the package as a dev-dependency:
# With npm
npm install --save-dev jest-serializer-no-mock-function

# With yarn
yarn add --dev jest-serializer-no-mock-function

Update package.json to let Jest know about the serializer:
"jest": {
  "snapshotSerializers": ["jest-serializer-no-mock-function"]
}

Example

test('Mock test', () => {
  const mock = jest.fn().mockName('myMock');

  mock('hello', { foo: 'bar' });

  expect(mock).toMatchSnapshot();
});

Output with:
exports[`Mock test 1`] = `[Function]`;

Output without:
exports[`Mock test 1`] = `
[MockFunction myMock] {
  "calls": Array [
    Array [
      "hello",
      Object {
        "foo": "bar",
      },
    ],
  ],
}
`;