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

[jest-mock] Include tracked call results in serialized mock #6244

Merged
merged 3 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
([#6221](https://github.com/facebook/jest/pull/6221))
* `[expect]` Improve return matchers
([#6172](https://github.com/facebook/jest/pull/6172))
* `[jest-mock]` Include tracked call results in serialized mock
Copy link
Member

Choose a reason for hiding this comment

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

should probably be marked as a breaking change in hindsight - this will break people's snapshots if the have jest mocks in them

Choose a reason for hiding this comment

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

Just experienced it. This is definitely a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

PR welcome 🙂

([#6244](https://github.com/facebook/jest/pull/6244))

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ Object {
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
},
}
`;
Expand All @@ -35,6 +41,12 @@ exports[`mock with 1 calls in React element 1`] = `
"Mocking you!",
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
}
>
Expand All @@ -53,5 +65,38 @@ exports[`mock with 2 calls 1`] = `
42,
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
Object {
"isThrow": false,
"value": undefined,
},
],
}
`;

exports[`mock with 2 calls, 1 return, 1 throw 1`] = `
[MockFunction] {
"calls": Array [
Array [
2,
],
Array [
3,
],
],
"results": Array [
Object {
"isThrow": false,
"value": 4,
},
Object {
"isThrow": true,
"value": [Error: Error Message!],
},
],
}
`;
45 changes: 42 additions & 3 deletions packages/jest-snapshot/src/__tests__/mock_serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ test('mock with 0 calls and default name', () => {
expect(fn).toMatchSnapshot();
});

test('mock with 2 calls, 1 return, 1 throw', () => {
const fn = jest.fn(value => {
if (value % 2 === 0) {
return value * 2;
} else {
throw new Error('Error Message!');
}
});

fn(2);

try {
fn(3);
} catch (error) {
// ignore error
}

expect(fn).toMatchSnapshot();
});

test('mock with 0 calls and default name in React element', () => {
const fn = jest.fn();
const val = {
Expand Down Expand Up @@ -68,7 +88,7 @@ test('mock with 2 calls', () => {
});

test('indent option', () => {
const fn = jest.fn();
const fn = jest.fn(val => val);
fn({key: 'value'});
const expected = [
'[MockFunction] {',
Expand All @@ -79,15 +99,24 @@ test('indent option', () => {
'},',
'],',
'],',
'"results": Array [',
'Object {',
'"isThrow": false,',
'"value": Object {',
'"key": "value",',
'},',
'},',
'],',
'}',
].join('\n');
expect(prettyFormat(fn, {indent: 0, plugins: [plugin]})).toBe(expected);
});

test('min option', () => {
const fn = jest.fn();
const fn = jest.fn(val => val);
fn({key: 'value'});
const expected = '[MockFunction] {"calls": [[{"key": "value"}]]}';
const expected =
'[MockFunction] {"calls": [[{"key": "value"}]], "results": [{"isThrow": false, "value": {"key": "value"}}]}';
expect(prettyFormat(fn, {min: true, plugins: [plugin]})).toBe(expected);
});

Expand Down Expand Up @@ -119,16 +148,26 @@ test('maxDepth option', () => {
' [Object],', // ++depth === 4
' ],',
' ],',
' "results": Array [', // ++depth === 2
' Object {', // ++depth === 3
' "isThrow": false,',
' "value": undefined,',
' },',
' ],',
' },',
' "greaterThan1": Object {', // ++depth === 2
' "fn2": [MockFunction atDepth2] {',
' "calls": Array [', // ++depth === 3
' [Array],', // ++depth === 4
' ],',
' "results": Array [', // ++depth === 3
' [Object],', // ++depth === 4
' ],',
' },',
' "greaterThan2": Object {', // ++depth === 3
' "fn3": [MockFunction atDepth3] {',
' "calls": [Array],', // ++depth === 4
' "results": [Array],', // ++depth === 4
' },',
' },',
' },',
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-snapshot/src/mock_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export const serialize = (
indentationNext +
'"calls": ' +
printer(val.mock.calls, config, indentationNext, depth, refs) +
(config.min ? ', ' : ',') +
config.spacingOuter +
indentationNext +
'"results": ' +
printer(val.mock.results, config, indentationNext, depth, refs) +
(config.min ? '' : ',') +
config.spacingOuter +
indentation +
Expand Down