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

Invariant Violation when testing hook that uses npm-imported hook #60

Closed
abuinitski opened this issue May 3, 2019 · 3 comments
Closed
Labels
question Further information is requested

Comments

@abuinitski
Copy link

I'm trying to test my custom hook that is based on another hook imported from an npm package (useConnect).

So the call chain is: useMyCustomHook -> calling useConnect -> calling stock react hooks

When calling this in a React component in browser it works great.

When running inside renderHook test harness (with jest) it immediately throws an "Invariant Violation: Hooks can only be called inside the body of a function component". It is raised from inside useConnect when it tries to invoke it's first hook.

When I don't import useConnect and just copy-paste it's source code (so it's transformed by jest's babel rather than imported from a "production" build of the package) – again it works flawlessly.

What am I missing?

@abuinitski abuinitski added the question Further information is requested label May 3, 2019
@mpeyper
Copy link
Member

mpeyper commented May 4, 2019

Can you share a repo or make a codesandbox that replicates the issue? That warning is literally why this library exists.

A common cause for seeing this error when you don't expect it is if there are duplicate versions of react, which might explain why it works when you paste the source code into you project instead of when it's situated in node_modules. npm ls react might give you some clues here. This is also easy to do if you're using npm link for local installation or a tool like lerna for managing monorepos.

@abuinitski
Copy link
Author

@mpeyper thank you!

You are totally right, I had my other hook npm-linked to my development package, and seems like it had a different react version installed in it's node_modules.

Works like a charm now 🥇

Should I close the issue? It's a question of hating hanging tickets vs. stuff others could experience and should know.

@mpeyper
Copy link
Member

mpeyper commented May 4, 2019

Closing is fine.

@mpeyper mpeyper closed this as completed May 4, 2019
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

2 participants