๐ 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
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?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)
}
}
}
And the test error is
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.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
}
Ah ok, are all your tests using
new Miniflare(...)
or do you have some files that just run imported Worker functions?yes, I have some tests using worker functions.
Hmmm, ok, right at the top of
bluegreenRouter.test.ts
, could you try add...
Thanks, that has fixed the timeout issue, still get a fetch error. I will work on it, thanks again.