Gathering detailed insights and metrics for jest-localstorage-mock
Gathering detailed insights and metrics for jest-localstorage-mock
Gathering detailed insights and metrics for jest-localstorage-mock
Gathering detailed insights and metrics for jest-localstorage-mock
A module to mock window.localStorage and window.sessionStorage in Jest
npm install jest-localstorage-mock
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
BSD-3-Clause License
318 Stars
171 Commits
34 Forks
7 Watchers
2 Branches
12 Contributors
Updated on Jul 10, 2025
Latest Version
2.4.26
Package Id
jest-localstorage-mock@2.4.26
Unpacked Size
20.14 kB
Size
6.79 kB
File Count
10
NPM Version
6.14.16
Node Version
12.22.12
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Use this module with Jest to run web tests
that rely on localstorage
and / or sessionStorage
where you want a working
localStorage API with mocked functions.
This module has no runtime dependencies so your project won't pull in additional module dependencies by using this.
Note that with jest@24
and above this project potentially duplicating functionality.
This should only be installed as a development dependency (devDependencies
) as
it is only designed for testing. The module is transpiled via
babel to support the current active Node LTS
version (6.11.3).
yarn:
1yarn add --dev jest-localstorage-mock
npm:
1npm i --save-dev jest-localstorage-mock
The simplest setup is to use the module system, you may also choose to create a setup file if needed.
In your package.json
under the jest
configuration section
create a setupFiles
array and add jest-localstorage-mock
to the array. Also, ensure you have not enabled resetMocks
.
1{ 2 "jest": { 3 "resetMocks": false, 4 "setupFiles": ["jest-localstorage-mock"] 5 } 6}
If you already have a setupFiles
attribute you can also append
jest-localstorage-mock
to the array.
1{ 2 "jest": { 3 "resetMocks": false, 4 "setupFiles": ["./__setups__/other.js", "jest-localstorage-mock"] 5 } 6}
Alternatively you can create a new setup file which then requires this module or
add the require
statement to an existing setup file.
__setups__/localstorage.js
1import 'jest-localstorage-mock'; 2// or 3require('jest-localstorage-mock');
Add that file to your setupFiles
array:
1"jest": { 2 "setupFiles": [ 3 "./__setups__/localstorage.js" 4 ] 5}
For a create-react-app
project you can replace the
suggested mock
with this at the beginning of the existing src/setupTests.js
file:
1require('jest-localstorage-mock');
You must also override some of create-react-app's default jest configuration. You can do so in your package.json
:
1{ 2 "jest": { 3 "resetMocks": false 4 } 5}
For more information, see #125.
By including this in your Jest setup you'll allow tests that expect a
localStorage
and sessionStorage
object to continue to run. The module can
also allow you to use the mocks provided to check that your localStorage is
being used as expected.
The __STORE__
attribute of localStorage.__STORE__
or
sessionStorage.__STORE__
is made available for you to directly access the
storage object if needed.
Check that your localStorage
calls were made when they were supposed to.
1test('should save to localStorage', () => { 2 const KEY = 'foo', 3 VALUE = 'bar'; 4 dispatch(action.update(KEY, VALUE)); 5 expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE); 6 expect(localStorage.__STORE__[KEY]).toBe(VALUE); 7 expect(Object.keys(localStorage.__STORE__).length).toBe(1); 8});
Check that your sessionStorage
is empty, examples work with either
localStorage
or sessionStorage
.
1test('should have cleared the sessionStorage', () => { 2 dispatch(action.reset()); 3 expect(sessionStorage.clear).toHaveBeenCalledTimes(1); 4 expect(sessionStorage.__STORE__).toEqual({}); // check store values 5 expect(sessionStorage.length).toBe(0); // or check length 6});
Check that localStorage
calls were not made when they shouldn't have been.
1test('should not have saved to localStorage', () => { 2 const KEY = 'foo', 3 VALUE = 'bar'; 4 dispatch(action.notIdempotent(KEY, VALUE)); 5 expect(localStorage.setItem).not.toHaveBeenLastCalledWith(KEY, VALUE); 6 expect(Object.keys(localStorage.__STORE__).length).toBe(0); 7});
Reset your localStorage
data and mocks before each test to prevent leaking.
1beforeEach(() => { 2 // to fully reset the state between tests, clear the storage 3 localStorage.clear(); 4 // and reset all mocks 5 jest.clearAllMocks(); 6 7 // clearAllMocks will impact your other mocks too, so you can optionally reset individual mocks instead: 8 localStorage.setItem.mockClear(); 9}); 10 11test('should not impact the next test', () => { 12 const KEY = 'foo', 13 VALUE = 'bar'; 14 dispatch(action.update(KEY, VALUE)); 15 expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE); 16 expect(localStorage.__STORE__[KEY]).toBe(VALUE); 17 expect(Object.keys(localStorage.__STORE__).length).toBe(1); 18}); 19 20test('should not be impacted by the previous test', () => { 21 const KEY = 'baz', 22 VALUE = 'zab'; 23 dispatch(action.update(KEY, VALUE)); 24 expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE); 25 expect(localStorage.__STORE__[KEY]).toBe(VALUE); 26 expect(Object.keys(localStorage.__STORE__).length).toBe(1); 27});
See the contributing guide for details on how you can contribute.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 5/10 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-14
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More