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

🐛 BUG: vitest-pool-workers doesn't correctly resolve required modules if no extension is provided #6591

Closed
dario-piotrowicz opened this issue Aug 28, 2024 · 5 comments · Fixed by #8097 · May be fixed by #6661
Closed
Assignees
Labels
bug Something that isn't working vitest Relating to the Workers Vitest integration

Comments

@dario-piotrowicz
Copy link
Member

dario-piotrowicz commented Aug 28, 2024

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 dario-piotrowicz added the bug Something that isn't working label Aug 28, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Aug 28, 2024
@andyjessop andyjessop moved this from Untriaged to Backlog in workers-sdk Sep 16, 2024
@emily-shen
Copy link
Contributor

I think this is the same issue as #6405

@emily-shen emily-shen added the vitest Relating to the Workers Vitest integration label Dec 16, 2024
@dario-piotrowicz
Copy link
Member Author

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 🤔

@mattrubin
Copy link

mattrubin commented Jan 26, 2025

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: mattrubin/discord-worker-vitest-bug@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.

@edmundhung
Copy link
Member

edmundhung commented Feb 12, 2025

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

@mattrubin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working vitest Relating to the Workers Vitest integration
Projects
Status: Done
5 participants