A mock implementation of Window.localStorage.
- Install package:
npm i localstorage-mock --save-dev
- Import / require module:
-
import 'localstorage-mock'
orrequire('localstorage-mock')
Mock should now be accessible globally as localStorage
.
-
getItem(key)
- get item by key -
setItem(key, value)
- insert new item -
clear()
- clear storage -
length
- number of items in storage
Note: I realise there are already packages out there which implement local storage. I wrote this simply as a fun exercise in publishing my first npm module.