Registry / testing / jasmine-local-storage

jasmine-local-storage

JSON →
library1.1.2jsnpmunverified

Mock localStorage in Jasmine 2.x tests. Version 1.1.2 is the latest stable, compatible with Jasmine >=2 and <5. It provides simple `mockLocalStorage()` and `unmockLocalStorage()` functions that replace the global localStorage with a spy-based mock, allowing assertions on calls like `setItem`, `getItem`, etc. Unlike full localStorage polyfills, this package integrates directly with Jasmine's spy framework, enabling call-count checks and clean state between tests. Low maintenance; last release 2021.

npm install jasmine-local-storage
INSTALL
IMPORT
SIG · JASMINE-LOCAL-STOR
J
jasmine-local-storage
testingjavascriptv1.1.2
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.

mockLocalStorage
import 'jasmine-local-storage'; // then use mockLocalStorage()
import { mockLocalStorage } from 'jasmine-local-storage';
Package adds functions globally; no named export.
unmockLocalStorage
import 'jasmine-local-storage'; // then use unmockLocalStorage()
const unmockLocalStorage = require('jasmine-local-storage').unmockLocalStorage;
Both functions are attached to global scope, not exported as module properties.
mockLocalStorage (CommonJS require)
require('jasmine-local-storage'); // then use mockLocalStorage()
const { mockLocalStorage } = require('jasmine-local-storage');
Same as above; no destructured exports.

Shows setup and basic usage: mock localStorage in beforeEach, unmock in afterEach, and verify spy calls.

// Install: npm install --save-dev jasmine-local-storage // In your jasmine helper file or spec: require('jasmine-local-storage'); describe('localStorage test', () => { beforeEach(() => { mockLocalStorage(); }); afterEach(() => { unmockLocalStorage(); }); it('should mock setItem and getItem', () => { localStorage.setItem('key', 'value'); expect(localStorage.getItem('key')).toBe('value'); expect(localStorage.setItem).toHaveBeenCalledWith('key', 'value'); }); it('should track clear calls', () => { localStorage.clear(); expect(localStorage.clear).toHaveBeenCalled(); }); });
Debug
Known issues
breakingJasmine peer dependency: Only supports Jasmine 2.x – 4.x (>=2 <5). Will fail with Jasmine 5+.
fix
Upgrade jasmine-local-storage if possible, or pin Jasmine version to 4.x.
affects: <1.0.0?
deprecatedLast updated in 2021; no active development. May not support modern Node.js or Jasmine 5.
fix
Consider alternatives like jest-localstorage-mock or manual spies.
affects: 0.x or 1.x?
gotchaRequires globally installed jasmine? Actually, it's a peer dependency, but the module adds functions to global scope via 'require' side-effect. Not a classic import.
fix
Ensure jasmine is installed as devDependency; use 'require' in helper before specs run.
affects: >=1.0.0
gotchamockLocalStorage() replaces localStorage with a spy; after unmockLocalStorage(), the real localStorage is restored. But if unmock is not called, other tests may leak.
fix
Always pair mockLocalStorage with unmockLocalStorage, e.g., in afterEach or afterAll.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: mockLocalStorage is not defined
Forgetting to require/import the package before use.
fix
Add 'require("jasmine-local-storage")' in jasmine helper or spec top-level.
Error: Jasmine version mismatch: expected >=2 <5, got 5.x.x
Jasmine version 5 installed but package does not support it.
fix
Downgrade Jasmine to 4.x or switch to a different mock library.
Cannot find module 'jasmine-local-storage'
Package not installed or not in node_modules.
fix
Run 'npm install --save-dev jasmine-local-storage' and ensure it's listed in devDependencies.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
jasminerequiredpeer dependency, must be installed with compatible version >=2 <5
Agent activity
13 hits · last 30 days
node
12
Resources
jasmine-local-storage — npm install jasmine-local-storage · libregistry