Cannot find module 'cloudflare:test' or its corresponding type declarations

I created a project with the bun hono template and installed @Cloudflare/vitest-pool-workers. Afterwards, the test code was not able to find the module.
// test/index.test.ts
import app from '../src/index'
import { env } from 'cloudflare:test' // << not found error
import { expect, describe, it } from 'vitest'

describe('Test the application', () => {
it('Should return 200 response', async () => {
const res = await app.request('/', {}, env)
expect(res.status).toBe(200)
})
it('should return 404 for unknown routes', async () => {
const res = await app.request('/unknown', {});
expect(res.status).toBe(404);
})
// test/index.test.ts
import app from '../src/index'
import { env } from 'cloudflare:test' // << not found error
import { expect, describe, it } from 'vitest'

describe('Test the application', () => {
it('Should return 200 response', async () => {
const res = await app.request('/', {}, env)
expect(res.status).toBe(200)
})
it('should return 404 for unknown routes', async () => {
const res = await app.request('/unknown', {});
expect(res.status).toBe(404);
})
// package.json
{
"name": "my-app",
"scripts": {
"dev": "wrangler dev",
"deploy": "wrangler deploy --minify"
},
"dependencies": {
"hono": "^4.6.16"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.6.0",
"@cloudflare/workers-types": "^4.20250109.0",
"vitest": "2.1.8"
}
}
// package.json
{
"name": "my-app",
"scripts": {
"dev": "wrangler dev",
"deploy": "wrangler deploy --minify"
},
"dependencies": {
"hono": "^4.6.16"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.6.0",
"@cloudflare/workers-types": "^4.20250109.0",
"vitest": "2.1.8"
}
}
// vitest.config.ts
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';

export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
},
},
},
});
// vitest.config.ts
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';

export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
},
},
},
});
// test/tsconfig.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"moduleResolution": "bundler",
"types": [
"@cloudflare/workers-types/experimental",
"@cloudflare/vitest-pool-workers"
]
},
"include": ["./**/*.ts", "../src/env.d.ts"]
}
// test/tsconfig.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"moduleResolution": "bundler",
"types": [
"@cloudflare/workers-types/experimental",
"@cloudflare/vitest-pool-workers"
]
},
"include": ["./**/*.ts", "../src/env.d.ts"]
}
4 Replies
James
James2w ago
What does your root tsconfig look like? Can you share a repo if possible?
EasyDev
EasyDevOP2w ago
https://github.com/EasyDevv/bun-hono-cloudflare-vitests
bun create hono@lastest my-app
cd my-app
bun install [email protected] --dev --exact
bun install @cloudflare/vitest-pool-workers --dev
bun create hono@lastest my-app
cd my-app
bun install [email protected] --dev --exact
bun install @cloudflare/vitest-pool-workers --dev
files I added - ./vitest.config.ts - ./tests/tsconfig.json - ./tests/indext.test.ts Above are the repositories and the commands I used. Adding @Cloudflare/vitest-pool-workers to the root tsconfig.json types made the error go away, but I don't know if that's the right way to do it.
GitHub
GitHub - EasyDevv/bun-hono-cloudflare-vitests
Contribute to EasyDevv/bun-hono-cloudflare-vitests development by creating an account on GitHub.
James
James2w ago
Add the following to your root tsconfig.json:
"include": ["src/**/*.ts"],
"exclude": ["test"]
"include": ["src/**/*.ts"],
"exclude": ["test"]
Here's an example one of my projects where this is setup and working, if it helps: https://github.com/Cherry/ShareX-R2-Cloudflare-Workers/tree/main
EasyDev
EasyDevOP2w ago
Thank you !

Did you find this page helpful?