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.

The Upward Mobility Law Firm

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.

About UsPractice AreasBar PrepLegal Templates

Currently viewing this site in English

Switch to Dark Mode

SupportPGP Key
Privacy PolicyTerms of ServiceModern Slavery Statement

Copyright © 2020 Shook Law PLLC

This website is monitored with Fathom Analytics.