Skip to content

Trying to detect host component names triggered the following error #1413

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

Closed
Sahand-Stefan opened this issue May 24, 2023 · 8 comments
Closed
Labels
question Further information is requested

Comments

@Sahand-Stefan
Copy link

Ask your Question

beforeEach(() => {
// Render the component with default props for each test
component = render(<LActButton {...defaultProps} />)
})

I am using the testing library version : ^12.1.2
and the react native : o.71.8

And i get the following message for rendering my component
Trying to detect host component names triggered the following error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly. Please check if you are using compatible versions of React Native and React Native Testing Library.

@Sahand-Stefan Sahand-Stefan added the question Further information is requested label May 24, 2023
@pierrezimmermannbam
Copy link
Collaborator

Hello @Sahand-Stefan! If you get this error message then it means that there has to be some problem with your test setup that prevents the library from working correctly. Based on the information you gave I can't see what the problem really is, could you provide a minimal repro? Or at least provide your jest config and jest setup files?

@mdjastrzebski
Copy link
Member

@Sahand-Stefan Pierre made good points about the setup issue, and the need to provide repro repository so that we can diagnose such issue.

I would add following two thoughts:

  1. Perhaps you are using jest-expo package to test your code using React Native Web. If so, more details here.
  2. [nit] Rendering components in beforeEach is generally considered a bad practice. More details here.

@Sahand-Stefan
Copy link
Author

Thanks guys
I solved this issue with mocking each related package or function locally in the test file
The jest config couldn't read the mocs from a public global file

@pierrezimmermannbam
Copy link
Collaborator

@najumasad can you please provide a repro or give the code for your jest.config.js and package.json files?

@mdjastrzebski
Copy link
Member

Perhaps it would be useful to use jest.requireActual inside detectHostComponentNames to support cases when user mocks react-native. @pierrezimmermannbam wdyt?

@pierrezimmermannbam
Copy link
Collaborator

Perhaps it would be useful to use jest.requireActual inside detectHostComponentNames to support cases when user mocks react-native. @pierrezimmermannbam wdyt?

I'm not sure it would make a difference. If we use host component names that do not match the mocks then test will fail no? And the error will be more difficult to debug because queries will fail whereas here we know that there is an issue with the test setup. Moreover I think we should recommend not mocking react-native manually and using the preset instead, but maybe that's not really clear from the docs right now? I'm looking at the installation guide as well as the readme and the preset is not directly mentioned, it's only included in a snippet in the Additional Jest matchers section, which may lead to think that it's optionnal?

@mdjastrzebski
Copy link
Member

@pierrezimmermannbam With #1425 PR things get slightly improved as users should be able to mock whole react-native package and use renderHook.

@mdjastrzebski
Copy link
Member

Added Troubleshooting Guide entry about following error:

"Trying to detect host component names triggered the following error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.":

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants