Skip to content

Commit e97c0ee

Browse files
authored
Merge pull request #94 from shmargum/add-tests
Add tests
2 parents 69f8110 + 5e9c694 commit e97c0ee

File tree

6 files changed

+2312
-54
lines changed

6 files changed

+2312
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
*.swp

lib/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function haveBabelrc(functionsDir) {
3131

3232
function webpackConfig(dir, additionalConfig) {
3333
var config = conf.load();
34-
var envConfig = config.build.environment || config.build.Environment || {};
34+
var envConfig = conf.loadContext(config).environment;
3535
var babelOpts = { cacheDirectory: true };
3636
if (!haveBabelrc(dir)) {
3737
babelOpts.presets = [

lib/config.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@ exports.load = function() {
1313

1414
return toml.parse(fs.readFileSync(configPath));
1515
};
16+
17+
exports.loadContext = function(config) {
18+
var buildConfig = config.build;
19+
var contextConfig = (
20+
process.env.CONTEXT &&
21+
config.context &&
22+
config.context[process.env.CONTEXT]
23+
) || {};
24+
var branchConfig = (
25+
process.env.BRANCH &&
26+
config.context &&
27+
config.context[process.env.BRANCH]
28+
) || {};
29+
var buildEnv = buildConfig.environment || buildConfig.Environment || {};
30+
var contextEnv = contextConfig.environment || contextConfig.Environment || {};
31+
var branchEnv = branchConfig.environment || branchConfig.Environment || {};
32+
return {
33+
...buildConfig,
34+
...contextConfig,
35+
...branchConfig,
36+
environment: {
37+
...buildEnv,
38+
...contextEnv,
39+
...branchEnv,
40+
}
41+
};
42+
};

lib/config.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var conf = require('./config');
2+
3+
beforeAll(function() {
4+
process.env.CONTEXT = 'branch-deploy';
5+
process.env.BRANCH = 'foo-branch';
6+
});
7+
8+
describe('loadContext', function() {
9+
it('should merge in context config', function() {
10+
var config = {
11+
build: {
12+
publish: '/default',
13+
environment: {
14+
SOME_VAR: true,
15+
},
16+
},
17+
context: {
18+
'branch-deploy': {
19+
publish: '/branch-deploy',
20+
environment: {
21+
SOME_VAR: false,
22+
},
23+
},
24+
'foo-branch': {
25+
publish: '/foo-branch',
26+
environment: {
27+
SOME_OTHER_VAR: 10,
28+
},
29+
},
30+
},
31+
};
32+
var contextConfig = conf.loadContext(config);
33+
expect(contextConfig).toEqual({
34+
publish: '/foo-branch',
35+
environment: {
36+
SOME_VAR: false,
37+
SOME_OTHER_VAR: 10,
38+
},
39+
});
40+
});
41+
});

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"netlify-lambda": "bin/cmd.js"
99
},
1010
"scripts": {
11-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"test": "jest"
1212
},
1313
"author": "Mathias Biilmann Christensen",
1414
"license": "MIT",
@@ -34,5 +34,8 @@
3434
"toml": "^2.3.3",
3535
"webpack": "^4.17.1",
3636
"webpack-merge": "^4.1.4"
37+
},
38+
"devDependencies": {
39+
"jest": "^23.6.0"
3740
}
3841
}

0 commit comments

Comments
 (0)