Skip to content

Commit 95f284d

Browse files
committed
feat: load environment variables in local process.env
1 parent b638b2f commit 95f284d

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed

invokeLocal/googleInvokeLocal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class GoogleInvokeLocal {
2222
await this.validate();
2323
await this.setDefaults();
2424
await this.getDataAndContext();
25-
// TODO load env var in process.env
2625
},
2726
'invoke:local:invoke': async () => this.invokeLocal(),
2827
};

invokeLocal/googleInvokeLocal.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('GoogleInvokeLocal', () => {
4545
${'invokeLocalNodeJs'}
4646
${'loadFileInOption'}
4747
${'validateEventsProperty'}
48+
${'addEnvironmentVariablesToProcessEnv'}
4849
`('should declare $method method', ({ method }) => {
4950
expect(googleInvokeLocal[method]).toBeDefined();
5051
});

invokeLocal/lib/nodeJs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const chalk = require('chalk');
44
const path = require('path');
5+
const _ = require('lodash');
56

67
const tryToRequirePaths = (paths) => {
78
let loaded;
@@ -38,6 +39,8 @@ module.exports = {
3839
throw new Error(`Failed to load function "${functionObj.handler}" from the loaded file`);
3940
}
4041

42+
this.addEnvironmentVariablesToProcessEnv(functionObj);
43+
4144
function handleError(err) {
4245
let errorResult;
4346
if (err instanceof Error) {
@@ -91,4 +94,9 @@ module.exports = {
9194
return maybeThennable;
9295
});
9396
},
97+
98+
addEnvironmentVariablesToProcessEnv(functionObj) {
99+
const environmentVariables = this.provider.getConfiguredEnvironment(functionObj);
100+
_.merge(process.env, environmentVariables);
101+
},
94102
};

invokeLocal/lib/nodeJs.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ describe('invokeLocalNodeJs', () => {
1515
const context = {
1616
name: contextName,
1717
};
18+
const myVarValue = 'MY_VAR_VALUE';
1819
let serverless;
1920
let googleInvokeLocal;
2021

2122
beforeEach(() => {
2223
serverless = new Serverless();
2324
serverless.setProvider('google', new GoogleProvider(serverless));
25+
serverless.service.provider.environment = {
26+
MY_VAR: myVarValue,
27+
};
2428
serverless.serviceDir = path.join(process.cwd(), 'invokeLocal', 'lib', 'testMocks'); // To load the index.js of the mock folder
2529
serverless.cli.consoleLog = jest.fn();
2630
googleInvokeLocal = new GoogleInvokeLocal(serverless, {});
@@ -69,4 +73,13 @@ describe('invokeLocalNodeJs', () => {
6973
expect.stringContaining('"errorMessage": "ASYNC_ERROR"')
7074
);
7175
});
76+
77+
it('should give the environment variables to the handler', async () => {
78+
const functionConfig = {
79+
handler: 'envHandler',
80+
};
81+
await googleInvokeLocal.invokeLocalNodeJs(functionConfig, event, context);
82+
// eslint-disable-next-line no-console
83+
expect(console.log).toHaveBeenCalledWith(myVarValue);
84+
});
7285
});

invokeLocal/lib/testMocks/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ module.exports = {
2525
console.log('ASYNC_HANDLER');
2626
throw new Error('ASYNC_ERROR');
2727
},
28+
envHandler: async () => {
29+
// eslint-disable-next-line no-console
30+
console.log(process.env.MY_VAR);
31+
},
2832
};

test/serverless.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Serverless {
1919
}
2020
return this.functions[functionName];
2121
};
22+
this.service.provider = {};
2223
this.utils = {
2324
writeFileSync() {},
2425
readFileSync() {},

0 commit comments

Comments
 (0)