Registry / testing / mock-storage

mock-storage

JSON →
library1.1.0jsnpmunverified

A lightweight in-memory mock implementation of the Web Storage API (Storage interface) for testing. Version 1.1.0. Fully compliant with TypeScript's Storage interface, uses ES2015 Proxy and Map. Ideal for unit testing localStorage/sessionStorage in Node.js or browser environments without side effects. Differentiates from alternatives like jest-localstorage-mock by being pure TypeScript with a simple API.

npm install mock-storage
INSTALL
IMPORT
SIG · MOCK-STORAGE
M
mock-storage
testingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mockStorage
import { mockStorage } from 'mock-storage'
const mockStorage = require('mock-storage')
Library is ESM-only. CommonJS require will fail.
Storage
import type { Storage } from 'mock-storage'
import { Storage } from 'mock-storage'
Storage is exported as a type only, not a value. Use type import to avoid runtime errors.
default import
import { mockStorage } from 'mock-storage'
import mockStorage from 'mock-storage'
No default export exists. Named import is required.

Demonstrates creating a mock storage instance, setting/getting items, using property access, TypeScript type, and initializing with data.

import { mockStorage } from 'mock-storage'; // Create a mock storage instance const storage = mockStorage(); // Set and get items storage.setItem('foo', 'bar'); console.log(storage.getItem('foo')); // 'bar' // Use property access (Proxy-based) storage.baz = 'qux'; console.log(storage.baz); // 'qux' // Works with TypeScript's Storage type const typedStorage: Storage = mockStorage(); // Optionally initialize with a Map const initialData = new Map<string, string>([['key', 'value']]); const preloadedStorage = mockStorage(initialData); console.log(preloadedStorage.getItem('key')); // 'value'
Debug
Known issues
gotchaProperty access (e.g., storage.foo) may not work if the environment lacks ES2015 Proxy support (e.g., older Node versions, some test runners).
fix
Use explicit setItem/getItem methods instead of property access, or ensure Proxy polyfill is available.
affects: >=1.0.0
gotchaThe library is ESM-only. Using require() will throw an error.
fix
Use import syntax or configure your test framework to support ESM (e.g., use --experimental-vm-modules in Node, or transform in Jest).
affects: >=1.0.0
gotchamockStorage() with no arguments creates an empty storage; passing a Map<string, string> overwrites any existing default data.
fix
To add to existing data, pass a Map containing the desired initial entries.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module
Using CommonJS require() on an ESM-only package.
fix
Replace require with import: import { mockStorage } from 'mock-storage'
TypeError: storage.foo is not a function
Trying to use property access on a storage instance that doesn't support Proxy (older Node).
fix
Use setItem/getItem instead: storage.setItem('foo', 'bar')
TypeError: Class extends value #<Object> is not a constructor or null
Importing Storage as a value instead of a type.
fix
Use type-only import: import type { Storage } from 'mock-storage'
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Amazon
1
Resources