Skip to content

Commit

Permalink
Set NODE_ENV to 'test' in the test worker if not already set (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick committed Sep 17, 2017
1 parent 1df502d commit 6c3a6f2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ if (fs.realpathSync(__filename) !== __filename) {
console.warn('WARNING: `npm link ava` and the `--preserve-symlink` flag are incompatible. We have detected that AVA is linked via `npm link`, and that you are using either an early version of Node 6, or the `--preserve-symlink` flag. This breaks AVA. You should upgrade to Node 6.2.0+, avoid the `--preserve-symlink` flag, or avoid using `npm link ava`.');
}

let env = process.env;
const env = Object.assign({NODE_ENV: 'test'}, process.env);

// Ensure NODE_PATH paths are absolute
if (env.NODE_PATH) {
env = Object.assign({}, env);

env.NODE_PATH = env.NODE_PATH
.split(path.delimiter)
.map(x => path.resolve(x))
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,8 @@ t.true(a.test(b) || b === c)

Each test file is run in a separate Node.js process. This allows you to change the global state or overriding a built-in in one test file, without affecting another. It's also great for performance on modern multi-core processors, allowing multiple test files to execute in parallel.

For each process, `NODE_ENV` gets set to `'test'` if it was not set. This could change some ouput and/or the behaviour of some modules (e.g. express' final error handler does [*not* log errors when `NODE_ENV` is `'test'`](https://github.com/expressjs/express/blob/c087a45b9cc3eb69c777e260ee880758b6e03a40/lib/application.js#L630)). Is very convenient to set some defaults (change database, disable logging, etc…), but you shouldn't rely on it too much (if you check your [coverage](#code-coverage), you'll see it go down). As `NODE_ENV` is always set, `'NODE_ENV' in process.env` always is `false` when called from your tests.

## Tips

### Temp files
Expand Down
16 changes: 16 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,19 @@ test('--color enables formatting colors', t => {
t.end();
});
});

test('sets NODE_ENV to test when it is not set', t => {
execCli([path.join('fixture', 'node-env-test.js')], {env: {}}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, /1 passed/);
t.end();
});
});

test('doesn\'t set NODE_ENV when it is set', t => {
execCli([path.join('fixture', 'node-env-foo.js')], {env: {NODE_ENV: 'foo'}}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, /1 passed/);
t.end();
});
});
6 changes: 6 additions & 0 deletions test/fixture/node-env-foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from '../../';

test('NODE_ENV is foo', t => {
t.plan(1);
t.is(process.env.NODE_ENV, 'foo');
});
6 changes: 6 additions & 0 deletions test/fixture/node-env-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from '../../';

test('NODE_ENV is foo', t => {
t.plan(1);
t.is(process.env.NODE_ENV, 'foo');
});

0 comments on commit 6c3a6f2

Please sign in to comment.