nuxt / nuxt/test-utils

Nuxt `multiApp` breaks nuxt/test-utils

Open
#1,521 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
437
Forks
120
Avg merge
22h 52m
Merged PRs (30d)
23

Description

Environment

Nuxt project info (That's stackblitz's terminal info):

Operating system Linux 5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
CPU Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz (4 cores)
Node.js version v20.19.1
nuxt/cli version 3.31.3
Package manager npm@10.8.2
Nuxt version 4.2.2
Nitro version 2.12.9
Builder vite@7.3.0
Config compatibilityDate, devtools, future, modules
Modules @nuxt/test-utils/module@3.21.0
Reproduction

https://stackblitz.com/edit/github-sqshpc8h?file=nuxt.config.ts&view=editor

(using stackblitz's template for github/nuxt/test-utils/tree/main/examples/app-vitest, but I removed all non-necessary tests)

Describe the bug

We are moving a project from Vue to Nuxt and we are using future: { multiApp: true } as part of this project's solution. When moving tests, we noticed they don't work and are throwing errors that other projects weren't. Upon further investigation, we pinpointed to the multiApp flag.

Running tests (npm run test) with future: { multiApp: false } passes tests. When changing to future: { multiApp: true }, we got the following errors:

During vitest running the file:

stderr | test/app.nuxt.spec.ts
[nuxt] error caught during app initialization TypeError: Cannot read properties of undefined (reading 'app')
    at setup (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/plugins/router.js:48:139)
    at eval (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:156:60)
    at fn (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:238:44)
    at Object.runWithContext (/home/projects/jvmikwmyxv.github/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4218:18)
    at callWithNuxt (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:244:24)
    at eval (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:55:41)
    at EffectScope.run (/home/projects/jvmikwmyxv.github/node_modules/@vue/reactivity/dist/reactivity.cjs.js:85:16)
    at Object.runWithContext (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:55:31)
    at applyPlugin (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:156:39)
    at executePlugin (/home/projects/jvmikwmyxv.github/node_modules/nuxt/dist/app/nuxt.js:175:23)

which is referencing to NuxtPlugin nuxt:router's setup where useRuntimeConfig is not returning an app in useRuntimeConfig().app.baseURL

Image

and Vitest report after finished running shows the following:

 FAIL   nuxt  test/app.nuxt.spec.ts [ test/app.nuxt.spec.ts ]
TypeError: Cannot read properties of undefined (reading 'afterEach')
 ❯ setupNuxt node_modules/@nuxt/test-utils/dist/runtime/shared/nuxt.mjs:6:14
      4|   const nuxtApp = useNuxtApp();
      5|   await nuxtApp.callHook("page:finish");
      6|   useRouter().afterEach(() => nuxtApp.callHook("page:finish"));
       |              ^
      7| }
      8| 
 ❯ eval node_modules/@nuxt/test-utils/dist/runtime/entry.mjs:4:3

There seems to be some kind of issue in Router when multiApp is preset. We do have appId and app.rootId set in nuxt.config, but this made no change to the tests so I did not include in the stackblitz.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproduction from test/app.nuxt.spec.ts with future.multiApp enabled and compare it with multiApp disabled. Read the Nuxt router plugin stack trace and @nuxt/test-utils runtime/shared/nuxt.mjs, then verify that the test completes without the undefined app, router, or afterEach errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, typescript
Domain
frontend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.