Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-test-renderer: improve findByType() error message #17439

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) || 'Unknown'}"`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,4 +1022,14 @@ describe('ReactTestRenderer', () => {
expect(Scheduler).toFlushWithoutYielding();
ReactTestRenderer.create(<App />);
});

it('calling findByType() with an invalid component will fall back to "Unknown" for component name', () => {
const App = () => null;
const renderer = ReactTestRenderer.create(<App />);
const NonComponent = {};

expect(() => {
renderer.root.findByType(NonComponent);
}).toThrowError(`No instances found with node type: "Unknown"`);
});
});