question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in `useUser` when running tests with react testing library

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
adamjmcgrathcommented, Jun 18, 2021

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

2reactions
vharnycommented, Jun 17, 2021

Hello @flapjackfritz,

I had this problem few days ago, you have to wrap your component that uses the useUser hook with the UserContext and pass your mocked values in the value prop.

import { UserContext } from '@auth0/nextjs-auth0'

...

const checkSession = jest.fn()

it("Should display user name 'Valentin' when user is connected", async () => {
  render(
    <UserContext.Provider value={{ user: { name: 'Valentin' }, isLoading: false, checkSession }}>
      <NavBar />
    </UserContext.Provider>
  )
  await waitFor(() => {
    expect(screen.queryByText('Valentin')).toBeInTheDocument()
  })
})

I hope this can help you !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found