Skip to content

Commit

Permalink
Print jest mock fuctions as MockFunction in jest-snapshot (#4836)
Browse files Browse the repository at this point in the history
* Print jest mock fuctions as `MockFunction` in jest-snapshot

Fixes #4835

* less string concat
  • Loading branch information
SimenB authored and cpojer committed Nov 4, 2017
1 parent 317ac37 commit 68a2f54
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`instantiated mock 1`] = `
Object {
MockFunction {
"calls": Array [
Array [
Object {
Expand All @@ -21,7 +21,7 @@ Object {
`;

exports[`mock with calls 1`] = `
Object {
MockFunction {
"calls": Array [
Array [],
Array [
Expand All @@ -36,7 +36,7 @@ Object {
`;

exports[`mock with name 1`] = `
Object {
MockFunction {
"calls": Array [],
"name": "name of mock is nice",
}
Expand Down
24 changes: 19 additions & 5 deletions packages/jest-snapshot/src/mock_serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,26 @@ export const serialize = (
refs: Refs,
printer: Printer,
): string => {
const mockObject = {
calls: val.mock.calls,
name: val.getMockName(),
};
const indentationNext = indentation + config.indent;

return printer(mockObject, config, indentation, depth, refs);
return (
'MockFunction {\n' +
`${indentationNext}"calls": ${printer(
val.mock.calls,
config,
indentationNext,
depth,
refs,
)},\n` +
`${indentationNext}"name": ${printer(
val.getMockName(),
config,
indentationNext,
depth,
refs,
)},\n` +
'}'
);
};

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

0 comments on commit 68a2f54

Please sign in to comment.