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

116
Views
Should you mock modules within a beforeEach?

For example should you do something like:

(1)

beforeEach(() => {
  jest.mock('react-router-dom', () => ({
    ...(jest.requireActual('react-router-dom')),
    useLocation: () => ({
      pathName: '/',
      search: '?something=everything'
    })
  }));
})

or is importing react-router-dom like that for every test wasteful (it seems so... if you have hundreds of tests this must cause test slowdown right)?

For example instead you could do this:

(2)

let mockUseLocation;
jest.mock('react-router-dom', () => ({
  ...(jest.requireActual('react-router-dom')),
  useLocation: () => mockUseLocation
}));

beforeEach(() => {
  mockUseLocation = {
    pathName: '/',
    search: '?something=everything'
  }
})

Is there a best practice about something like this? Is my intuition correct that the second option is better than the first?

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!