@shinshin86/local-storage-mock
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

localStorage Mock

This is the mock of localStorage.

The behavior is not perfectly reproduced (The value returned when an unintended value is passed, for example), but the basic logic is implemented.

Install

This package uses scoped modules. Therefore, please install it as follows.

npm install local-storage-mock@npm:@shinshin86/local-storage-mock
# or
yarn add local-storage-mock@npm:@shinshin86/local-storage-mock

Of course, you can also install it this way. In that case, please also enter the package with @ when you load it.

(Ex: require('@shinshin86/local-storage-mock') )

npm install @shinshin86/local-storage-mock
# or
yarn add @shinshin86/local-storage-mock

Usage

const { getLocalStorageMock } = require('local-storage-mock');

const window = {
  localStorage: getLocalStorageMock(),
};

window.localStorage.setItem('testkey', 'testvalue');
console.log(window.localStorage.getItem('testkey'));
// => testvalue

console.log(window.localStorage.key(0));
// => testkey

console.log(window.localStorage.length);
// => 1

window.localStorage.removeItem('testkey');
console.log(window.localStorage.getItem('testkey'));
// => null

window.localStorage.setItem('testkey', 'testvalue');
console.log(window.localStorage.getItem('testkey'));
// => testvalue

window.localStorage.clear();
console.log(window.localStorage.length);
// => 0

console.log(window.localStorage.getItem('testkey'));
// => null

TypeScript localStorage Mock

It was originally a project to create a mock for localStorage in TypeScript.

If you want to use the localStorage mock written in TypeScript, you can copy and use the code here( src/index.ts ).

License

MIT

Author

Yuki Shindo

Package Sidebar

Install

npm i @shinshin86/local-storage-mock

Weekly Downloads

51

Version

0.0.3

License

MIT

Unpacked Size

6.19 kB

Total Files

5

Last publish

Collaborators

  • shinshin86