-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define environment variables to be injected in the test file processes
- Loading branch information
1 parent
626e58c
commit a53ea15
Showing
10 changed files
with
98 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
function validateEnvironmentVariables(environmentVariables) { | ||
if (!environmentVariables) { | ||
return {}; | ||
} | ||
|
||
for (const value of Object.values(environmentVariables)) { | ||
if (typeof value !== 'string') { | ||
throw new TypeError('The \'environmentVariables\' configuration must be an object containing string values.'); | ||
} | ||
} | ||
|
||
return environmentVariables; | ||
} | ||
|
||
module.exports = validateEnvironmentVariables; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const config = require('.'); | ||
|
||
export default { | ||
environmentVariables: {[config.name]: config.value} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
name: 'MY_ENVIRONMENT_VARIABLE', | ||
value: 'some value' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import test from '../../..'; | ||
import {name, value} from '.'; | ||
|
||
test('works', t => { | ||
t.is(process.env[name], value); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ava": { | ||
"environmentVariables": { | ||
"SOME_INVALID_ENVIRONMENT_VARIABLE": {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
const {test} = require('tap'); | ||
const figures = require('figures'); | ||
const {execCli} = require('../helper/cli'); | ||
const {name, value} = require('../fixture/environment-variables'); | ||
|
||
test('sets default environment variables from the config', t => { | ||
execCli(['test.js'], {dirname: 'fixture/environment-variables'}, (err, stdout) => { | ||
t.ifError(err); | ||
t.match(stdout, /1 test passed/); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('overrides environment variables provided through the CLI', t => { | ||
const env = {[name]: `${value} (updated)`}; | ||
|
||
execCli(['test.js'], {dirname: 'fixture/environment-variables', env}, (err, stdout) => { | ||
t.ifError(err); | ||
t.match(stdout, /1 test passed/); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
test('errors if environment variables are not string values', t => { | ||
execCli(['es2015.js'], {dirname: 'fixture/invalid-environment-variables'}, (err, stdout, stderr) => { | ||
t.ok(err); | ||
|
||
let expectedOutput = '\n'; | ||
expectedOutput += figures.cross + ' The \'environmentVariables\' configuration must be an object containing string values.'; | ||
expectedOutput += '\n'; | ||
|
||
t.is(stderr, expectedOutput); | ||
t.end(); | ||
}); | ||
}); |