Skip to content

Used for mocking out a fully function localStorage

Notifications You must be signed in to change notification settings

Vital38Media/lsMock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"));

About

Used for mocking out a fully function localStorage

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%