Git Product home page Git Product logo

Comments (6)

JPStrydom avatar JPStrydom commented on June 1, 2024 5

It seems that upgrading to Jest 22.0.0 or beyond causes the issue

from jest-localstorage-mock.

th3fallen avatar th3fallen commented on June 1, 2024 2
jest.spyOn(Storage.prototype, 'setItem');
  jest.spyOn(Storage.prototype, 'getItem');
  jest.spyOn(Storage.prototype, 'removeItem');

Fixes it for me per
jestjs/jest#6858
and jestjs/jest#6798

from jest-localstorage-mock.

clarkbw avatar clarkbw commented on June 1, 2024 2

Seems like this is related
#80

from jest-localstorage-mock.

AlexandrLi avatar AlexandrLi commented on June 1, 2024 2

Hey, guys, I guess this issue could be closed due merge of #80. Btw, I don't see any release with this changes. Any comments on this?

from jest-localstorage-mock.

JPStrydom avatar JPStrydom commented on June 1, 2024

The following can be used as a work around:

class LocalStorageMock {
    constructor() {
        this.__STORE__ = {};
    }

    clear = jest
        .fn()
        .mockName('localStorage.clear')
        .mockImplementation(() => (this.__STORE__ = {}));

    getItem = jest
        .fn()
        .mockName('localStorage.getItem')
        .mockImplementation(key => this.__STORE__[key] || null);

    setItem = jest
        .fn()
        .mockName('localStorage.setItem')
        .mockImplementation((key, value) => (this.__STORE__[key] = value.toString()));

    removeItem = jest
        .fn()
        .mockName('localStorage.removeItem')
        .mockImplementation(key => delete this.__STORE__[key]);
}

Object.defineProperty(window, 'localStorage', {
    value: new LocalStorageMock()
});

beforeEach(() => {
    window.localStorage.clear();
    window.localStorage.clear.mockClear();
    window.localStorage.getItem.mockClear();
    window.localStorage.setItem.mockClear();
    window.localStorage.removeItem.mockClear();
});

Just add this code in your setup file 👍

Edit: This workaround only seems to work in a Windows environment

from jest-localstorage-mock.

JPStrydom avatar JPStrydom commented on June 1, 2024

I'll give it a try. But the we still can't access the mocked __STORE__ variable unfortunately.

from jest-localstorage-mock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.