Closed chronicadventure closed 6 months ago
In order to use the imports from @nuxt/test-utils/runtime
, you need to configure vitest to run in a Nuxt runtime environment, which you can do by following the steps here: https://nuxt.com/docs/getting-started/testing#setup
In order to use the imports from
@nuxt/test-utils/runtime
, you need to configure vitest to run in a Nuxt runtime environment, which you can do by following the steps here: https://nuxt.com/docs/getting-started/testing#setup
If i use
// @vitest-environment nuxt
in my test file, I get this error:
WARN - IDE integration: Cannot finish not started testing
TypeError: Cannot read properties of undefined (reading 'app') at Object.setup (/frontend/node_modules/@nuxt/test-utils/dist/vitest-environment.mjs:68:87) at withEnv (/frontend/node_modules/vitest/dist/chunks/runtime-runBaseTests.SKlFOhuq.js:80:33) at run (/frontend/node_modules/vitest/dist/chunks/runtime-runBaseTests.SKlFOhuq.js:103:9) at runBaseTests (/frontend/node_modules/vitest/dist/vendor/base.nhvUBzQY.js:30:3) at run (/frontend/node_modules/vitest/dist/worker.js:130:5) at /frontend/node_modules/tinypool/dist/esm/entry/worker.js:72:20
Same thing happens to me.
I also failed to import it and found this thread and tried to run the test with nuxt
environment, then this error happened.
TypeError: Cannot read properties of undefined (reading 'app') Object.setup node_modules/@nuxt/test-utils/dist/vitest-environment.mjs:68:87 withEnv nodemodules/vitest/dist/chunks/runtime-runBaseTests.-x-nNuJ.js:81:33 run nodemodules/vitest/dist/chunks/runtime-runBaseTests.-x-nNuJ.js:105:9 runBaseTests node_modules/vitest/dist/vendor/base.gAwDs8Jc.js:30:3 run node_modules/vitest/dist/worker.js:93:5 node_modules/tinypool/dist/esm/entry/worker.js:72:20
The versions of the dependencies are below.
nuxt.config.ts
export default defineNuxtConfig({
...
modules: [
...
"@nuxt/test-utils/module",
...
],
...
vitest.config.ts
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { fileURLToPath } from "url";
export default defineConfig({
plugins: [vue()],
test: {
globals: true,
environment: "nuxt",
},
resolve: {
alias: [
{
find: "@",
replacement: fileURLToPath(new URL("./", import.meta.url)),
},
],
},
});
Please follow the instructions to use the config definition helper from this module in your vitest config, or you will not be benefiting from this library.
Environment
Reproduction
https://github.com/chronicadventure/nuxt-bug
just run
npm test
and the console will error out. Note you don't even need an actual test, just the import causes the entire file to fail.Describe the bug
Importing
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
as stated in the docs causes the entire test file to fail.Additional context
No response
Logs