vitest-localstorage-mock

0.1.2 • Public • Published

vitest-localstorage-mock

Auto mock localstorage and sessionstorage in your Vitest scripts for testing.

npm version npm downloads actions status

Install

via pnpm, yarn or npm:

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 create a setupFiles array and add vitest-localstorage-mock to the array. Also, ensure you have enabled globals We don't need globals any more.

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
import 'vitest-localstorage-mock'

Add that file to your setupFiles array:

import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    setupFiles: ['./__setups__/localstorage.js'],
    mockReset: false,
  }
})

License

MIT © Mitscherlich

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i vitest-localstorage-mock

    Weekly Downloads

    17,050

    Version

    0.1.2

    License

    MIT

    Unpacked Size

    6.12 kB

    Total Files

    4

    Last publish

    Collaborators

    • mitscherlich36