From 47c1509d6102984e8e9aa811d17dda7b5c388581 Mon Sep 17 00:00:00 2001 From: hdineen Date: Sat, 23 Nov 2019 00:06:58 -0500 Subject: [PATCH 1/3] improve findByType error message --- packages/react-test-renderer/src/ReactTestRenderer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/react-test-renderer/src/ReactTestRenderer.js b/packages/react-test-renderer/src/ReactTestRenderer.js index 18a69c4202111..91bdbc106db0e 100644 --- a/packages/react-test-renderer/src/ReactTestRenderer.js +++ b/packages/react-test-renderer/src/ReactTestRenderer.js @@ -42,6 +42,7 @@ import { ScopeComponent, } from 'shared/ReactWorkTags'; import invariant from 'shared/invariant'; +import getComponentName from 'shared/getComponentName'; import ReactVersion from 'shared/ReactVersion'; import {getPublicInstance} from './ReactTestHostConfig'; @@ -346,7 +347,7 @@ class ReactTestInstance { findByType(type: any): ReactTestInstance { return expectOne( this.findAllByType(type, {deep: false}), - `with node type: "${type.displayName || type.name}"`, + `with node type: "${getComponentName(type)}"`, ); } From 74a3e77fc0d748eecf42818da383d937c98aa7e3 Mon Sep 17 00:00:00 2001 From: hdineen Date: Sat, 23 Nov 2019 00:44:09 -0500 Subject: [PATCH 2/3] fix flow typing --- packages/react-test-renderer/src/ReactTestRenderer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-test-renderer/src/ReactTestRenderer.js b/packages/react-test-renderer/src/ReactTestRenderer.js index 91bdbc106db0e..6cebfab39ce60 100644 --- a/packages/react-test-renderer/src/ReactTestRenderer.js +++ b/packages/react-test-renderer/src/ReactTestRenderer.js @@ -347,7 +347,7 @@ class ReactTestInstance { findByType(type: any): ReactTestInstance { return expectOne( this.findAllByType(type, {deep: false}), - `with node type: "${getComponentName(type)}"`, + `with node type: "${getComponentName(type) || 'Unknown'}"`, ); } From 72ff92a5d8fe6d2e3c86d37042e3db834d5a9977 Mon Sep 17 00:00:00 2001 From: hdineen Date: Fri, 28 Feb 2020 11:46:05 -0500 Subject: [PATCH 3/3] Adding a test for the "Unknown" branch when `getComponentName()` returns a falsy value. The error message in this case not the most descriptive but seems consistent with the `getComponentName(type) || 'Unknown'` pattern seen in multiple places in this code base. --- .../src/__tests__/ReactTestRenderer-test.internal.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js b/packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js index d9c8d5b1c5208..06f7131f2bd59 100644 --- a/packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js +++ b/packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js @@ -1022,4 +1022,14 @@ describe('ReactTestRenderer', () => { expect(Scheduler).toFlushWithoutYielding(); ReactTestRenderer.create(); }); + + it('calling findByType() with an invalid component will fall back to "Unknown" for component name', () => { + const App = () => null; + const renderer = ReactTestRenderer.create(); + const NonComponent = {}; + + expect(() => { + renderer.root.findByType(NonComponent); + }).toThrowError(`No instances found with node type: "Unknown"`); + }); });