mockNuxtImport doesn't work for nested composables
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- nuxtjs, typescript
- Domain
- testing
Research direction
Start with the StackBlitz reproduction and composables/nested/useParent.nuxt.spec.ts, then run the reported npm test command to confirm the failing mock call. Trace how mockNuxtImport handles the re-exported composables in composables/index.ts. Done means the test observes the mock for useChild instead of the original implementation.
Written by the indexing model from the issue text.
Description
Environment
Nuxt project info: (copied to clipboard) nuxi 2:52:12 PM
------------------------------
- Operating System: Linux
- Node Version: v22.13.1
- Nuxt Version: 4.0.3
- CLI Version: 3.27.0
- Nitro Version: 2.12.4
- Package Manager: [email protected]
- Builder: -
- User Config: app, compatibilityDate, devtools, modules, devServer, imports, runtimeConfig, vite, routeRules
- Runtime Modules: @nuxt/[email protected], @nuxt/test-utils/[email protected]
- Build Modules: -
------------------------------
👉 Report an issue: https://github.com/nuxt/nuxt/issues/new?template=bug-report.yml
👉 Suggest an improvement: https://github.com/nuxt/nuxt/discussions/new
👉 Read documentation: https://nuxt.com
Reproduction
https://stackblitz.com/edit/github-lzbjpwsv
Describe the bug
mockNuxtImport doesn't seem to work with composables in a nested directory when following the documentation's recommendation to re-export nested composables (https://nuxt.com/docs/4.x/guide/directory-structure/app/composables#how-files-are-scanned).
- In the reproduction code, there are two composables in
composables/nested/:useParentanduseChild. useParentcallsuseChild.- These composables are re-exported by
composables/index.ts, as recommended by the aforementioned documentation. - The test suite for
useParent(composables/nested/useParent.nuxt.spec.ts) mocksuseChildusingmockNuxtImport; however,useParentcalls the originaluseChildinstead of the mock.
It's possible that this is a duplicate of https://github.com/nuxt/test-utils/issues/708. Apologies if so.
Switching to nested directory scanning via Nuxt config (nuxt.config.ts -> imports-> dirs) fixes the issue, but goes against the recommendations in the docs.
Additional context
Note that the repro. uses Nuxt 3, but the issue is still present in Nuxt 4.
Logs
❯ npm run test
> test
> vitest
(node:357) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
DEV v3.2.4 /home/projects/mklilmrmvl.github
stdout | composables/nested/useParent.nuxt.spec.ts > checks that the child is called
useParent called. Calling useChild().
useChild called.
❯ nuxt composables/nested/useParent.nuxt.spec.ts (1 test | 1 failed) 8ms
× checks that the child is called 7ms
→ expected +0 to be 1 // Object.is equality
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL nuxt composables/nested/useParent.nuxt.spec.ts > checks that the child is called
AssertionError: expected +0 to be 1 // Object.is equality
- Expected
+ Received
- 1
+ 0
❯ eval composables/nested/useParent.nuxt.spec.ts:13:3
11| useParent();
12|
13| expect(mockUseChild.mock.calls.length).toBe(1);
| ^
14| });
15|
❯ ../../../blitz.96435430.js:31:27890
❯ new Promise ../../../blitz.96435430.js:31:27849
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
Test Files 1 failed (1)
Tests 1 failed (1)
Start at 15:05:16
Duration 3.74s (transform 475ms, setup 449ms, collect 419ms, tests 8ms, environment 804ms, prepare 645ms)
FAIL Tests failed. Watching for file changes...
press h to show help, press q to quit
The test outputs "useChild called." That shouldn't happen. Instead, the mock should be called.
- Dominant language
- TypeScript
- Stars
- 441
- Forks
- 120
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 34
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nuxt/test-utils
-
pending triage
Difficulty 3/5 1-2 days Newbie friendliness 55/100
nuxt/test-utils#1671 · 4 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
pending triage
Difficulty 4/5 3-5 days Newbie friendliness 52/100
nuxt/test-utils#1607 · 2 comments ·
Maintainers usually reply within 1 day
-
`@vitest-environment-options` is not workingPossibly taken @yamachi4416 claimed this 179 days ago. Openpending triage
Difficulty 3/5 1-2 days Newbie friendliness 45/100
nuxt/test-utils#1541 · 2 reactions ·
Maintainers usually reply within 1 day
-
Nuxt `multiApp` breaks nuxt/test-utilsPossibly taken @nicolaspayot claimed this 879 days ago. Openpending triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
nuxt/test-utils#1521 · 1 comment · 4 reactions ·
Maintainers usually reply within 1 day
-
pending triage
Difficulty 4/5 3-5 days Newbie friendliness 35/100
nuxt/test-utils#1494 ·
Maintainers usually reply within 1 day
Similar issues
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 90/100
lingdojo/kana-dojo#32663 · 1 comment · 5 reactions ·
Maintainers usually reply within 1 day
-
Admin: Title Separator field can't be cleared (input falls back to "|")Possibly taken @emdashbot claimed this today. Openbot:bug bot:in-review
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
emdash-cms/emdash#4236 · 3 comments ·
Maintainers usually reply within 1 day
-
PHP workflow streaming requests buffer the full SSE responsePossibly taken @Winoren claimed this today. Openreview: medium
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
langgenius/dify#44270 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
area:holodeck review-finding
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
hyperdrive-technology/holodeck#142 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
code-yeongyu/senpi#3573 ·
Maintainers usually reply within 1 day