Setting up Environment Variables in Mocha
When running TypeScript tests using the great mocha library, you may want to set environment variables that are only valid for your test process.
https://github.com/mochajs/mocha/issues/185#issuecomment-349700266
This comment on GitHub details on how to do just that.
In your mocha.opts which is referred to in your package.json
file for yarn
test
.
--require test/mocha.env
The above will require a mocha.env
file to be sourced before your tests are
ran. In mocha.env.ts
referenced by the options:
ts process.env.NODE_ENV = 'test';
From here, the NODE_ENV
environment variable will be set to test
, you can
then use the value of this environment variable as a global constant to
determine the logic of your application.