Skip to content

Commit a53ea15

Browse files
alexisfontainenovemberborn
authored andcommitted
Define environment variables to be injected in the test file processes
1 parent 626e58c commit a53ea15

File tree

10 files changed

+98
-8
lines changed

10 files changed

+98
-8
lines changed

docs/06-configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ To ignore files, prefix the pattern with an `!` (exclamation mark).
3030
"concurrency": 5,
3131
"failFast": true,
3232
"failWithoutAssertions": false,
33+
"environmentVariables": {
34+
"MY_ENVIRONMENT_VARIABLE": "some value"
35+
},
3336
"tap": true,
3437
"verbose": true,
3538
"compileEnhancements": false,
@@ -57,6 +60,7 @@ Arguments passed to the CLI will always take precedence over the CLI options con
5760
- `cache`: cache compiled test and helper files under `node_modules/.cache/ava`. If `false`, files are cached in a temporary directory instead
5861
- `failFast`: stop running further tests once a test fails
5962
- `failWithoutAssertions`: if `false`, does not fail a test if it doesn't run [assertions](./03-assertions.md)
63+
- `environmentVariables`: specifies environment variables to be made available to the tests. The environment variables defined here override the ones from `process.env`
6064
- `tap`: if `true`, enables the [TAP reporter](./05-command-line.md#tap-reporter)
6165
- `verbose`: if `true`, enables verbose output
6266
- `snapshotDir`: specifies a fixed location for storing snapshot files. Use this if your snapshots are ending up in the wrong location

lib/cli.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ exports.run = async () => { // eslint-disable-line complexity
163163
const babelPipeline = require('./babel-pipeline');
164164
const normalizeExtensions = require('./extensions');
165165
const {normalizeGlobs} = require('./globs');
166+
const validateEnvironmentVariables = require('./environment-variables');
166167

167168
let babelConfig = null;
168169
try {
@@ -171,6 +172,13 @@ exports.run = async () => { // eslint-disable-line complexity
171172
exit(error.message);
172173
}
173174

175+
let environmentVariables;
176+
try {
177+
environmentVariables = validateEnvironmentVariables(conf.environmentVariables);
178+
} catch (error) {
179+
exit(error.message);
180+
}
181+
174182
let extensions;
175183
try {
176184
extensions = normalizeExtensions(conf.extensions || [], babelConfig);
@@ -206,6 +214,7 @@ exports.run = async () => { // eslint-disable-line complexity
206214
failFast: conf.failFast,
207215
failWithoutAssertions: conf.failWithoutAssertions !== false,
208216
globs,
217+
environmentVariables,
209218
match,
210219
parallelRuns,
211220
projectDir,

lib/environment-variables.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
function validateEnvironmentVariables(environmentVariables) {
3+
if (!environmentVariables) {
4+
return {};
5+
}
6+
7+
for (const value of Object.values(environmentVariables)) {
8+
if (typeof value !== 'string') {
9+
throw new TypeError('The \'environmentVariables\' configuration must be an object containing string values.');
10+
}
11+
}
12+
13+
return environmentVariables;
14+
}
15+
16+
module.exports = validateEnvironmentVariables;

lib/fork.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ if (fs.realpathSync(__filename) !== __filename) {
99
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`.');
1010
}
1111

12-
const env = {NODE_ENV: 'test', ...process.env};
12+
// In case the test file imports a different AVA install,
13+
// the presence of this variable allows it to require this one instead
14+
const AVA_PATH = path.resolve(__dirname, '..');
1315

1416
// Ensure NODE_PATH paths are absolute
15-
if (env.NODE_PATH) {
16-
env.NODE_PATH = env.NODE_PATH
17+
let NODE_PATH;
18+
19+
if (process.env.NODE_PATH) {
20+
NODE_PATH = process.env.NODE_PATH
1721
.split(path.delimiter)
1822
.map(x => path.resolve(x))
1923
.join(path.delimiter);
2024
}
2125

22-
// In case the test file imports a different AVA install,
23-
// the presence of this variable allows it to require this one instead
24-
env.AVA_PATH = path.resolve(__dirname, '..');
25-
2626
const describeTTY = tty => ({
2727
colorDepth: tty.getColorDepth ? tty.getColorDepth() : undefined,
2828
columns: tty.columns || 80,
@@ -56,7 +56,7 @@ module.exports = (file, opts, execArgv) => {
5656
const subprocess = childProcess.fork(workerPath, args, {
5757
cwd: opts.projectDir,
5858
silent: true,
59-
env,
59+
env: {NODE_ENV: 'test', ...process.env, ...opts.environmentVariables, AVA_PATH, NODE_PATH},
6060
execArgv: execArgv || process.execArgv
6161
});
6262

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const config = require('.');
2+
3+
export default {
4+
environmentVariables: {[config.name]: config.value}
5+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = {
4+
name: 'MY_ENVIRONMENT_VARIABLE',
5+
value: 'some value'
6+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import test from '../../..';
2+
import {name, value} from '.';
3+
4+
test('works', t => {
5+
t.is(process.env[name], value);
6+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"ava": {
3+
"environmentVariables": {
4+
"SOME_INVALID_ENVIRONMENT_VARIABLE": {}
5+
}
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const {test} = require('tap');
3+
const figures = require('figures');
4+
const {execCli} = require('../helper/cli');
5+
const {name, value} = require('../fixture/environment-variables');
6+
7+
test('sets default environment variables from the config', t => {
8+
execCli(['test.js'], {dirname: 'fixture/environment-variables'}, (err, stdout) => {
9+
t.ifError(err);
10+
t.match(stdout, /1 test passed/);
11+
t.end();
12+
});
13+
});
14+
15+
test('overrides environment variables provided through the CLI', t => {
16+
const env = {[name]: `${value} (updated)`};
17+
18+
execCli(['test.js'], {dirname: 'fixture/environment-variables', env}, (err, stdout) => {
19+
t.ifError(err);
20+
t.match(stdout, /1 test passed/);
21+
t.end();
22+
});
23+
});
24+
25+
test('errors if environment variables are not string values', t => {
26+
execCli(['es2015.js'], {dirname: 'fixture/invalid-environment-variables'}, (err, stdout, stderr) => {
27+
t.ok(err);
28+
29+
let expectedOutput = '\n';
30+
expectedOutput += figures.cross + ' The \'environmentVariables\' configuration must be an object containing string values.';
31+
expectedOutput += '\n';
32+
33+
t.is(stderr, expectedOutput);
34+
t.end();
35+
});
36+
});

0 commit comments

Comments
 (0)