vitest-dev / vitest-dev/vitest

__mocks__ folder beside hook being ingored

Open
#8,343 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 21h
Merged PRs (30d)
92

Description

Describe the bug

I want to get my useAppUrls hook to be mocked. I placed my mock in a __mocks__ folder beside the actual hook. In my test I call vi.mock(@urls/appUrls/useAppUrls). But it is not picking up the mock at all, it is just auto-mocking the return object (i.e., not seeing my file in the mocks folder).

Reproduction

If the mock is working properly, then the unit test renders link correctly (in MyComponent.test.ts) will pass.
StackBlitz Example

NOTE: I am using vite-tsconfig-paths and I believe my aliases are working properly. But the issue persists event if not using aliases, see the MyComponentNoAlias.test.ts file.

System Info
System:
    OS: Windows 11 10.0.26100
    CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
    Memory: 31.97 GB / 63.14 GB
  Binaries:
    Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 11.2.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (134.0.3124.85)
    Internet Explorer: 11.0.26100.1882

But the issue exists on Stack Blitz, as well as my Linux laptop.
Used Package Manager

npm

Validations

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 linked StackBlitz reproduction and inspect src/urls/appUrls/mocks/useAppUrls.ts, MyComponent.test.ts, and MyComponentNoAlias.test.ts. Run the unit test named "renders link correctly" with and without the alias; done means the test passes using the beside-the-hook mock rather than only an auto-mocked return object.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.