NuxtN
Nuxt2y 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',
  },
});


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
  });
});


my fixture class:
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
  }
};


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
Was this page helpful?