workers-sdk
workers-sdk copied to clipboard
Cannot test with `node-redis`: No such module ...
What versions & operating system are you using?
Wrangler v4.16.1 on Debian 12/WSL
Please provide a link to a minimal reproduction
No response
Describe the Bug
Vitest fails to run test when using node-redis, installed via npm install redis. The workers use redis via the nodejs compatibility flag.
Running the worker locally npx wrangler dev does work, only the test is failling.
Simple worker example:
src/index.ts:
import { createClient } from "redis";
export default {
async fetch(request, env, ctx): Promise<Response> {
const redis = await createClient({
socket: {
host: env.REDIS_HOST,
port: Number(env.REDIS_PORT),
tls: true
},
username: env.REDIS_USERNAME,
password: env.REDIS_PASSWORD
}).connect();
const result = await redis.ping() // should returns "PONG"
return new Response(result);
},
} satisfies ExportedHandler<Env>;
wrangler.jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "hello-world",
"main": "src/index.ts",
"compatibility_date": "2025-05-25",
"compatibility_flags": ["nodejs_compat"],
"observability": {
"enabled": true
}
}
tests/index.spec.ts
import { SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
describe('Hello World worker', () => {
it("test root", async () => {
const response = await SELF.fetch('https://example.com');
expect(await response.text()).toBe('PONG');
})
});
Please provide any relevant error logs
When running npm test:
> [email protected] test
> vitest
DEV v3.0.9 /home/caong/test
Using vars defined in .dev.vars
[vpw:inf] Starting isolated runtimes for vitest.config.mts...
[mf:wrn] The latest compatibility date supported by the installed Cloudflare Workers Runtime is "2025-05-08",
but you've requested "2025-05-25". Falling back to "2025-05-08"...
workerd/server/server.c++:3251: error: Fallback service failed to fetch module; exception = (unknown):-1: failed: std::exception: Uncaught JsExceptionThrown
stack: /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@27204c3 /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1a716fa /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1a4243b /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@272a198 /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1cc9801 /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@212c9ed /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@1cd8517 /home/caong/test/node_modules/@cloudflare/workerd-linux-64/bin/workerd@29f400c; spec = /?specifier=%2Fhome%2Fcaong%2Ftest%2Fnode_modules%2F%40redis%2Fclient%2Fdist%2Fpackage.json&referrer=%2Fhome%2Fcaong%2Ftest%2Fnode_modules%2F%40redis%2Fclient%2Fdist%2Flib%2Fclient%2Findex.js%3Fmf_vitest_no_cjs_esm_shim&rawSpecifier=..%2F..%2Fpackage.json
❯ test/index.spec.ts (0 test)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL test/index.spec.ts [ test/index.spec.ts ]
Error: No such module "home/caong/test/node_modules/@redis/client/dist/package.json".
❯ home/caong/test/node_modules/@redis/client/dist/lib/client/index.js?mf_vitest_no_cjs_esm_shim:24:24
❯ home/caong/test/node_modules/@redis/client/dist/index.js?mf_vitest_no_cjs_esm_shim:28:34
❯ home/caong/test/node_modules/redis/dist/index.js?mf_vitest_no_cjs_esm_shim:21:18
This is possibly an issue with unenv polyfills not being passed down correctly to vitest. I think @petebacondarwin is currently looking into this. Will add to our backlog
Related to #7324 maybe?