JSPM

  • Created
  • Published
  • Downloads 343250
  • Score
    100M100P100Q179748F
  • License ISC

Mock localstorage in your Jest tests

Package Exports

  • jest-localstorage-mock

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jest-localstorage-mock) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Use this module with Jest to run web tests that rely on localstorage and / or sessionStorage where you want a working localStorage API with mocked functions.

This module has no runtime dependencies so your project won't pull in additional module dependencies by using this.

npm npm Codecov Greenkeeper badge Twitter

Install

This should only be required as a dev dependency when your tests are running.

yarn:

yarn add --dev jest-localstorage-mock

npm:

npm i --save-dev jest-localstorage-mock

Setup

Module

In your package.json under the jest section add the module name to the setupFiles array. This is by far the simplest method for using this.

"jest": {
  "setupFiles": [
    "jest-localstorage-mock"
  ]
}

You can also append this to the array if you have other setup files.

"jest": {
  "setupFiles": [
    "./__setups__/other.js",
    "jest-localstorage-mock"
  ]
}

Setup file

Alternatively you can create a new setup file which then requires this module or add the require statement to an existing setup file.

__setups__/localstorage.js

require('jest-localstorage-mock');

Add that file to your setupFiles array:

"jest": {
  "setupFiles": [
    "./__setups__/localstorage.js"
  ]
}

In create-react-app

For a create-react-app project you can replace the suggested mock with this at the beginning of the existing src/setupTests.js file:

require("jest-localstorage-mock");

In tests

By including this in your Jest setup you'll allow tests that expect a localStorage and sessionStorage object to continue to run. The module can also allow you to use the mocks provided to check that your localStorage is being used as expected.

The __STORE__ attribute of localStorage.__STORE__ or sessionStorage.__STORE__ is made available for you to directly access the storage object if needed.

Test Examples

Check that your localStorage calls were made when they were supposed to.

test('should save to localStorage', () => {
  const KEY = 'foo', VALUE = 'bar';
  dispatch(action.update(KEY, VALUE));
  expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);
  expect(localStorage.__STORE__[KEY]).toBe(VALUE);
  expect(Object.keys(localStorage.__STORE__).length).toBe(1);
});

Check that your sessionStorage is empty, examples work with either localStorage or sessionStorage.

test('should have cleared the sessionStorage', () => {
  dispatch(action.reset());
  expect(sessionStorage.clear).toHaveBeenCalledTimes(1);
  expect(sessionStorage.__STORE__).toEqual({}); // check store values
  expect(sessionStorage.length).toBe(0); // or check length
});

Check that localStorage calls were not made when they shouldn't have been.

test('should not have saved to localStorage', () => {
  const KEY = 'foo', VALUE = 'bar';
  dispatch(action.notIdempotent(KEY, VALUE));
  expect(localStorage.setItem).not.toHaveBeenLastCalledWith(KEY, VALUE);
  expect(Object.keys(localStorage.__STORE__).length).toBe(0);
});

Development

yarn install
yarn test

Pull Request

Before every PR run the following:

yarn run prettier

Publish

When publishing a new build, run the following:

yarn run prettier
yarn run build
npm version `${version}`
npm publish
git push --tags