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?
4 Replies
Harry
Harry4d ago
For each test run vitest spins up a local instance of the worker and makes the request against that. What’s the reason for wanting to test “production” too?
EasyDev
EasyDevOP4d ago
I am new to creating a serverless. This is because I anticipate that there may be different issues when running on a real cloudflare workers server than in my local environment.
Harry
Harry4d ago
There aren’t loads of differences between Wrangler and actually running on the edge, there are some but in my experience I haven’t needed to test the specific cases of them separately. If you wanted to, you could write another test that actually calls the https://<yourworker>.workers.dev route which tests against production, that’s more of an end to end test than a unit test but might give you the confidence you’re looking for.
EasyDev
EasyDevOP4d ago
Thanks for the advice!

Did you find this page helpful?