I'm experiencing an issue where my Cloudflare Worker with Durable Objects works correctly when deployed using wrangler deploy, but fails during testing with vitest-pool-workers due to Node module compatibility problems.
Problem Summary:
Deployment Works as Expected:
When I deploy my Worker using wrangler deploy, it successfully handles Node module dependencies through compatibility transformations.
This suggests that wrangler correctly replaces or polyfills Node-specific modules to make them compatible with the Workers environment.
Testing Fails Due to Node Dependencies:
When running tests using vitest and @cloudflare/vitest-pool-workers, the same Worker code fails to execute.
The testing environment does not seem to perform the same compatibility module replacements as wrangler deploy.
As a result, imports of Node modules cause errors, and the Durable Objects cannot be tested effectively.
Details:
Node Dependencies in Use:
import { https } from 'node:https';
import { Wallet, keccak256 } from 'ethers';
My Worker code imports https from 'node:https' and uses Wallet and keccak256 from the ethers library.
These dependencies are essential for the functionality of my Durable Objects.
Testing Setup:
I'm using vitest along with @cloudflare/vitest-pool-workers to test my Worker.
My vitest.config.ts is configured using defineWorkersProject from @cloudflare/vitest-pool-workers/config.
Error Messages During Testing:
When running tests, I encounter errors like:
Error: Cannot find module 'node:https'
This indicates that the Node modules are not being resolved or replaced in the testing environment.
Expected Behavior:
The testing environment provided by vitest-pool-workers should handle Node module compatibility in the same way as wrangler deploy.
This would allow tests to run successfully, ensuring that the Durable Objects function correctly in both development and production environments.
Questions and Request for Assistance:
Is there a way to configure vitest-pool-workers to perform the same Node module compatibility transformations as wrangler deploy?
Are there recommended practices for testing Workers that depend on Node modules using vitest-pool-workers?
Could this be a limitation or missing feature in vitest-pool-workers that needs to be addressed?
Steps to Reproduce:
Create a Worker with Node Dependencies:
Write a Cloudflare Worker script that imports Node modules like node:https and uses ethers.
Deploy the Worker:
Use wrangler deploy to deploy the Worker.
Confirm that it works as expected in the production environment.
Set Up Testing Environment:
Install vitest and @cloudflare/vitest-pool-workers.
Configure vitest.config.ts using defineWorkersProject:
I tried adjusting the vitest configuration, including using defineConfig and specifying pool: '@cloudflare/vitest-pool-workers', but the issue persists.
It seems that the compatibility layer provided by wrangler during deployment is not available or not activated during testing.
Conclusion:
I believe there may be a discrepancy between how wrangler deploy and vitest-pool-workers handle Node module compatibility. I would appreciate any guidance on resolving this issue or insights into whether this is a known limitation.
Which Cloudflare product(s) does this pertain to?
Workers Vitest Integration
What version(s) of the tool(s) are you using?
Wrangler 3.83, "vitest": "^2.1.5", "@cloudflare/vitest-pool-workers": "^0.5.28",
What version of Node are you using?
v20.13.1
What operating system and version are you using?
Mac Sonoma 15.3
Describe the Bug
Description:
I'm experiencing an issue where my Cloudflare Worker with Durable Objects works correctly when deployed using
wrangler deploy
, but fails during testing withvitest-pool-workers
due to Node module compatibility problems.Problem Summary:
Deployment Works as Expected:
wrangler deploy
, it successfully handles Node module dependencies through compatibility transformations.wrangler
correctly replaces or polyfills Node-specific modules to make them compatible with the Workers environment.Testing Fails Due to Node Dependencies:
vitest
and@cloudflare/vitest-pool-workers
, the same Worker code fails to execute.wrangler deploy
.Details:
Node Dependencies in Use:
https
from'node:https'
and usesWallet
andkeccak256
from theethers
library.Testing Setup:
vitest
along with@cloudflare/vitest-pool-workers
to test my Worker.vitest.config.ts
is configured usingdefineWorkersProject
from@cloudflare/vitest-pool-workers/config
.Error Messages During Testing:
When running tests, I encounter errors like:
This indicates that the Node modules are not being resolved or replaced in the testing environment.
Expected Behavior:
vitest-pool-workers
should handle Node module compatibility in the same way aswrangler deploy
.Questions and Request for Assistance:
Is there a way to configure
vitest-pool-workers
to perform the same Node module compatibility transformations aswrangler deploy
?Are there recommended practices for testing Workers that depend on Node modules using
vitest-pool-workers
?Could this be a limitation or missing feature in
vitest-pool-workers
that needs to be addressed?Steps to Reproduce:
Create a Worker with Node Dependencies:
node:https
and usesethers
.Deploy the Worker:
wrangler deploy
to deploy the Worker.Set Up Testing Environment:
vitest
and@cloudflare/vitest-pool-workers
.Configure
vitest.config.ts
usingdefineWorkersProject
:Run Tests:
vitest
to run the tests.Additional Information:
Environment Details:
vitest
version: [Please specify]@cloudflare/vitest-pool-workers
version: [Please specify]wrangler
version: [Please specify]Attempts to Resolve:
vitest
configuration, including usingdefineConfig
and specifyingpool: '@cloudflare/vitest-pool-workers'
, but the issue persists.wrangler
during deployment is not available or not activated during testing.Conclusion:
I believe there may be a discrepancy between how
wrangler deploy
andvitest-pool-workers
handle Node module compatibility. I would appreciate any guidance on resolving this issue or insights into whether this is a known limitation.Thank you for your assistance!
Please provide a link to a minimal reproduction
https://github.com/zach-is-my-name/reproduction-vitest-pool-compat-module-error
Please provide any relevant error logs
Click to expand
[~/Projects/charli/apps/session-time-tracker]$ bun run test $ vitest run --config tests/vitest.config.ts RUN v2.1.5 /Users/zm/Projects/charli/apps/session-time-tracker Using vars defined in .dev.vars [vpw:inf] Starting isolated runtimes for tests/vitest.config.ts... ❯ tests/sessionTimer.test.ts (0) ❯ SessionTimer Durable Object (0) ❯ tests/websocketEndpoint.test.ts (3) ❯ POST /websocket (3) × should process peer:joined websocket event · should return error for invalid signature · should return error for unsupported event type workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2Futils%2Fgeturl.js&rawSpecifier=node%3Ahttps workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2Futils%2Fgeturl.js&rawSpecifier=node%3Ahttps workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2Futils%2Fgeturl.js&rawSpecifier=node%3Ahttps workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2Futils%2Fgeturl.js&rawSpecifier=node%3Ahttps workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2Futils%2Fgeturl.js&rawSpecifier=node%3Ahttps workerd/server/server.c++:3059: error: Fallback service failed to fetch module; payload = ; spec = /?specifier=node%3Ahttps&referrer=%2FUsers%2Fzm%2FProjects%2Fcharli%2Fapps%2Fsession-time-tracker%2Fnode_modules%2Fethers%2Flib.esm%2 ❯ tests/connectionManager.test.ts (0) ❯ tests/initEndpoint.test.ts (0) ❯ tests/sessionTimer.test.ts (0) ❯ SessionTimer Durable Object (0) ❯ tests/webSocketManager.test.ts (0) ❯ tests/webhookEndpoint.test.ts (0) ❯ tests/websocketEndpoint.test.ts (3) ❯ POST /websocket (3) × should process peer:joined websocket event × should return error for invalid signature × should return error for unsupported event type ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 6 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ FAIL tests/connectionManager.test.ts [ tests/connectionManager.test.ts ] FAIL tests/initEndpoint.test.ts [ tests/initEndpoint.test.ts ] FAIL tests/webSocketManager.test.ts [ tests/webSocketManager.test.ts ] FAIL tests/webhookEndpoint.test.ts [ tests/webhookEndpoint.test.ts ] Error: No such module "node:https". imported from "Users/zm/Projects/charli/apps/session-time-tracker/node_modules/ethers/lib.esm/utils/geturl.js" ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/9]⎯ FAIL tests/sessionTimer.test.ts [ tests/sessionTimer.test.ts ] Error: No test found in suite tests/sessionTimer.test.ts ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/9]⎯ FAIL tests/sessionTimer.test.ts > SessionTimer Durable Object Error: No test found in suite SessionTimer Durable Object ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/9]⎯ ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ FAIL tests/websocketEndpoint.test.ts > POST /websocket > should process peer:joined websocket event AssertionError: expected 404 to be 200 // Object.is equality - Expected + Received - 200 + 404 ❯ tests/websocketEndpoint.test.ts:51:29 49| }); 50| 51| expect(response.status).toBe(200); | ^ 52| const text = await response.text(); 53| expect(text).toBe('Webhook processed successfully'); ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[4/9]⎯ FAIL tests/websocketEndpoint.test.ts > POST /websocket > should return error for invalid signature AssertionError: expected 404 to be 401 // Object.is equality - Expected + Received - 401 + 404 ❯ tests/websocketEndpoint.test.ts:78:29 76| }); 77| 78| expect(response.status).toBe(401); | ^ 79| const data = await response.json() as { status: string; message: string }; 80| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[5/9]⎯ FAIL tests/websocketEndpoint.test.ts > POST /websocket > should return error for unsupported event type AssertionError: expected 404 to be 400 // Object.is equality - Expected + Received - 400 + 404 ❯ tests/websocketEndpoint.test.ts:98:29 96| }); 97| 98| expect(response.status).toBe(400); | ^ 99| const data = await response.json() as { status: string; message: string }; 100| ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[6/9]⎯ Test Files 6 failed (6) Tests 3 failed (3) Start at 18:38:11 Duration 918ms (transform 107ms, setup 131ms, collect 51ms, tests 52ms, environment 0ms, prepare 935ms) [vpw:dbg] Shutting down runtimes...