Test Your Module
Testing helps ensure your module works as expected given various setups. Find in this section how to perform various kinds of tests against your module.
Write Unit Tests
Check out this RFC to join the conversation.
Write E2E Tests
Nuxt Test Utils is the go-to library to help you test your module in an end-to-end way. Here's the workflow to adopt with it:
- Create a Nuxt application to be used as a "fixture" inside
test/fixtures/* - Setup Nuxt with this fixture inside your test file
- Interact with the fixture using utilities from
@nuxt/test-utils(e.g. fetching a page) - Perform checks related to this fixture (e.g. "HTML contains ...")
- Repeat
In practice, the fixture:
// 1. Create a Nuxt application to be used as a "fixture"
import MyModule from '../../../src/module'
export default defineNuxtConfig({
ssr: true,
modules: [
MyModule,
],
})
And its test:
import { describe, expect, it } from 'vitest'
import { fileURLToPath } from 'node:url'
import { $fetch, setup } from '@nuxt/test-utils/e2e'
describe('ssr', async () => {
// 2. Setup Nuxt with this fixture inside your test file
await setup({
rootDir: fileURLToPath(new URL('./fixtures/ssr', import.meta.url)),
})
it('renders the index page', async () => {
// 3. Interact with the fixture using utilities from `@nuxt/test-utils`
const html = await $fetch('/')
// 4. Perform checks related to this fixture
expect(html).toContain('<div>ssr</div>')
})
})
// 5. Repeat
describe('csr', async () => { /* ... */ })
Test Manually
Having a playground Nuxt application to test your module when developing it is really useful. The module starter integrates one for that purpose.
You can test your module with other Nuxt applications (applications that are not part of your module repository) locally. To do so, you can use npm pack command, or your package manager equivalent, to create a tarball from your module. Then in your test project, you can add your module to package.json packages as: "my-module": "file:/path/to/tarball.tgz".
After that, you should be able to reference my-module like in any regular project.