vitest-environment-nuxt#
A vitest environment for testing code that needs a Nuxt runtime environment
Warning This library is in active development and you should pin the patch version before using.
Installation#
- First install
vitest-environment-nuxt:
pnpm add -D vitest-environment-nuxt
# or
yarn add --dev vitest-environment-nuxt
npm i -D vitest-environment-nuxt
- Then create a
vitest.config.mjswith the following content:
import { defineConfigWithNuxtEnv } from 'vitest-environment-nuxt/config'
export default defineConfigWithNuxtEnv({
// any custom vitest config you require
})
That's it. Now when you run vitest your Nuxt environment will be available throughout your tests.
👉 Important notes#
When you run your tests within vitest-environment-nuxt, they will be running in a happy-dom environment. Before your tests run, a global Nuxt app will be initialised (including, for example, running any plugins or code you've defined in your app.vue).
This means you should be take particular care not to mutate the global state in your tests (or, if you have, to reset it afterwards).
🛠️ Helpers#
vitest-environment-nuxt provides a number of helpers to make testing Nuxt apps easier.
mountSuspended#
// TODO:
mockNuxtImport#
mockNuxtImport allows you to mock Nuxt's auto import functionality. For example, to mock useStorage, you can do so like this:
import { mockNuxtImport } from 'vitest-environment-nuxt/utils'
mockNuxtImport('useStorage', () => {
return () => {
return { value: 'mocked storage' }
}
})
// your tests here
💻 Development#
- Clone this repository
- Enable Corepack using
corepack enable(usenpm i -g corepackfor Node.js < 16.10) - Install dependencies using
pnpm install - Stub the library using
pnpm dev:prepare - Run interactive tests using
pnpm test
License#
Made with ❤️
Published under the MIT License.