Skip to content

Commit

Permalink
[jest-mock] Include tracked call results in serialized mock (#6244)
Browse files Browse the repository at this point in the history
* Add call results to serialized mocks.

* Update CHANGELOG
  • Loading branch information
UselessPickles authored and cpojer committed May 24, 2018
1 parent bbbffcf commit b7e8582
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
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
([#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

0 comments on commit b7e8582

Please sign in to comment.