GithubHelp home page GithubHelp logo

sevensc / mock-local-storage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kaisforza/mock-local-storage

0.0 2.0 0.0 11 KB

Mock lodcalStorage for headless unit tests

License: MIT License

JavaScript 100.00%

mock-local-storage's Introduction

mock-local-storage

Mock localStorage for headless unit tests

Inspired by StackOverflow answers and wrapped into npm package.

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 localStorage from 'mock-local-storage'
window.localStorage = global.localStorage

This is very useful when you want to run headless tests on code mant 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 localStorage from 'mock-local-storage'
window.localStorage = global.localStorage

using-localstorage.test.js

import './mock-localstorage'

// unit tests follow here

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.

Additionally, test code can provide 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);
    });
});

Tests

npm install
npm test

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.