-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Would be nice if mocha sets the NODE_ENV variable to 'test' #185
Comments
i dont want to force it on anyone, but you can easily just do |
fair enough! |
One of the advantages of not having this set by default is when writing tests for apps that run in many different environments. For example, one app that I develop has an additional set of tests that it runs on a staging server (NODE_ENV=staging) when it has access to production quality data. As the DB config and the logging transports all depend on that env variable, mocha overwriting it would cause some issues. -1 on this. |
Just to clarify I meant an implementation where NODE_ENV would default to test but could be overwritten. However setting it through a Makefile is good enough for me. I just don't want to type |
for sure. i always have |
A possible workaround, assuming you're running mocha from the same terminal every time, would be: NODE_ENV will persist through every command that runs in that window. |
Note: I made a dumb plugin for that. It's a dead simple one-line module, but doing the job in If a |
+1 for a --env option. Or even a --define option that would let me set a global variable. |
nah. why add |
Because then when I want to run one test, I have to type:
instead of putting
and I a very lazy. ;) |
export the var in your shell config would work the same as mocha.opts |
Yes, although then I need to make sure everyone else on my team exports it in their shell while they are running tests, and not export it while they are not running tests, which is a bit painful. In my case I'm using it to suppress logging (since they mess up the pretty output from Mocha). So it's nothing essential. |
Oh yeah it seems like the perfect way to do it, let's put NODE_ENV=test in The only sane way is using mocha.opts, use case has been pretty clearly Given that conclusion, the real question is a dumb plug-in enough or a
|
Note the usual case is about fixtures, deleting local database because of |
Because it's not portable, and especially because CLI arguments can be put On 6 September 2014 00:20, Travis Jeffery notifications@github.com wrote:
Nicolas Chambrier, aka naholyr Blog : http://naholyr.fr |
No, Mocha doesn't need this in its core. There are plenty of other ways to get this done. Some options include, but are not limited to:
The root cause of this, of course, is that your application needs an environment variable in the first place. Could it do without? What about a config or run control file? What about multiple environments? Does your app need to run in multiple environments? Can you standardize it, and spin up Vagrant machines instead? What about your team? Is it Mocha's problem that your team doesn't know to set the proper environment variables? Is there any reason this hasn't been automated? |
As far as I know |
It would be nice to have a simple |
I've since discovered |
Sure but then the same command will not work on *nix systems, if you want to put it in your |
@felixfbecker @tswaters have you checked env-test? |
That only works for NODE_ENV though and relying on values of NODE_ENV is bad practice. See http://12factor.net/config |
I know this is super super old, but cross-env (https://www.npmjs.com/package/cross-env) takes care of the windows issue. |
@halkeye Good tip, thanks. |
I am one of those people that always runs raw |
Also you can use https://www.npmjs.com/package/env-test and add requires env-test to your mocha.opts too There's lots of options out there. |
But should someone that doesn't know that Mocha itself should be the one championing idempotency. The |
Since this still seems to get a lot of traffic I thought I would throw out a relatively simply solution for people like me who wish there was a
|
Per @jasonk, the same approach works in TypeScript: // In mocha.opts
--require test/mocha.env
...
// In mocha.env.ts
process.env.NODE_ENV = 'test' |
Guys, cannot get what do you mean by "There are plenty of other ways to get this done" #185 (comment). |
Okay, I had to do this way (within package.json script section): |
Another idea: if you use mocha's config JavaScript file you can just put it there: process.env.NODE_ENV = 'test';
module.exports = {
require: 'ts-node/register/transpile-only',
extension: ['ts'],
watchExtensions: ['ts'],
spec: ['test/**/*.test.ts'],
} |
since node 20.6.0: use "node-option" to natively load an env file. {
"ui": "bdd",
"node-option": ["import=tsx", "env-file=.env.test"],
} |
Pretty handy if different middlewares / configs etc. in Express or such is needed when running the test suite.
The text was updated successfully, but these errors were encountered: