Skip to content

Commit

Permalink
Add a serializer for jest.fn to jest-snapshot (#4668)
Browse files Browse the repository at this point in the history
* Add a serializer for jest.fn to jest-snapshot

* Add link to PR in changelog

* Don't depend on jest-mock

* Remove instances from the snapshots

They don't seem as useful as I though
  • Loading branch information
SimenB authored and cpojer committed Oct 11, 2017
1 parent 1c2dba6 commit afb4bf5
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* `[jest-runtime]` Add `module.loaded`, and make `module.require` not enumerable ([#4623](https://github.com/facebook/jest/pull/4623))
* `[jest-runtime]` Add `module.parent` ([#4614](https://github.com/facebook/jest/pull/4614))
* `[jest-runtime]` Support sourcemaps in transformers ([#3458](https://github.com/facebook/jest/pull/3458))
* `[jest-snapshot]` Add a serializer for `jest.fn` to allow a snapshot of a jest mock ([#4668](https://github.com/facebook/jest/pull/4668))
* `[jest-worker]` Initial version of parallel worker abstraction, say hello! ([#4497](https://github.com/facebook/jest/pull/4497))

### Chore & Maintenance
Expand Down
8 changes: 8 additions & 0 deletions docs/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ expect(mockFunc).toBeCalledWith(arg1, arg2);

// The last call to the mock function was called with the specified args
expect(mockFunc).lastCalledWith(arg1, arg2);

// All calls and the name of the mock is written as a snapshot
expect(mockFunc).toMatchSnapshot();
```

These matchers are really just sugar for common forms of inspecting the `.mock`
Expand All @@ -259,6 +262,11 @@ expect(mockFunc.mock.calls[mockFunc.mock.calls.length - 1]).toEqual(
// The first arg of the last call to the mock function was `42`
// (note that there is no sugar helper for this specific of an assertion)
expect(mockFunc.mock.calls[mockFunc.mock.calls.length - 1][0]).toBe(42);

// A snapshot will check that a mock was invoked the same number of times,
// in the same order, with the same arguments. It will also assert on the name.
expect(mockFunc.mock.calls).toEqual([[arg1, arg2]]);
expect(mockFunc.mock.getMockName()).toBe('a mock name');
```

For a complete list of matchers, check out the [reference docs](/jest/docs/en/expect.html).
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`empty with no calls mock 1`] = `
Object {
"calls": Array [],
"name": "jest.fn()",
}
`;

exports[`instantiated mock 1`] = `
Object {
"calls": Array [
Array [
Object {
"name": "some fine name",
},
],
],
"name": "jest.fn()",
}
`;

exports[`mock with calls 1`] = `
Object {
"calls": Array [
Array [],
Array [
Object {
"foo": "bar",
},
42,
],
],
"name": "jest.fn()",
}
`;

exports[`mock with name 1`] = `
Object {
"calls": Array [],
"name": "name of mock is nice",
}
`;
36 changes: 36 additions & 0 deletions packages/jest-snapshot/src/__tests__/mock_serializer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';

const mock = jest.fn();

afterEach(() => mock.mockReset());

test('empty with no calls mock', () => {
expect(mock).toMatchSnapshot();
});

test('instantiated mock', () => {
// eslint-disable-next-line no-new
new mock({name: 'some fine name'});

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

test('mock with calls', () => {
mock();
mock({foo: 'bar'}, 42);

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

test('mock with name', () => {
const mockWithName = jest.fn().mockName('name of mock is nice');

expect(mockWithName).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/__tests__/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const testPath = names => {
it('gets plugins', () => {
const {getSerializers} = require('../plugins');
const plugins = getSerializers();
expect(plugins.length).toBe(4);
expect(plugins).toHaveLength(5);
});

it('adds plugins from an empty array', () => testPath([]));
Expand Down
30 changes: 30 additions & 0 deletions packages/jest-snapshot/src/mock_serializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat';

export const serialize = (
val: any,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer,
): string => {
const mockObject = {
calls: val.mock.calls,
name: val.getMockName(),
};

return printer(mockObject, config, indentation, depth, refs);
};

export const test = (val: any) => val && !!val._isMockFunction;

export default ({serialize, test}: NewPlugin);
9 changes: 8 additions & 1 deletion packages/jest-snapshot/src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {Plugin} from 'types/PrettyFormat';

import prettyFormat from 'pretty-format';
import jestMockSerializer from './mock_serializer';

const {
DOMElement,
Expand All @@ -18,7 +19,13 @@ const {
ReactTestComponent,
} = prettyFormat.plugins;

let PLUGINS = [ReactTestComponent, ReactElement, DOMElement, Immutable];
let PLUGINS = [
ReactTestComponent,
ReactElement,
DOMElement,
Immutable,
jestMockSerializer,
];

// Prepend to list so the last added is the first tested.
export const addSerializer = (plugin: Plugin) => {
Expand Down

0 comments on commit afb4bf5

Please sign in to comment.