Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-localstorage-mock

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-localstorage-mock

Auto mock all localstorage and sessionstorage APIs for your Jest tests

  • 2.4.26
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
264K
decreased by-9.71%
Maintainers
1
Weekly downloads
 
Created

What is jest-localstorage-mock?

The jest-localstorage-mock package is a Jest mock for the localStorage API, allowing developers to test code that interacts with localStorage without relying on the actual browser implementation. This can help ensure tests are isolated and run consistently across different environments.

What are jest-localstorage-mock's main functionalities?

Mocking localStorage

This feature allows you to mock the localStorage API, enabling you to test interactions with localStorage without relying on the actual browser implementation. The code sample demonstrates how to set an item in localStorage and verify that it was set correctly.

const localStorageMock = require('jest-localstorage-mock');

// Example test
it('should store an item in localStorage', () => {
  localStorage.setItem('key', 'value');
  expect(localStorage.setItem).toHaveBeenCalledWith('key', 'value');
  expect(localStorage.getItem('key')).toBe('value');
});

Clearing localStorage

This feature allows you to clear the localStorage, which is useful for ensuring a clean state between tests. The code sample demonstrates setting an item in localStorage and then clearing it, verifying that the item is no longer present.

const localStorageMock = require('jest-localstorage-mock');

// Example test
it('should clear localStorage', () => {
  localStorage.setItem('key', 'value');
  localStorage.clear();
  expect(localStorage.getItem('key')).toBeNull();
});

Removing an item from localStorage

This feature allows you to remove a specific item from localStorage. The code sample demonstrates setting an item in localStorage, removing it, and verifying that it has been removed.

const localStorageMock = require('jest-localstorage-mock');

// Example test
it('should remove an item from localStorage', () => {
  localStorage.setItem('key', 'value');
  localStorage.removeItem('key');
  expect(localStorage.getItem('key')).toBeNull();
});

Other packages similar to jest-localstorage-mock

Keywords

FAQs

Package last updated on 04 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc