nuxt / nuxt/test-utils

mockNuxtImport doesn't work for nested composables

Open
#1,395 0 comments 0 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: (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:  npm@11.3.0
- Builder:          -
- User Config:      app, compatibilityDate, devtools, modules, devServer, imports, runtimeConfig, vite, routeRules
- Runtime Modules:  @nuxt/eslint@1.5.2, @nuxt/test-utils/module@3.19.2
- 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/: useParent and useChild.
  • useParent calls useChild.
  • 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) mocks useChild using mockNuxtImport; however, useParent calls the original useChild instead 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.

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 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.

Assessment

Tech stack
nuxtjs, typescript
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.