env. return undefined in tests

I'm experiencing an issue with environment variables in Cloudflare Workers tests. When running tests using the Vitest configuration, all environment variables accessed through the env object are returning undefined (happens only on cloudfare deployment, locally its as expected)
import { env, createExecutionContext } from 'cloudflare:test';

const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

declare module "cloudflare:test" {
interface ProvidedEnv extends Env {}
}
describe('Testing environment', () => {
it('should have access to environment variables', async () => {
// Trying to use env variables for authorization
const headers = {
Authorization: `Bearer ${env.BYPASS_AUTH_TOKEN}`,
};

// Create the request
const request = new IncomingRequest(url, {
method: "GET",
headers,
});

// env.BYPASS_AUTH_TOKEN is undefined, making the Authorization header invalid
console.log(headers.Authorization); // "Bearer undefined"

// All other env variables also return undefined
console.log(env.API_KEY); // undefined
});
});
import { env, createExecutionContext } from 'cloudflare:test';

const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

declare module "cloudflare:test" {
interface ProvidedEnv extends Env {}
}
describe('Testing environment', () => {
it('should have access to environment variables', async () => {
// Trying to use env variables for authorization
const headers = {
Authorization: `Bearer ${env.BYPASS_AUTH_TOKEN}`,
};

// Create the request
const request = new IncomingRequest(url, {
method: "GET",
headers,
});

// env.BYPASS_AUTH_TOKEN is undefined, making the Authorization header invalid
console.log(headers.Authorization); // "Bearer undefined"

// All other env variables also return undefined
console.log(env.API_KEY); // undefined
});
});
I have double checked that the variable exists under variables and secrets. i was unable to access not only this specific variable but all secret variables in the test env
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?