nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
313 stars 81 forks source link

Failed to load url #imports after importing mockNuxtImport #800

Closed chronicadventure closed 6 months ago

chronicadventure commented 6 months ago

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

DEV  v1.4.0 C:/Users/AdrianCampos/BugRepo/repro

 ❯ tests/random.nuxt.test.js (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  tests/random.nuxt.test.js [ tests/random.nuxt.test.js ]
Error: Failed to load url #imports (resolved id: #imports) in C:/Users/AdrianCampos/BugRepo/node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs. Does the file exist?
 ❯ loadAndTransform ../node_modules/vite/dist/node/chunks/dep-B-u6xNiR.js:53832:21

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  no tests
   Start at  21:58:52
   Duration  2.22s (transform 69ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 846ms)

 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit
danielroe commented 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

larsalbrecht commented 4 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

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

spider-hand commented 4 months ago

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)),
      },
    ],
  },
});
danielroe commented 4 months ago

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.