npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-local-storage

v1.1.24

Published

Mock localStorage for headless unit tests

Downloads

247,873

Readme

Build Status Code Coverage

mock-local-storage

Mock localStorage for headless unit tests

Inspired by StackOverflow answers and wrapped into npm package.

Moving Notice

This package has moved from the letsrock-today repository into KaiSforza's GitLab repository.

There is a copy of the code in Github, as well, but issues and pull requests will only be watched in Gitlab.

Motivation

Used to mock localStorage to run headless tests of cache implementation in terminal (ie. without browser).

Installation

npm install mock-local-storage --save-dev

Usage

Mocha

Require in Mocha, which will replace localStorage and sessionStorage on the global and window objects:

mocha --require mock-local-storage

If you are using jsdom-global, make sure it is required before mock-local-storage:

mocha --require jsdom-global --require mock-local-storage

Other testing frameworks

In a node environment you can mock the window.localStorage as follows:

global.window = {}
import 'mock-local-storage'
window.localStorage = global.localStorage

This is very useful when you want to run headless tests on code meant for the browser that use localStorage

You can even store this in a file that is reused across tests:

mock-localstorage.js

global.window = {}
import 'mock-local-storage'
window.localStorage = global.localStorage

using-localstorage.test.js

import './mock-localstorage'

// unit tests follow here

Extra

Besides mocking of conventional localStorage interface, this implementation provides a way for test code to register a callback to be invoked on item insertion. Mock implementation will invoke it when localStorage.setItem() is called (but not with localStorage[key] notation).

It can be used to emulate allocation errors, like this:

describe('test with mock localStorage', () => {
    afterEach(() => {
	localStorage.clear();
		// remove callback
	localStorage.itemInsertionCallback = null;
    });
    it('emulate quota exceeded error', () => {
	localStorage.length.should.equal(0);
		// register callback
	localStorage.itemInsertionCallback = (len) => {
	    if (len >= 5) {
		let err = new Error('Mock localStorage quota exceeded');
		err.code = 22;
		throw err;
	    }
	};
	let handled = false;
	try {
	    for (let i = 0; i < 10; ++i) {
		localStorage.setItem(i, i);
	    }
	} catch (e) {
	    if (e.code == 22) {
		// handle quota exceeded error
		handled = true;
	    }
	}
	handled.should.be.true;
	localStorage.length.should.equal(5);
    });
});

Caveats

There are some caveats with using index operator. Browser's localStorage works with strings and stringifyes objects stored via localStorage[key] notation, but this implementation does not.

localStorage.itemInsertionCallback won't be invoked with localStorage[key] notation.

Tests

npm install
npm test

Bugs, issues, MRs, participation, contribution

Please feel free to send us occusionall MRs along with unit tests, we'll merge them if they successfully build and pass unit tests. Consider to always provide unit tests, illustrating your problem, along with PR to avoid future regression.

License

MIT