๐Ÿ”Œ Multiple Workers ยท Miniflare

Hi, I have a main worker which has service bindings to two other workers. I would like to create jest integration test for the main worker. I have the directory structure similar to the one in https://miniflare.dev/core/mount and the main worker wrangler.toml has [miniflare.mounts] [build.upload]. I am creating a miniflare instance in the jest test file with a script path
describe('bluegreen routing', () => { const mf = new Miniflare({ envPath: true, packagePath: true, wranglerConfigPath: true, scriptPath: 'dist/index.mjs', modules: true }) it('should route to the green worker', async () => { const response = await mf.dispatchFetch('http://127.0.0.1/parent', { method: 'GET' }) if (response) { const parsedResp = await response.text() expect(parsedResp).toEqual('Green Hello World!') } }) })
This test exceeds the timeout. I get this error ReferenceError: clearImmediate is not defined Is this the right way to do jest tests with Miniflare?
๐Ÿ”Œ Multiple Workers ยท Miniflare
Fun, full-featured, fully-local simulator for Cloudflare Workers
9 Replies
MrBBot
MrBBotโ€ข2y ago
Hey! ๐Ÿ‘‹ Apologies for the delayed response. Are you trying to use clearImmediate inside your Worker? This isn't defined in the Workers sandbox, so will lead to the ReferenceErrorError you're seeing. Could you share the full stack trace for that error?
vas
vasOPโ€ข2y ago
Hi, I don't use clearImmediate in my worker. This is my worker code
import { Env } from './types/environment' export default { async fetch(request: Request, env: Env): Promise<Response> { const nextHeader = request.headers.has(env.NEXT_HEADER) console.log(green router is ${JSON.stringify(env)}) if (env.LIVE_ROUTER === 'blue') { return nextHeader ? await env.GREEN_ROUTER.fetch(request) : await env.BLUE_ROUTER.fetch(request) } else { return nextHeader ? await env.BLUE_ROUTER.fetch(request) : await env.GREEN_ROUTER.fetch(request) } } }
vas
vasOPโ€ข2y ago
And the test error is
No description
MrBBot
MrBBotโ€ข2y ago
Hmmm, could you share your Jest config? Are you using the miniflare test environment? You'll need to use the default Node environment, if you're constructing new Miniflare() instances in your tests.
vas
vasOPโ€ข2y ago
This is my jest config, I am using miniflare test environment
export default { preset: 'ts-jest/presets/default-esm', transform: { '^.+\\.tsx?$': [ 'ts-jest', { tsconfig: './tsconfig.json', useESM: true } ] }, testEnvironment: 'miniflare', testEnvironmentOptions: { scriptPath: 'dist/index.mjs', modules: true }, testTimeout: 10000 }
MrBBot
MrBBotโ€ข2y ago
Ah ok, are all your tests using new Miniflare(...) or do you have some files that just run imported Worker functions?
vas
vasOPโ€ข2y ago
yes, I have some tests using worker functions.
MrBBot
MrBBotโ€ข2y ago
Hmmm, ok, right at the top of bluegreenRouter.test.ts, could you try add...
/**
* @jest-environment node
*/
/**
* @jest-environment node
*/
vas
vasOPโ€ข2y ago
Thanks, that has fixed the timeout issue, still get a fetch error. I will work on it, thanks again.
Want results from more Discord servers?
Add your server