https://serverless-stack.com/ logo
#sst
Title
# sst
c

colin

08/19/2021, 2:40 PM
Hey guys, how have you all set your env vars for the
sst test
command? It doesn’t seem that having a
.env.test
file injects the test environment vars into the test environment but I don’t want to overwrite the jest.config under the hood.
f

Frank

08/19/2021, 2:42 PM
Hey @colin,
.env.test
only injects env vars into the SST code.
Are you looking to refer to the env vars in ur test code?
c

colin

08/19/2021, 2:44 PM
ahh that makes sense, I’m basically wanting to “nullify” my env vars in the jest environment so I don’t have anything actually hitting data sources. Was just wondering if there was a less manual way than something like
process.env = {}
f

Frank

08/19/2021, 3:28 PM
I see ppl do this:
Copy code
beforeAll(() => {
  process.env = Object.assign(process.env, { MY_ENV: '...' });
});
I guess this is the best way:
Copy code
beforeAll(() => {
  process.env = {};
});
r

Ross Coundon

08/19/2021, 5:02 PM
Just a couple of things to avoid potential trip-hazards Make a copy of process.env and set it back the way it was when the jest suite finishes. Make sure sure that none of your files call dotenv.config() (or similar) at their top level, as you'll find that the process.env gets set when they're imported and the beforeAll approach has no effect
c

colin

08/20/2021, 5:34 PM
Its odd, I added this to see what would happen and I still have an
process.env
with real values which makes me thing something like @Ross Coundon mentioned is occurring higher up.
Copy code
beforeAll(() => {
    process.env = Object.assign(process.env, {})
    console.log('process.env: ', process.env) // has stuff
  })
I have two env files in my root one
.env
and one
.env.test
. I could have sworn tests used to get populated with the
.env.test
values… Im running the tests with this command:
npm run test -- --watch
which becomes
sst test "--watch"