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

98
Views
React jest appropriate place to use beforeEach

So I am trying to setup my JEST test and want to set some local storage state before running all the tests. I have used it under server.js. Is that a good place to set, so that all tests are able to access the local storage mocked value?

//src/setupTests.js
import '@testing-library/jest-dom/extend-expect';
import {server} from './mocks/server';

beforeAll(() => server.listen())
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers())
// Clean up after the tests are finished.
afterAll(() => server.close())

Below is my server.js

import {setupServer} from "msw/node";
import {handlers} from './handlers'
import mockData from "./data/mockData";

// This configures a request mocking server with the given request handlers.
export const server = setupServer(...handlers);

beforeEach(() => {
    localStorage.setItem('mockData', JSON.stringify(mockData));
});
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!