[READ-ONLY] Mirror of https://github.com/danielroe/nuxt-vitest. An vitest environment with support for testing code that needs a Nuxt runtime environment
nuxt nuxt-module testing unit-testing vitest
0

Configure Feed

Select the types of activity you want to include in your feed.

44 12 47

Clone this repository

https://tangled.org/danielroe.dev/nuxt-vitest https://tangled.org/did:plc:e3xxtgj4na7yot7auqe4qhjw
git@tangled.org:danielroe.dev/nuxt-vitest git@tangled.org:did:plc:e3xxtgj4na7yot7auqe4qhjw

For self-hosted knots, clone URLs may differ based on your setup.



README.md

vitest-environment-nuxt#

npm version npm downloads Github Actions Codecov

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#

  1. First install vitest-environment-nuxt:
pnpm add -D vitest-environment-nuxt

# or
yarn add --dev vitest-environment-nuxt
npm i -D vitest-environment-nuxt
  1. Then create a vitest.config.mjs with 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 (use npm i -g corepack for 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.