Skip to content

Commit 053347e

Browse files
authored
react-test-renderer: improve findByType() error message (#17439)
* improve findByType error message * fix flow typing * 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.
1 parent 4ee592e commit 053347e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/react-test-renderer/src/ReactTestRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
ScopeComponent,
4343
} from 'shared/ReactWorkTags';
4444
import invariant from 'shared/invariant';
45+
import getComponentName from 'shared/getComponentName';
4546
import ReactVersion from 'shared/ReactVersion';
4647

4748
import {getPublicInstance} from './ReactTestHostConfig';
@@ -346,7 +347,7 @@ class ReactTestInstance {
346347
findByType(type: any): ReactTestInstance {
347348
return expectOne(
348349
this.findAllByType(type, {deep: false}),
349-
`with node type: "${type.displayName || type.name}"`,
350+
`with node type: "${getComponentName(type) || 'Unknown'}"`,
350351
);
351352
}
352353

packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.internal.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,14 @@ describe('ReactTestRenderer', () => {
10221022
expect(Scheduler).toFlushWithoutYielding();
10231023
ReactTestRenderer.create(<App />);
10241024
});
1025+
1026+
it('calling findByType() with an invalid component will fall back to "Unknown" for component name', () => {
1027+
const App = () => null;
1028+
const renderer = ReactTestRenderer.create(<App />);
1029+
const NonComponent = {};
1030+
1031+
expect(() => {
1032+
renderer.root.findByType(NonComponent);
1033+
}).toThrowError(`No instances found with node type: "Unknown"`);
1034+
});
10251035
});

0 commit comments

Comments
 (0)