I am using the SAM CLI to test and deploy AWS Lambda functions. I am trying to run unit tests using Mocha and Chai in NodeJS. The tests are located in the test
directory and I can run the tests using the command mocha --recursive
.
The problem is I am using environment variables in the tests. My environemnt variables are defined in the template.yaml
file for the SAM CLI. It looks something like this:
RefreshFunction:
Type: AWS::Serverless::Function
Properties:
Handler: src/handlers/index.handler
Runtime: nodejs12.x
MemorySize: 128
Timeout: 100
Environment:
Variables:
APP_ID: A
CLIENT_ID: B
CLIENT_SECRET: C
AWS_REGION_DB: D
They are defined here as when I deploy my function they automatically go with it onto AWS too. It also allows me to use the sam local invoke
command which runs my lambda function locally using Docker.
Is there anyway that anyone knows that the sam local invoke
command can be used to run test cases locally instaed of running the lambda function? Or how to access my environment variables in the template.yaml
file from test cases using mocha and chai?
Decided to update this answer as a few people have bookmarked this question.
To get the same setup I have to start with run
sam init
, select theAWS Quick Start Templates
, the run time isnodejs12.x
and the application template to use isQuick Start: From Scratch
.After this step you should have a directory structure that looks like this below in your sam application.
Run an
npm install
to install the dependecies which creates yournode_modules
directory. After this has finished run the following command to install thedotenv
module.Create a file named
.env
in the root directory. Your root directory should now look like this.Now in your
.env
file create an environment variable like my sample one below.In your
template.yml
file also define an environment variable of the same name but give it a different value so we can see when the two different locations are being read in the next step.In the handler file located at
src/handlers/hello-from-lambda.js
change the code to the following which is reading in the environment varibaleTEST_ENV_VAR
and returning it.In your test handler located at
__tests__/unit/handlers/hello-from-lambda.test.js
paste the following code. It reads the environment variables from the.env
you created earlier using thedotenv
module. The test will now pass if the value returned from thehelloFromLambdaHandler
is equal to the valueHELLO_FROM_TEST
which is the value we defined for the environment variableTEST_ENV_VAR
in the.env
file.Now run the test by running the command
npm run test
in the root directory, it passes showing that we are reading from the.env
for test cases.To prove we are reading from the
template.yml
file for production builds you can invoke a local test by runningsam build
to build the project and thensam local invoke
all from the root directory.You can see we now get the value
Hello_From_Local_Invoke
which is the value we assigned to the environment variableTEST_ENV_VAR
in thetemplate.yml
file.Hope this helps!