A cross-browser mock of the Web Storage API (localStorage and sessionStorage) that includes full support for the storage event, enabling testing of event listeners in environments like Node.js or headless browsers. The current version is 2.1.0, released with stable maintenance. It differentiates from simpler mocks by providing a complete window-like mock object that dispatches storage events on changes, making it suitable for testing event-driven code. The package is lightweight, has no dependencies, and supports Node.js >=4. It is primarily used in unit testing and CI/CD pipelines to simulate browser storage behavior.
npm install storage-mockNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates creating a mocked window, adding a storage event listener, setting/getting/removing/clearing items, and removing the listener.
const mock = require('storage-mock'); beforeEach(() => { mock.localStorage.clear(); mock.sessionStorage.clear(); });Use Object.keys(mock.localStorage).length instead of setting length.
When testing, ensure event listeners do not assume same-origin/tab restrictions.
Wrap usage with the new window-like object: const mock = require('storage-mock'); mock.localStorage.setItem(...);Upgrade to version >=2.0.0: npm install storage-mock@latest
Use the mock: const mockedWindow = require('storage-mock'); const localStorage = mockedWindow.localStorage;Run: npm install storage-mock --save-dev
Upgrade Node.js to version >=4, or polyfill the events module.
No dependency data recorded yet.