Error in `useUser` when running tests with react testing library
See original GitHub issueDescribe the problem
When testing a component that calls useUser
in a jest runner with react-testing-library, there’s an error about Warning: An update to null inside a test was not wrapped in act(...).
console.error
Warning: An update to null inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
at Object.<anonymous>.exports.default (/Users/tommyfritz/workspace/bugs/jest-auth0-nextjs-bug/node_modules/@auth0/nextjs-auth0/src/frontend/use-user.tsx:134:3)
What was the expected behavior?
There would be no error at all, clean run.
Reproduction
The bug can be seen by cloning the following repo and running yarn, yarn test. https://github.com/flapjackfritz/jest-auth0-nextjs-issue
Environment
version: 1.4.0 framework: jest, react-testing-library, Nextjs ARM64 OSX Node 16.1.0
- Version of this library used:
- Which framework are you using, if applicable:
- Other modules/plugins/libraries that might be involved:
- Any other relevant information you think would be useful:
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Testing an error thrown by a React component using testing ...
I have a codesandbox for this, the above code can be found in UserContext.js and UserContext.test.js . Note: Tests can be run in...
Read more >Error thrown when using react hooks · Issue #128 - GitHub
Description. Given a react component using hooks · Reason. As far as I understand this is because we are using the fireEvent from...
Read more >user-event v13 - Testing Library
This page describes user-event@13.5.0 . This version is no longer maintained. Please use user-event@14 instead, as it includes important bug ...
Read more >Modern React testing, part 3: Jest and React Testing Library
Learn how to test React components with Jest and React Testing Library, and how to apply testing best practices.
Read more >Testing & Error Handling (Jest, React Testing Library) - YouTube
Companies all over the world are using Next.js to build performant, scalable applications. In this video, we'll talk about...- Unit test vs ...
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
Great, thanks for your help @LunarzFR
@flapjackfritz - closing this, feel free to ping me if you’d like me to reopen
Also, check out the nextjs-auth0 sample app that uses react-testing-library https://github.com/auth0-samples/auth0-nextjs-samples/blob/main/Sample-01/tests/pages/profile.test.jsx#L9
Hello @flapjackfritz,
I had this problem few days ago, you have to wrap your component that uses the
useUser
hook with theUserContext
and pass your mocked values in the value prop.I hope this can help you !