next-page-tester: Test fails because "Invalid hook call."
💬 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,
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 23
Commits related to this issue
- Reproduce invalid hook call error with emotion https://github.com/toomuchdesign/next-page-tester/issues/147 — committed to fracture91/next-page-tester-hook-error by fracture91 3 years ago
- next-page-tester 0.21.0 - patch no longer necessary! https://github.com/toomuchdesign/next-page-tester/issues/147 — committed to fracture91/ahurle-dev by fracture91 3 years ago
- next-page-tester 0.21.0 - patch no longer necessary! https://github.com/toomuchdesign/next-page-tester/issues/147 — committed to fracture91/ahurle-dev by fracture91 3 years ago
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.
Thanks @fracture91 for setting up the repo. It’s one of the funniest bugs I’ve met 😃 I hope to get back to you soon(ish) with some good news.
Cheers!
v0.20.0 is on the launching pad and should solve at least one reason why
swrcan’t render.