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

module mock will not work with yarn workspaces mode #578

Closed
6 tasks done
uuau99999 opened this issue Jan 19, 2022 · 9 comments · Fixed by #712
Closed
6 tasks done

module mock will not work with yarn workspaces mode #578

uuau99999 opened this issue Jan 19, 2022 · 9 comments · Fixed by #712

Comments

@uuau99999
Copy link

Describe the bug

My project use yarn workspaces mode. So some node_modules will hoist to the project root node_modules directory.
However, when I use vi.mock to mock a module which hoisted to the root it didnot work as expected.
// in test.spec.ts
vi.mock('some-package');
// in index.ts
import { someFn } from 'some-package'; // someFn still not mocked
But when mocking a module not hoisted to the root node_modules it works as expected.

In this case to fix that, I have to use relateive path while mocking such module:
vi.mock('../some_path_to_root_dir/node_modules/some-package');
Which is pretty tricky. Hope that vi.mock can resolve the correct module path.
Thanks.

Reproduction

  1. Use yarn workspaces mode in package.json:
    "workspaces": [ "packages/**" ],
  2. create a sub project which path is like: root/packages/app1 and in this dir use yarn to install any package like axios.
  3. mock axios in test code like: vi.mock('axios') under the dir root/packages/app1;
  4. mock will not succeed.

System Info

System:
    OS: macOS 12.0.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 319.96 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.0 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.17.3 - ~/.yarn/bin/yarn
    npm: 6.14.13 - ~/.nvm/versions/node/v14.17.0/bin/npm

Used Package Manager

yarn

Validations

@Demivan
Copy link
Member

Demivan commented Jan 27, 2022

Is this still happening? I just tried to reproduce it and it works fine. Was able to mock hoisted axios package.
Please, provide a reproduction if this it still happening.

@NMinhNguyen
Copy link
Contributor

NMinhNguyen commented Feb 2, 2022

I have a repro: https://codesandbox.io/s/festive-goldstine-ysjbf?file=/packages/a/index.test.js I can mock an external package like lodash-es but I can't mock a workspace package (@minh/b). The terminal may not actually load for you online but if you check out the project, you should be able to run yarn && yarn test locally to repro:

image

Alternatively, you can fork the sandbox and open and file and hit Ctrl/Cmd + S to force a reload and it should work.

@Demivan
Copy link
Member

Demivan commented Feb 2, 2022

@NMinhNguyen Thanks for repro, this makes more sense then not being able to mock axios.

You can use resolve.alias config to mimic yarn workspaces: https://codesandbox.io/s/ecstatic-margulis-liqjb?file=/packages/a/vite.config.js

Will investigate if it is possible to resolve yarn workspace references.

@NMinhNguyen
Copy link
Contributor

NMinhNguyen commented Feb 2, 2022

You can use resolve.alias config to mimic yarn workspaces: https://codesandbox.io/s/ecstatic-margulis-liqjb?file=/packages/a/vite.config.js

Thanks for the temporary workaround!

Will investigate if it is possible to resolve yarn workspace references.

I've updated the repro to make use of require.resolve and it's able to do so without any Yarn-specific configuration, so I'm not sure why Vitest wouldn't be able to resolve? Take a look at https://codesandbox.io/s/festive-goldstine-ysjbf?file=/packages/a/test-resolve.js:0-206

@sheremet-va
Copy link
Member

I found the issue, will make a PR in a few hours.

@sheremet-va
Copy link
Member

I tested #712 on your reproduction, and everything worked 🎆

@NMinhNguyen
Copy link
Contributor

I tested #712 on your reproduction, and everything worked 🎆

Great, thank you for fixing this!

@uuau99999
Copy link
Author

Thanks!

chaii3 pushed a commit to chaii3/vitest that referenced this issue May 13, 2022
it was using "and" as title instead of "or"
@koistya
Copy link

koistya commented May 26, 2023

@sheremet-va could you check please if this is somehow related to this issue #3426

@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 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

Successfully merging a pull request may close this issue.

5 participants