Hey guys, how have you all set your env vars for t...
# sst
c
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
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
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
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
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
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"