Black Lives Matter

Neon Law

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.

Neon Law

All content presented herein is for informational purposes only. Nothing should be construed as legal advice. Transmission and receipt of this information is not intended to create and does not constitute, an attorney-client relationship with lawyers on this platform. There is no expectation of attorney-client privilege or confidentiality of anything you may communicate to us in this forum. Do not act upon any information presented without seeking professional counsel.

Currently viewing this site in English

SupportPro BonoUpward Mobility
Join our e-mail list.
Sign up for our e-mail list, the Neon Law Monthly.

Copyright © 2021 Shook Law PLLC

this website was crafted by Nisar