Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

407
Views
React testing library-ReferenceError: Cannot access '' before initialization

I am mocking useNavigate to test my login page redirect works after login. But I got this reference error.

ReferenceError: Cannot access 'mockedUsedNavigate' before initialization

      12 | jest.mock('react-router-dom', () => ({
      13 |   ...jest.requireActual('react-router-dom'),
    > 14 |   useNavigate: () => mockedUsedNavigate,

I've looked through the forum and aware it might be the jest.mock() hoisted to the top but I did define mockedUsedNavigate before the mock.

// mock useNavigate
const mockedUsedNavigate = jest.fn()

jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useNavigate: () => mockedUsedNavigate, 
}))

code for my login page:

  const dispatch = useDispatch()
  const navigate = useNavigate()

  const handleSubmit = e => {
    e.preventDefault()
    if (isSignup) {
      dispatch(signup(formData, navigate))
    } else {
      dispatch(signin(formData, navigate))
    }
  }

However, the same test code works for my other project.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!