Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__mocks__ folder doesn't work #552

Closed
6 tasks done
fadi-george opened this issue Jan 17, 2022 · 6 comments
Closed
6 tasks done

__mocks__ folder doesn't work #552

fadi-george opened this issue Jan 17, 2022 · 6 comments

Comments

@fadi-george
Copy link

Describe the bug

__mocks__ is a common way (with Jest) to mock node-modules (and something helpers). But currently it doesn't seem to work. It works for my regular Jest setup that uses vite-tsconfig-paths.

Reproduction

https://stackblitz.com/edit/node-b3r6jj?file=src%2FSomeComponent.spec.tsx

I tried vite-tsconfig-paths (similar to my own setup), as well as Vite's own resolve alias config. Neither work.

System Info

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 82.06 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  Browsers:
    Brave Browser: 97.1.34.80
    Chrome: 97.0.4692.71
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-react: ^1.1.4 => 1.1.4 
    vite: 2.7.12 => 2.7.12 
    vitest: 0.1.17 => 0.1.16

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Jan 17, 2022

We don't support automocking. You need to manually call vi.mock("path"). Or you can call it once in setupFiles.

For external libraries we use root/__mocks__ folder. For internal, you need to have __mocks__ folder inside the folder where the file is located.

Docs: https://vitest.dev/api/#vi-mock

Working stackblitz: https://stackblitz.com/edit/node-e1vryb?file=src/hooks/__mocks__/index.ts

@sschneider-ihre-pvs
Copy link

What about paths like @composable/useSomething will that be resolved in vi.mock ?

@sheremet-va
Copy link
Member

What about paths like @composable/useSomething will that be resolved in vi.mock ?

yes

@sschneider-ihre-pvs
Copy link

sschneider-ihre-pvs commented Feb 9, 2022

Ok, that doesn't work also :)
is there a debugging possibility to inspect where vi.mock('@composable/useSomething') is looking for the mock?
seems like that kind of aliasing doesn't work so well with vite/vitest

@sheremet-va
Copy link
Member

sheremet-va commented Feb 9, 2022

No debugging option available at the moment. You can look into examples/mocks in the repo.

If nothing works, please create an issue with reproduction.

@sschneider-ihre-pvs
Copy link

I tried it out in the stackblitz repo

@github-actions github-actions bot locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants