Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Can't define custom vitest.config file anymore since Nuxt 4 #1375

@wJoenn

Description

@wJoenn

Environment

  • Operating System: Linux
  • Node Version: v22.14.0
  • Nuxt Version: 4.0.2
  • CLI Version: 3.27.0
  • Nitro Version: 2.12.4
  • Package Manager: [email protected]
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-rh3mlpir

Describe the bug

Since I updated my module's nuxt version to 4.0.2 I can't run my tests anymore.
Locally I get this error

➜  shared git:(dependabot/npm_and_yarn/nuxt-4.0.2) yarn test
yarn run v1.22.22
$ vitest run
failed to load config from /home/joenn/code/leexi/shared/vitest.config.ts

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Startup Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
AggregateError: Failed to initialize projects. There were errors during projects setup. See below for more details.
    at resolveProjects (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:7486:27)
    at async Vitest._setServer (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:9319:20)
    at async BasicMinimalPluginContext.handler (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10169:6)
    at async _createServer (file:///home/joenn/code/leexi/shared/node_modules/vite/dist/node/chunks/dep-BHkUv4Z8.js:28570:86)
    at async createViteServer (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:6911:17)
    at async createVitest (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10202:17)
    at async prepareVitest (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10541:14)
    at async startVitest (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10488:14)
    at async start (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cac.Cb-PYCCB.js:1412:15)
    at async CAC.run (file:///home/joenn/code/leexi/shared/node_modules/vitest/dist/chunks/cac.Cb-PYCCB.js:1391:2) {
  [errors]: [
    Error: Nuxt instance is unavailable!
        at useNuxt (file:///home/joenn/code/leexi/shared/node_modules/@nuxt/vite-builder/node_modules/@nuxt/kit/dist/index.mjs:43:11)
        at useNitro (file:///home/joenn/code/leexi/shared/node_modules/@nuxt/vite-builder/node_modules/@nuxt/kit/dist/index.mjs:795:16)
        at useResolveFromPublicAssets (file:///home/joenn/code/leexi/shared/node_modules/@nuxt/vite-builder/dist/index.mjs:666:17)
        at PublicDirsPlugin (file:///home/joenn/code/leexi/shared/node_modules/@nuxt/vite-builder/dist/index.mjs:573:39)
        at bundle (file:///home/joenn/code/leexi/shared/node_modules/@nuxt/vite-builder/dist/index.mjs:1662:11)
        at async bundle (file:///home/joenn/code/leexi/shared/node_modules/nuxt/dist/index.mjs:7275:5)
        at async build (file:///home/joenn/code/leexi/shared/node_modules/nuxt/dist/index.mjs:7135:3)
  ]
}



error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

In the reproduction a similar error can be found when running yarn test

~/projects/bnmijrqrzb.github 13s
❯ yarn test
yarn run v1.22.19
$ vitest run
(node:399) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
failed to load config from /home/projects/bnmijrqrzb.github/vitest.config.ts
[warn] Overriding NuxtWelcome component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtLayout component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtErrorBoundary component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding ClientOnly component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding DevOnly component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding ServerPlaceholder component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtLink component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtLoadingIndicator component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtTime component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtRouteAnnouncer component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtImg component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtPicture component. You can specify a `priority` option when calling `addComponent` to avoid this warning.
[warn] Overriding NuxtIsland component. You can specify a `priority` option when calling `addComponent` to avoid this warning.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Startup Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
AggregateError: Failed to initialize projects. There were errors during projects setup. See below for more details.
    at resolveProjects (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:7515:27)
    at async Vitest._setServer (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:9348:20)
    at async BasicMinimalPluginContext.handler (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10198:6)
    at async _createServer (file:///home/projects/bnmijrqrzb.github/node_modules/vite/dist/node/chunks/dep-BHkUv4Z8.js:28667:86)
    at async createViteServer (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:6940:17)
    at async createVitest (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10231:17)
    at async prepareVitest (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10570:14)
    at async startVitest (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cli-api.BkDphVBG.js:10517:14)
    at async start (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cac.Cb-PYCCB.js:1429:15)
    at async CAC.run (file:///home/projects/bnmijrqrzb.github/node_modules/vitest/dist/chunks/cac.Cb-PYCCB.js:1408:2) {
  [errors]: [
    Error: Nitro is not initialized yet. You can call `useNitro()` only after `ready` hook.
        at Module.useNitro (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/kit/dist/index.mjs:890:11)
        at initNuxt (file:///home/projects/bnmijrqrzb.github/node_modules/nuxt/dist/index.mjs:5772:27)
        at async loadNuxt (file:///home/projects/bnmijrqrzb.github/node_modules/nuxt/dist/index.mjs:5864:5)
        at async Module.loadNuxt (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/test-utils/node_modules/@nuxt/kit/dist/index.mjs:2822:19)
        at async startNuxtAndGetViteConfig (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/test-utils/dist/config.mjs:57:16)
        at async getVitestConfigFromNuxt (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/test-utils/dist/config.mjs:111:15)
        at async resolveConfig (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/test-utils/dist/config.mjs:297:5)
        at async eval (file:///home/projects/bnmijrqrzb.github/node_modules/@nuxt/test-utils/dist/config.mjs:242:28)
        at async loadConfigFromFile (file:///home/projects/bnmijrqrzb.github/node_modules/vite/dist/node/chunks/dep-BHkUv4Z8.js:36439:20)
        at async resolveConfig (file:///home/projects/bnmijrqrzb.github/node_modules/vite/dist/node/chunks/dep-BHkUv4Z8.js:36093:22)
  ]
}



error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Deleting the vitest.config.ts file resolves the issue but I want to define a custom config file to add aliases to vitest as well as configure some of the test options

import path from 'path';
import { defineVitestConfig } from '@nuxt/test-utils/config';

export default defineVitestConfig({
  resolve: {
    alias: {
      '~': path.resolve(__dirname),
    },
  },
  test: {
    environment: 'happy-dom',
    globals: true,
    reporters: 'verbose',
  },
});

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions