DEV Community

Darmawan Zulkifli
Darmawan Zulkifli

Posted on

Setup localStorage for testing React with jest

How to deal with localStorage in React testing using jest.

I have been added to setupTest.js

const localStorageMock = {
  getItem: jest.fn()
  setItem: jest.fn(),
  removeItem: jest.fn(),
  clear: jest.fn(),
};

global.localStorage = localStorageMock;

Here's example of my service to get cache:

export default class Cache {
   getCache = () => {

Top comments (0)