Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
jest-localstorage-mock
Advanced tools
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.
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();
});
localstorage-memory is an in-memory implementation of the localStorage API. It can be used in Node.js environments to simulate localStorage without relying on a browser. Compared to jest-localstorage-mock, localstorage-memory is more focused on providing a full in-memory localStorage implementation rather than just mocking it for tests.
node-localstorage is a package that provides a localStorage implementation for Node.js. It writes data to disk, making it persistent across sessions. This is different from jest-localstorage-mock, which is designed specifically for mocking localStorage in tests and does not provide persistence.
mock-local-storage is another package that mocks the localStorage and sessionStorage APIs for testing purposes. It is similar to jest-localstorage-mock but also includes support for sessionStorage, providing a more comprehensive solution for testing storage APIs.
For yarn:
yarn add --dev jest-localstorage-mock
For npm:
npm i --save-dev jest-localstorage-mock
In your package.json
under the jest
section add the setupFiles
attribute with this module name.
"jest": {
"setupFiles": [
"jest-localstorage-mock"
]
}
Alternatively you can create a new setup file and require this module.
__setups__/localstorage.js
global.chrome = require('jest-localstorage-mock');
And add that file to your setupFiles
:
"jest": {
"setupFiles": [
"./__setups__/localstorage.js"
]
}
With this module setup in Jest you can run your web tests that rely on localstorage
without fail.
yarn install
yarn test
Before every PR run the following:
yarn run prettier
When publishing a new build, run the following:
yarn run prettier
yarn run build
yarn publish
FAQs
Auto mock all localstorage and sessionstorage APIs for your Jest tests
The npm package jest-localstorage-mock receives a total of 296,321 weekly downloads. As such, jest-localstorage-mock popularity was classified as popular.
We found that jest-localstorage-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.