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
Can't mock dependency of dependency #1336
Comments
You need to inline that dependency, using deps.inline config option. It is not inlined by default because of performance reasons. |
@sheremet-va thanks for the quick response, are you saying doing something like this: export default defineConfig({
test: {
deps: {
inline: ['loglevel'],
},
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
},
}); ? |
You need to inline the dependency that you are using. So, according to you example, you need to inline If you don't inline dependency, it doesn't go through Vite pipeline and can't benefit from our mock system, because it relies on it. |
I tried this in the Stackblitz example without success. |
They don't bundle ESM, so we cannot mock their call to |
That makes sense. Thanks for the clarification. |
@sheremet-va ok, I found now what the problem was. The dependency was dynamically imported, so e.g. if (!EnvRequestLib) {
EnvRequestLib = process?.versions?.node
? (await import('./node')).default
: (await import('./browser')).default
}
return new EnvRequestLib(method, endpoint, body, isHubCommand) Then vitest is not able to mock neither |
Vitest supports mocking dynamic modules |
But not if they are inlined dependencies. I am trying to build a reproducible but it is quite difficult. |
Mocking only works with files, that are processed by Vitest. Usually, node modules are not processed. If you use “deps.inline”, then every import inside that dependency will work with mocking system. |
@sheremet-va I kindly request to re-open the issue. I went a long way to figure out the problem: you are right that using However for some reason this was still not working in my project and and seems to be relative with linking NPM packages when working in Monorepos, e.g. Lerna. Even if setting |
Nevermind, it started to work for random reasons after debugging vitest. Not sure what has changed. |
Describe the bug
Given I have
packageA
that depends onpackageB
which depends onpackageC
. If I write tests forpackageA
I can't mockpackageC
, e.g.:Now If I want to write tests for
packageA
I want to be able to mock the dependency ofpackageB
rather than the whole package:Unfortunately it is impossible to mock
got
in this case. In Jest however this is possible.Reproduction
In this example I mock
loglevel
which is a direct dependency of@wdio/logger
. The test should throw an error because the dependency was mocked out with an undefined value but the test passes.System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: