Skip to content

Commit

Permalink
pretty-format should run plugins before serializing nested basic valu…
Browse files Browse the repository at this point in the history
…es (jestjs#3017)
  • Loading branch information
jamiebuilds authored and cpojer committed Feb 27, 2017
1 parent 40fd312 commit eb78dd2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions packages/pretty-format/src/__tests__/pretty-format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ describe('prettyFormat()', () => {
})).toEqual('1 - 2 - 3 - 4');
});

it('should call plugins on nested basic values', () => {
const val = {prop: 42};
expect(prettyFormat(val, {
plugins: [{
print(val, print) {
return '[called]';
},
test(val) {
return typeof val === 'string' || typeof val === 'number';
},
}],
})).toEqual('Object {\n [called]: [called],\n}');
});

it('prints objects with no constructor', () => {
expect(prettyFormat(Object.create(null))).toEqual('Object {}');
});
Expand Down
10 changes: 5 additions & 5 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,16 @@ function printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDep
}

function print(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors) {
const basic = printBasicValue(val, printFunctionName, escapeRegex);
if (basic) {
return basic;
}

const plugin = printPlugin(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors);
if (plugin) {
return plugin;
}

const basic = printBasicValue(val, printFunctionName, escapeRegex);
if (basic) {
return basic;
}

return printComplexValue(val, indent, prevIndent, spacing, edgeSpacing, refs, maxDepth, currentDepth, plugins, min, callToJSON, printFunctionName, escapeRegex, colors);
}

Expand Down

0 comments on commit eb78dd2

Please sign in to comment.