Skip to content
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

Set NODE_ENV to to 'test' if not already set #1523

Merged
merged 4 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

## 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 test', t => {
t.plan(1);
t.is(process.env.NODE_ENV, 'test');
});