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

Omit empty calls array in plugin for mock functions #4848

Merged
merged 3 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,43 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`mock with 0 calls and default name in React element 1`] = `
<button
onClick={[MockFunction]}
>
Mock me!
</button>
`;

exports[`instantiated mock 1`] = `
MockFunction {
"calls": Array [
Array [
Object {
"name": "some fine name",
},
exports[`mock with 1 calls and non-default name via new in object 1`] = `
Object {
"fn": [MockFunction MyConstructor]
calls: Array [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty weird

Array [
Object {
"name": "some fine name",
},
],
],
],
"name": "jest.fn()",
}
`;

exports[`mock with calls 1`] = `
MockFunction {
"calls": Array [
exports[`mock with 1 calls in React element 1`] = `
<button
onClick={
[MockFunction]
calls: Array [
Array [
"Mocking you!",
],
]
}
>
Mock me!
</button>
`;

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

exports[`mock with name 1`] = `
MockFunction {
"calls": Array [],
"name": "name of mock is nice",
}
]
`;
120 changes: 106 additions & 14 deletions packages/jest-snapshot/src/__tests__/mock_serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,122 @@
*/
'use strict';

const mock = jest.fn();
const prettyFormat = require('pretty-format');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import


afterEach(() => mock.mockReset());
const plugin = require('../mock_serializer');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import


test('empty with no calls mock', () => {
expect(mock).toMatchSnapshot();
test('mock with 0 calls and default name', () => {
const fn = jest.fn();
expect(fn).toMatchSnapshot();
});

test('instantiated mock', () => {
test('mock with 0 calls and default name in React element', () => {
const fn = jest.fn();
const val = {
$$typeof: Symbol.for('react.test.json'),
children: ['Mock me!'],
props: {
onClick: fn,
},
type: 'button',
};
expect(val).toMatchSnapshot();
});

test('mock with 1 calls and non-default name via new in object', () => {
const fn = jest.fn();
fn.mockName('MyConstructor');
// eslint-disable-next-line no-new
new mock({name: 'some fine name'});
new fn({name: 'some fine name'});
const val = {
fn,
};
expect(val).toMatchSnapshot();
});

expect(mock).toMatchSnapshot();
test('mock with 1 calls in React element', () => {
const fn = jest.fn();
fn('Mocking you!');
const val = {
$$typeof: Symbol.for('react.test.json'),
children: ['Mock me!'],
props: {
onClick: fn,
},
type: 'button',
};
expect(val).toMatchSnapshot();
});

test('mock with calls', () => {
mock();
mock({foo: 'bar'}, 42);
test('mock with 2 calls', () => {
const fn = jest.fn();
fn();
fn({foo: 'bar'}, 42);
expect(fn).toMatchSnapshot();
});

expect(mock).toMatchSnapshot();
test('indent option', () => {
const fn = jest.fn();
fn({key: 'value'});
const expected = [
'[MockFunction]',
'calls: Array [',
'Array [',
'Object {',
'"key": "value",',
'},',
'],',
']',
].join('\n');
expect(prettyFormat(fn, {indent: 0, plugins: [plugin]})).toBe(expected);
});

test('mock with name', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is what I referred to in my comment

const mockWithName = jest.fn().mockName('name of mock is nice');
test('min option', () => {
const fn = jest.fn();
fn({key: 'value'});
const expected = '[MockFunction] calls: [[{"key": "value"}]]';
expect(prettyFormat(fn, {min: true, plugins: [plugin]})).toBe(expected);
});

expect(mockWithName).toMatchSnapshot();
test('maxDepth option', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a mock function with a name to a toMatchSnapshot assertion? I can see it working in this test, but a separate assertion would be awesome!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See MyConstructor in mock with 1 calls and non-default name via new in object 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can see it working, but it would be sweet with a test of just the name, no calls or wrapping objects

const fn1 = jest.fn();
fn1.mockName('atDepth1');
fn1('primitive', {key: 'value'});
const fn2 = jest.fn();
fn2.mockName('atDepth2');
fn2('primitive', {key: 'value'});
const fn3 = jest.fn();
fn3.mockName('atDepth3');
fn3('primitive', {key: 'value'});
const val = {
fn1,
greaterThan1: {
fn2,
greaterThan2: {
fn3,
},
},
};
const expected = [
'Object {', // ++depth === 1
' "fn1": [MockFunction atDepth1]',
' calls: Array [', // ++depth === 2
' Array [', // ++depth === 3
' "primitive",',
' [Object],', // ++depth === 4
' ],',
' ],', // trailing comma after property of object
' "greaterThan1": Object {', // ++depth === 2
' "fn2": [MockFunction atDepth2]',
' calls: Array [', // ++depth === 3
' [Array],', // ++depth === 4
' ],', // trailing comma after property of object
' "greaterThan2": Object {', // ++depth === 3
' "fn3": [MockFunction atDepth3]',
' calls: [Array],', // ++depth === 4 trailing comma after property of object
' },',
' },',
'}',
].join('\n');
expect(prettyFormat(val, {maxDepth: 3, plugins: [plugin]})).toBe(expected);
});
33 changes: 14 additions & 19 deletions packages/jest-snapshot/src/mock_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,21 @@ export const serialize = (
refs: Refs,
printer: Printer,
): string => {
const indentationNext = indentation + config.indent;
// Serialize a non-default name, even if config.printFunctionName is false.
const name = val.getMockName();
const nameString = name === 'jest.fn()' ? '' : ' ' + name;

return (
'MockFunction {\n' +
`${indentationNext}"calls": ${printer(
val.mock.calls,
config,
indentationNext,
depth,
refs,
)},\n` +
`${indentationNext}"name": ${printer(
val.getMockName(),
config,
indentationNext,
depth,
refs,
)},\n` +
'}'
);
let callsString = '';
if (val.mock.calls.length !== 0) {
const indentationNext = indentation + config.indent;
callsString =
config.spacingInner +
indentationNext +
'calls: ' +
printer(val.mock.calls, config, indentationNext, depth, refs);
}

return '[MockFunction' + nameString + ']' + callsString;
};

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