-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Imported dependency file executed twice #5404
Comments
I've seen a similar issue recently and your case is somewhat similar where I thought there might be some packaging issue of wagmi, but they are all green on publint, so there might be a problem with Vitest's externalization heuristics. For the time being, I think it should be safe to explicitly set import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "jsdom",
server: {
deps: {
external: ["wagmi"]
}
}
},
}); I'll need to look into further to see where the issue lies. |
Thank you for help, I can confirm that this workaround/fix works! |
Vitest's externalization heuristic is based on vitest/packages/vite-node/src/externalize.ts Lines 64 to 72 in 6eda473
As mentioned in the comment, const package_ = await readPackageJSON(resolvedPath).catch(() => {});
// @ts-ignore
if (package_?.type === "module") {
return true;
}
if (/\.(\w+-)?esm?(-\w+)?\.js$|\/(esm?)\//.test(resolvedPath)) {
return false;
}
I wonder if Vitest introduced this flipped version on purpose or it may be a simple oversight. The PR #4368 is quite big, so I haven't gone through all the context. |
Describe the bug
My code is using libraries
@rainbow-me/rainbowkit
, which haswagmi
aspeerDependency
. These two work fine in development using vite, but trying to test same code with latest vitest fails. I tried to debug this, and found out thatnode_modules/wagmi/dist/esm/context.js
file is executed 2 times - once when my test code importsWagmiProvider
, and second time when@rainbow-me/rainbowkit
imports something fromwagmi
. This creates an issue becauseexport const WagmiContext = createContext(undefined);
is executed twice, creates two different contexts, and error is throw during test:WagmiProviderNotFoundError: useConfig must be used within WagmiProvider
If I downgrade vitest to
0.34.6
, same code works, andnode_modules/wagmi/dist/esm/context.js
is executed only onceMy test code:
node_modules/wagmi/dist/esm/context.js
which is executed twicepackage.json
vitest.config.ts
Reproduction
Here is small reproduction example. This one uses vitest version
1.4.0
. To make it work, downgrade it to0.34.6
https://github.com/pt7892/vitest-repro
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: