https://github.com/mitscherlich/vitest-localstorage-mock
A module to mock window.localStorage and window.sessionStorage in Vitest
https://github.com/mitscherlich/vitest-localstorage-mock
Last synced: over 1 year ago
JSON representation
A module to mock window.localStorage and window.sessionStorage in Vitest
- Host: GitHub
- URL: https://github.com/mitscherlich/vitest-localstorage-mock
- Owner: Mitscherlich
- License: mit
- Created: 2022-09-27T15:41:13.000Z (almost 4 years ago)
- Default Branch: dev
- Last Pushed: 2025-02-25T18:45:18.000Z (over 1 year ago)
- Last Synced: 2025-02-26T09:41:34.050Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://npm.im/vitest-localstorage-mock
- Size: 690 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vitest-localstorage-mock
Auto mock `localstorage` and `sessionstorage` in your [Vitest](https://vitest.dev/) scripts for testing.
[](https://npm.im/vitest-localstorage-mock) [](https://npm.im/vitest-localstorage-mock) 
## Install
via `pnpm`, `yarn` or `npm`:
```bash
pnpm add -D vitest-localstorage-mock
# or
yarn add -D vitest-localstorage-mock
# or
npm install -D vitest-localstorage-mock
```
## Setup
### Module
In your `vite.config.(js|ts)` under the `test` [configuration section](https://vitest.dev/config/#options) create a `setupFiles` array and add `vitest-localstorage-mock` to the array. ~~Also, ensure you have enabled [`globals`](https://vitest.dev/config/#globals)~~ We don't need globals any more.
```js
import { defineConfig } from 'vite'
export default defineConfig({
test: {
setupFiles: ['vitest-localstorage-mock'],
mockReset: false,
}
})
```
### Setup file
Alternatively you can create a new setup file which then requires this module or
add the `require` statement to an existing setup file.
- `__setups__/localstorage.js`
```js
import 'vitest-localstorage-mock'
```
Add that file to your `setupFiles` array:
```js
import { defineConfig } from 'vite'
export default defineConfig({
test: {
setupFiles: ['./__setups__/localstorage.js'],
mockReset: false,
}
})
```
## License
MIT © [Mitscherlich](https://mitscherlich.me)