From eb78dd27c8f404318671242765ff4eed1d020cc9 Mon Sep 17 00:00:00 2001 From: james kyle Date: Mon, 27 Feb 2017 10:21:05 -0800 Subject: [PATCH] pretty-format should run plugins before serializing nested basic values (#3017) --- .../src/__tests__/pretty-format-test.js | 14 ++++++++++++++ packages/pretty-format/src/index.js | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/pretty-format/src/__tests__/pretty-format-test.js b/packages/pretty-format/src/__tests__/pretty-format-test.js index be76b40e6132..983220ed9f4a 100644 --- a/packages/pretty-format/src/__tests__/pretty-format-test.js +++ b/packages/pretty-format/src/__tests__/pretty-format-test.js @@ -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 {}'); }); diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index ddccae2772ec..74bbcb3e34da 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -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); }