workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: `vitest-pool-workers` doesn't correctly resolve `require`d modules if no extension is provided

Open dario-piotrowicz opened this issue 1 year ago • 1 comments

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

dario-piotrowicz avatar Aug 28 '24 12:08 dario-piotrowicz

I think this is the same issue as #6405

emily-shen avatar Oct 11 '24 13:10 emily-shen

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 🤔

dario-piotrowicz avatar Jan 13 '25 12:01 dario-piotrowicz

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 dev in local mode works just fine. This issue specifically seems to impact code built/run for testing.

mattrubin avatar Jan 26 '25 22:01 mattrubin

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: {
        // ...
      },
    },
  },
});

edmundhung avatar Feb 12 '25 11:02 edmundhung

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).

mattrubin avatar Feb 13 '25 02:02 mattrubin