Skip to content

Commit

Permalink
Set NODE_ENV to to 'test' if not already set (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Seebauer authored and novemberborn committed Oct 1, 2017
1 parent 64b7755 commit 42e7c74
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
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
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.

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`.

This comment has been minimized.

Copy link
@OmgImAlexis

OmgImAlexis Oct 5, 2017

Note that 'NODE_ENV' in process.env will always be true.

Shouldn't this say "truthy" instead of "true" since it's a string not a boolean?

This comment has been minimized.

Copy link
@novemberborn

novemberborn Oct 10, 2017

Member

The in keyword results in a boolean.


## Tips

### Temp files
Expand Down
16 changes: 16 additions & 0 deletions test/cli.js
Expand Up @@ -810,3 +810,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
@@ -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
@@ -0,0 +1,6 @@
import test from '../../';

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

0 comments on commit 42e7c74

Please sign in to comment.