You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Below are the steps I've taken to separate .dev.vars for local development and unit testing. If there's a better way to achieve this, please let me know in the comments!
I prefer not to hardcode secure information like API keys directly into wrangler.json. I find it better to keep them in a .dev.vars file that isn't tracked by Git.
Taking this a step further, I wanted to use separate sets of variables for local development and unit testing. To achieve this, I created two .dev.vars files:
For local development: .dev.vars - Contains development-specific API keys and other variables.
MY_VARIABLE="development_value"
For unit testing: .dev.vars.test - Contains values specifically for testing. Since this is used for CI tests, it's safe to commit these values to Git.
MY_VARIABLE="test_value"
In vitest.config.mts, I configured it to load the test environment:
import{defineWorkersConfig}from'@cloudflare/vitest-pool-workers/config';exportdefaultdefineWorkersConfig({test: {poolOptions: {workers: {wrangler: {configPath: './wrangler.jsonc',environment: 'test',// add this line},},},},});
I added the following to wrangler.json to address a warning message in the console:
"env": {
"test": {}
}
I then used npm wrangler types to generate types from .dev.vars and updated src/index.ts:
import{env,createExecutionContext,waitOnExecutionContext,SELF}from'cloudflare:test';import{describe,it,expect}from'vitest';importworkerfrom'../src/index';constIncomingRequest=Request<unknown,IncomingRequestCfProperties>;describe('Hello World worker',()=>{it('responds with env.MY_VARIABLE (unit style)',async()=>{constrequest=newIncomingRequest('http://example.com');constctx=createExecutionContext();constresponse=awaitworker.fetch(request,env,ctx);awaitwaitOnExecutionContext(ctx);expect(awaitresponse.text()).toMatchInlineSnapshot(`"test_value"`);// This is the value of MY_VARIABLE in your .dev.vars.test file});it('responds with env.MY_VARIABLE (integration style)',async()=>{constresponse=awaitSELF.fetch('https://example.com');expect(awaitresponse.text()).toMatchInlineSnapshot(`"test_value"`);// This is the value of MY_VARIABLE in your .dev.vars.test file});});
This setup allows me to use different .dev.vars files for local development and unit testing.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Below are the steps I've taken to separate
.dev.vars
for local development and unit testing. If there's a better way to achieve this, please let me know in the comments!I prefer not to hardcode secure information like API keys directly into
wrangler.json
. I find it better to keep them in a.dev.vars
file that isn't tracked by Git.Taking this a step further, I wanted to use separate sets of variables for local development and unit testing. To achieve this, I created two
.dev.vars
files:For local development:
.dev.vars
- Contains development-specific API keys and other variables.For unit testing:
.dev.vars.test
- Contains values specifically for testing. Since this is used for CI tests, it's safe to commit these values to Git.In
vitest.config.mts
, I configured it to load the test environment:I added the following to
wrangler.json
to address a warning message in the console:I then used
npm wrangler types
to generate types from.dev.vars
and updatedsrc/index.ts
:Finally, I updated
test/index.spec.ts
:This setup allows me to use different
.dev.vars
files for local development and unit testing.Beta Was this translation helpful? Give feedback.
All reactions