lsmock

1.2.1 • Public • Published

lsMock

Simple library used to mock out localStorage with full functionality for use in test environments or other situations where localStorage is not supported.

Setup

Install:

npm i --save lsmock

Include:

const mock = require('lsmock');

or

import mock from 'lsmock';

Usage

For use in testing suites where localStorage isn't defined, you could use lsmock something like this:

const mock = require('lsmock');
window.localStorage = new mock();

Otherwise, localStorage can be defined in the local scope if desired:

let localStorage = new lsmock();

localStorage.setItem("foo", "bar");

const test = localStorage.getItem("foo");

As with the web API, you can also access elements directly:

const test = localStorage.foo;

In the case where your tests expect localStorage to be in some initial state, that can be set as well:

let localStorage = new lsmock({
	"foo": "bar",
	"hello": "world"
});

console.log(localStorage.getItem("hello"));

Readme

Keywords

none

Package Sidebar

Install

npm i lsmock

Weekly Downloads

10

Version

1.2.1

License

ISC

Unpacked Size

2.65 kB

Total Files

4

Last publish

Collaborators

  • keegandonley