An vitest environment with support for testing code that needs a Nuxt runtime environment
Nuxt-vitest is a library that provides a vitest environment for testing code that requires a Nuxt runtime environment. It is currently in active development and caution should be taken when using it. The library offers features such as built-in mocks for the DOM environment, helpers for testing Nuxt apps, and the ability to mock Nuxt’s auto import functionality.
To install nuxt-vitest, follow these steps:
Install nuxt-vitest by running the command npm install nuxt-vitest.
Add nuxt-vitest to your nuxt.config.js file:
// nuxt.config.js
{
buildModules: ['nuxt-vitest']
}
Create a vitest.config.ts file with the provided content.
Set the environment for your tests. You can do this in multiple ways:
.nuxt. in their filename will be run in the Nuxt environment automatically.@vitest-environment nuxt as a comment in your test file to opt-in per test file.environment: 'nuxt' in the vitest.config.ts file to enable the Nuxt environment for all tests.Take care not to mutate the global state in your tests when running them in the Nuxt environment.
Nuxt-vitest is a library that provides a vitest environment for testing code that requires a Nuxt runtime environment. It offers features such as built-in mocks for the DOM environment, testing helpers, and the ability to mock Nuxt’s auto import functionality. Caution should be taken when using the library as it is currently in active development.