Test fails because "Invalid hook call."
See original GitHub issue💬 Questions and Help
Hello,
I’m facing an issue that I’m not able to solve with the following test:
import { screen } from '@testing-library/react';
import { getPage } from 'next-page-tester';
describe('Home page', () => {
it('renders without crashing', async () => {
const { render } = await getPage({
route: '/',
dotenvFile: '.env.local'
});
render();
expect(screen.getByRole('heading', { name: 'HomePage' })).toBeInTheDocument();
});
I got this error
Home page › renders without crashing
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
22 |
23 | export default function Popup(): JSX.Element {
> 24 | const [prefix, setPrefix] = useState<string>('');
with this component:
[...]
export default function Popup(): JSX.Element {
const [prefix, setPrefix] = useState<string>('');
[...]
Do you know I can solve this?
Thank you,
Issue Analytics
- State:
- Created 3 years ago
- Comments:23
Top Results From Across the Web
Test fails because "Invalid hook call." · Issue #147 - GitHub
Home page › renders without crashing Invalid hook call. Hooks can only be called inside of the body of a function component.
Read more >Invalid hook call while testing with jest - react native
and error which I'm getting is: Invalid hook call. Hooks can only be called inside of the body of a function component.
Read more >Invalid Hook Call Warning - React
There are three common reasons you might be seeing it: You might have mismatching versions of React and React DOM. You might be...
Read more >Fix Invalid Hook Call Warning in React Tests - Sinistra
Damn. "Invariant Violation: Hooks can only be called inside the body of a function component." But... you ARE! Hope that helps!
Read more >Solving React Hooks' invalid hook call warning - Rob Kendal
So what causes the invalid hook call warning? · You might have mismatching versions of React and React DOM. · You might be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’m running into a similar issue with emotion/theme-ui. I have the jest patch installed. Here is a stack trace - not sure how useful it is. The offending hook call is here: https://github.com/emotion-js/emotion/blob/b59084b7cbfeab11c2a986033952d481d0b9fcf4/packages/react/src/context.js#L39
For some reason, I was able to get around the error on v0.19.0 by passing
nonIsolatedModules: ["react"]in thegetPagecall. I have no idea why that works, since"react"is isolated by default by next-page-tester.After that fix, I ran into trouble similar to #142 - it seemed like emotion was getting confused about whether it was running on the server or the client after I put its packages in
nonIsolatedModules.Luckily, v0.20.0 seems to have solved that problem (thank you!). But the
nonIsolatedModulesoption was removed, reintroducing the “invalid hook call” error. I patched in something similar with patch-package and now my tests pass:Again, I have no idea why that fixes it and this might introduce bugs of its own, but maybe this is helpful in the search for a long-term fix.
Hi @massimeddu-sonic, we’re working on a fix. We’ll release soonish in the upcoming days.