Skip to content

Commit 42e7c74

Browse files
P-Seebauernovemberborn
authored andcommitted
Set NODE_ENV to to 'test' if not already set (#1523)
1 parent 64b7755 commit 42e7c74

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

lib/fork.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ if (fs.realpathSync(__filename) !== __filename) {
1010
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`.');
1111
}
1212

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

1515
// Ensure NODE_PATH paths are absolute
1616
if (env.NODE_PATH) {
17-
env = Object.assign({}, env);
18-
1917
env.NODE_PATH = env.NODE_PATH
2018
.split(path.delimiter)
2119
.map(x => path.resolve(x))

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,8 @@ t.true(a.test(b) || b === c)
11271127

11281128
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.
11291129

1130+
AVA will set `process.env.NODE_ENV` to `test`, unless the `NODE_ENV` environment variable has been set. This is useful if the code you're testing has test defaults (for example when picking what database to connect to, or environment-specific Babel options). It may cause your code or its dependencies to behave differently though. Note that `'NODE_ENV' in process.env` will always be `true`.
1131+
11301132
## Tips
11311133

11321134
### Temp files

test/cli.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,3 +810,19 @@ test('--color enables formatting colors', t => {
810810
t.end();
811811
});
812812
});
813+
814+
test('sets NODE_ENV to test when it is not set', t => {
815+
execCli([path.join('fixture', 'node-env-test.js')], {env: {}}, (err, stdout, stderr) => {
816+
t.ifError(err);
817+
t.match(stderr, /1 passed/);
818+
t.end();
819+
});
820+
});
821+
822+
test('doesn\'t set NODE_ENV when it is set', t => {
823+
execCli([path.join('fixture', 'node-env-foo.js')], {env: {NODE_ENV: 'foo'}}, (err, stdout, stderr) => {
824+
t.ifError(err);
825+
t.match(stderr, /1 passed/);
826+
t.end();
827+
});
828+
});

test/fixture/node-env-foo.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../';
2+
3+
test('NODE_ENV is foo', t => {
4+
t.plan(1);
5+
t.is(process.env.NODE_ENV, 'foo');
6+
});

test/fixture/node-env-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../';
2+
3+
test('NODE_ENV is test', t => {
4+
t.plan(1);
5+
t.is(process.env.NODE_ENV, 'test');
6+
});

0 commit comments

Comments
 (0)