N
Nuxt5mo ago
peako

Mocking imported nuxt modules using @nuxt/test-utils environment

i have a vitest.config.ts that looks like this:
export default defineVitestConfig({
test: {
environment: 'nuxt',
setupFiles: './test/setup.ts',
},
});
export default defineVitestConfig({
test: {
environment: 'nuxt',
setupFiles: './test/setup.ts',
},
});
my setup file:
import { beforeAll } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup } from '@nuxt/test-utils'

beforeAll(async () => {
await setup({
rootDir: fileURLToPath(new URL('../fixtures/basic', import.meta.url)),
server: true, // Enable the server for SSR
});
});
import { beforeAll } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup } from '@nuxt/test-utils'

beforeAll(async () => {
await setup({
rootDir: fileURLToPath(new URL('../fixtures/basic', import.meta.url)),
server: true, // Enable the server for SSR
});
});
my fixture class:
export default defineNuxtConfig({
ssr: true,
modules: [
'../mocks/firebase',
'../mocks/login-code'
]
})
export default defineNuxtConfig({
ssr: true,
modules: [
'../mocks/firebase',
'../mocks/login-code'
]
})
an example of the "mocked nuxt modules" i'm trying to use:
export default {
meta: {
name: '@foo/moduleA',
configKey: 'moduleA',
compatibility: {
nuxt: '^3.0.0'
},
},
async setup(options, nuxt) {
console.log('setting up moduleA mock...')
// Mock implementation of the setup function
}
};
export default {
meta: {
name: '@foo/moduleA',
configKey: 'moduleA',
compatibility: {
nuxt: '^3.0.0'
},
},
async setup(options, nuxt) {
console.log('setting up moduleA mock...')
// Mock implementation of the setup function
}
};
given the above, how can i use my mocked modules in a vitest test? everytime i run the tests it seems to pick up my "real" module implementations, not the mocked ones
1 Reply
peako
peakoOP5mo ago
i'm surprised by how difficult it is to get something like this working for anyone else interested: the main issue was export default and not export default defineNuxtModule at least for a unit test within a nuxt environment, e2e tests still seem to use my actual module implementations for now….
Want results from more Discord servers?
Add your server