cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.72k stars 716 forks source link

🐛 BUG: vitest-pool-workers Cannot use require() to import an ES Module #6715

Closed AdiRishi closed 1 month ago

AdiRishi commented 2 months ago

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

What version(s) of the tool(s) are you using?

3.78.2 [wrangler], 0.5.2 [vitest-pool-workers]

What version of Node are you using?

20.17.0

What operating system and version are you using?

Mac Sonoma 14.6.1 (23G93)

Describe the Bug

Observed behavior

When using the @clerk/backend package and @cloudflare/vitest-pool-workers tests do not run successfully. The reported error is TypeError: Cannot use require() to import an ES Module. and it happens because of the snakecase-keys module that clerk uses.

It is worth noting that this error does not stop the actual worker from functioning, esbuild is able to handle this when building and deploying this worker.

I did try to solve this by using deps.inline however this didn't work either.

Expected behavior

I'm no expert in JS bundlers but I feel like this should be a solvable problem? Otherwise we are severly limited in what deps we can use when working with Cloudflare workers.

Please provide a link to a minimal reproduction

https://github.com/AdiRishi/vitest-pool-esm-bug

Please provide any relevant error logs

[vpw:inf] Starting isolated runtimes for vitest.config.mts...
 ❯ test/index.spec.ts (0)

⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯

 FAIL  test/index.spec.ts [ test/index.spec.ts ]
TypeError: Cannot use require() to import an ES Module.
 ❯ Users/arishi/playground/vitest-pool-esm-bug/node_modules/.pnpm/snakecase-keys@5.4.4/node_modules/snakecase-keys/index.js?mf_vitest_no_cjs_esm_shim:4:23

 Test Files  1 failed (1)
      Tests  no tests
   Start at  15:41:01
   Duration  771ms (transform 25ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 138ms)

[vpw:dbg] Shutting down runtimes...
 ELIFECYCLE  Test failed. See above for more details.
ogadra commented 1 month ago

Thank you for creating the issue. I tested the following versions but encountered the same error:

  1. "vitest": "1.5.0" && "@cloudflare/vitest-pool-workers": "0.4.31"
  2. "vitest": "2.1.1" && "@cloudflare/vitest-pool-workers": "0.5.7"

I hope for a resolution to this issue.

Ramzil11 commented 1 month ago

I have the same problem

emily-shen commented 1 month ago

Hey, I had a look at your repro and it seems like vitest will work if you change your config to:

deps: {
    inline: ['@clerk/backend', 'snakecase-keys', 'snake-case'],
}

Let me know if you're still having issues :)

AdiRishi commented 1 month ago

@emily-shen thank you so much!

I can't believe I didn't think to include the actual @clerk/backend package in deps.inline 🤦 Your fix absolutely works 😄 . I'll close this issue out 💥 .

For anyone worried about the vitest deprecation warning that comes with using deps.inline, you can achieve the same effect with server.deps.inline

server: {
    deps: {
        inline: ['@clerk/backend', 'snakecase-keys', 'snake-case'],
    },
},
mja00 commented 1 month ago

I've been following this and will have to see if including more in the inline deps fixes the same issue with hono.