cloudflare / workers-sdk

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

🚀 Feature Request: Supporting Node.js `readFileSync`[vitest-pool-workers] #6071

Open m-shaka opened 2 weeks ago

m-shaka commented 2 weeks ago

Describe the solution

Node.js polyfill of readFileSync is not yet implemented, but Vitest@1.6.x uses readFileSync. https://github.com/vitest-dev/vitest/blob/6b29f3ddc86060cf3265959d4ae32e90b186cb92/packages/vitest/src/runtime/execute.ts#L110

It's a barrier to supporting Vitest@1.6.x.

Is there any problem with implementing it? Can I help you?

petebacondarwin commented 2 weeks ago

@m-shaka - is this in terms of writing tests using the @cloudflare/vitest-pool-workers to write Workers tests or some other use of Vitest?

What would the behaviour be for loading files in a Worker?

m-shaka commented 2 weeks ago

Thank you for your quick response. seems like it's required just to run Vitest with @cloudflare/vitest-pool-workers.

Here is a minimum repro. vitest run fails since readFileSync is not yet implemented. https://codesandbox.io/p/devbox/misty-butterfly-lnhhns

I'm not sure what Vitest is doing there, probably it's about executing test files

m-shaka commented 2 weeks ago

I found https://github.com/cloudflare/workerd/pull/2147

~From my understanding, @cloudflare/vitest-pool-workers will work with Vitest >= 1.6 if miniflare supports experimental:nodejs_compat_v2 compatibility flag. Is that true?~ (It was a misunderstanding. v2 doesn't seem to support fs yet)

After I added experimental:nodejs_compat_v2 to compatibility_flags, I got this error. https://codesandbox.io/p/devbox/misty-butterfly-forked-mmn6v3?file=%2Fwrangler.toml

service core:user:vitest-pool-workers-runner-: No such compatibility flag: experimental:nodejs_compat_v2

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
 ❯ #assembleAndUpdateConfig node_modules/miniflare/dist/src/index.js:9161:13
 ❯ process.processTicksAndRejections node:internal/process/task_queues:95:5
 ❯ Mutex.runWith node_modules/miniflare/dist/src/index.js:3521:16
 ❯ #waitForReady node_modules/miniflare/dist/src/index.js:9218:5
 ❯ getProjectMiniflare node_modules/@cloudflare/vitest-pool-workers/dist/pool/index.mjs:1306:5
 ❯ Object.runTests node_modules/@cloudflare/vitest-pool-workers/dist/pool/index.mjs:1556:20
 ❯ Object.runTests node_modules/vitest/dist/vendor/cli-api.E07AF1Yq.js:8862:5
 ❯ async file:/project/workspace/node_modules/vitest/dist/vendor/cli-api.E07AF1Yq.js:11207:9