From c53a10fc7bffb46b2578683305c157a0defd8228 Mon Sep 17 00:00:00 2001 From: David Zilburg Date: Thu, 16 Jan 2020 14:34:13 -0800 Subject: [PATCH 1/2] Fix pretty-format to respect displayName on forwardRef. Unlike React DevTools, Jest ignores a custom displayName set on forwardRef components. We should align serialization with DevTools, so change behavior. --- .../src/__tests__/ReactElement.test.ts | 52 +++++++++++++++++++ .../pretty-format/src/plugins/ReactElement.ts | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 packages/pretty-format/src/__tests__/ReactElement.test.ts diff --git a/packages/pretty-format/src/__tests__/ReactElement.test.ts b/packages/pretty-format/src/__tests__/ReactElement.test.ts new file mode 100644 index 000000000000..70e5c119910b --- /dev/null +++ b/packages/pretty-format/src/__tests__/ReactElement.test.ts @@ -0,0 +1,52 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import React from 'react'; +import setPrettyPrint from './setPrettyPrint'; +import prettyFormat from '..'; + +const {ReactElement} = prettyFormat.plugins; + +setPrettyPrint([ReactElement]); + +describe('ReactElement Plugin', () => { + let forwardRefComponent: { + (_props: any, _ref: any): any; + displayName?: string; + }; + + let forwardRefExample: ReturnType; + + beforeEach(() => { + forwardRefComponent = (_props, _ref) => null; + + forwardRefExample = React.forwardRef(forwardRefComponent); + + forwardRefExample.displayName = undefined; + }); + + test('serializes forwardRef without displayName', () => { + forwardRefExample = React.forwardRef((_props, _ref) => null); + expect(React.createElement(forwardRefExample)).toPrettyPrintTo( + '', + ); + }); + + test('serializes forwardRef with displayName', () => { + forwardRefExample.displayName = 'Display'; + expect(React.createElement(forwardRefExample)).toPrettyPrintTo( + '', + ); + }); + + test('serializes forwardRef component with displayName', () => { + forwardRefComponent.displayName = 'Display'; + expect(React.createElement(forwardRefExample)).toPrettyPrintTo( + '', + ); + }); +}); diff --git a/packages/pretty-format/src/plugins/ReactElement.ts b/packages/pretty-format/src/plugins/ReactElement.ts index 0a8bd5a8e6ea..eda1a992523d 100644 --- a/packages/pretty-format/src/plugins/ReactElement.ts +++ b/packages/pretty-format/src/plugins/ReactElement.ts @@ -53,6 +53,10 @@ const getType = (element: any) => { } if (ReactIs.isForwardRef(element)) { + if (type.displayName) { + return type.displayName; + } + const functionName = type.render.displayName || type.render.name || ''; return functionName !== '' From 5d27d347ea3cbcc84fc7cd898e4485ebf5b18d95 Mon Sep 17 00:00:00 2001 From: David Zilburg Date: Thu, 16 Jan 2020 15:52:52 -0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67dfabea13cc..0dfc955ce7c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ - `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867)) - `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206)) - `[jest-reporters]` Transform file paths into hyperlinks ([#8980](https://github.com/facebook/jest/pull/8980)) +- `[pretty-format]` Fix pretty-format to respect displayName on forwardRef ([#9422](https://github.com/facebook/jest/pull/9422)) ### Fixes