🐛 BUG: `vitest-pool-workers` doesn't correctly resolve `require`d modules if no extension is provided
Which Cloudflare product(s) does this pertain to?
Workers Vitest Integration
What version(s) of the tool(s) are you using?
0.4.25
What version of Node are you using?
20
What operating system and version are you using?
Mac
Describe the Bug
vitest-pool-workers can fail to apply the correct module resolution on require calls that don't include a file extension
For example me and @WalshyDev noticed this with the discord api types package, in which, the package.json includes both cjs and esm versions for the various exports but vitest-pool-workers doesn't seem to correctly take those into consideration causing errors when trying to import from the package.
Please provide a link to a minimal reproduction
https://github.com/dario-piotrowicz/vitest-pool-workers-ext-repro
Please provide any relevant error logs
No response
I think this is the same issue as #6405
I think this is the same issue as #6405
I'm not sure, the error message is different, in #6405 the error seems to be:
Error: Cannot use require() to import an ES Module
while here it's
Error: No such module
feels to me like one is resolving the module incorrectly while the other is not resolving it at all 🤔
I recently encountered this same issue. My minimal repro (which appears quite similar to Dario's) is here: https://github.com/mattrubin/discord-worker-vitest-bug
The issue goes away if I switch my sample project to use Vitest without @cloudflare/vitest-pool-workers: https://github.com/mattrubin/discord-worker-vitest-bug/compare/main...do-not-use-vitest-pool-workers
Notably, this issue appeared on an old project that was previously testing without any issues. When I updated @cloudflare/vitest-pool-workers from 0.1.2 to 0.6.7 (and Vitest from 1.3.0 to 2.1.8), the
Error: No such module "[…]/node_modules/discord-api-types/v10.js". \ imported from "[…]/node_modules/discord-api-types/v10.mjs message first appeared. However, downgrading my minimal repro sample project to those same old dependency versions doesn't seem to resolve the issue in my sample project (possibly due to an updated transitive dependency?).
UPDATE with additional info:
- This issue was first seen on macOS 14.7.1 using npm 10.7.0
- Running
wrangler devin local mode works just fine. This issue specifically seems to impact code built/run for testing.
We have just added Vite dependency pre-bundling support on v0.6.14 that resolve this issue with the following config:
export default defineWorkersConfig({
test: {
deps: {
optimizer: {
ssr: {
enabled: true,
include: ["discord-api-types/v10"],
},
},
},
poolOptions: {
workers: {
// ...
},
},
},
});
Thanks for the fix! 🎉
FWIW, in order to get my tests to run with no errors, I ended up needing the include list to be ['@discordjs/rest', '@discordjs/core/http-only'] (the two module names my code imports from, which themselves depend on the troublesome discord-api-types/v10).