EasyDev
EasyDev
CDCloudflare Developers
Created by EasyDev on 1/14/2025 in #workers-help
Is it possible to make Vitest code available both locally and in production?
import app from '../src/index'
import { env } from 'cloudflare:test'
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);
expect(await res.json()).toEqual({
hello: 'world',
var: 'your_variable',
})
})
import app from '../src/index'
import { env } from 'cloudflare:test'
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);
expect(await res.json()).toEqual({
hello: 'world',
var: 'your_variable',
})
})
I wrote a simple test. Do I always need to convert the / part to workers address or is there a way to do it in vitest?
5 replies
CDCloudflare Developers
Created by EasyDev on 1/11/2025 in #workers-help
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"]
}
6 replies