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.xand 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 installto install the dependecies which creates yournode_modulesdirectory. After this has finished run the following command to install thedotenvmodule.Create a file named
.envin the root directory. Your root directory should now look like this.Now in your
.envfile create an environment variable like my sample one below.In your
template.ymlfile 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.jschange the code to the following which is reading in the environment varibaleTEST_ENV_VARand returning it.In your test handler located at
__tests__/unit/handlers/hello-from-lambda.test.jspaste the following code. It reads the environment variables from the.envyou created earlier using thedotenvmodule. The test will now pass if the value returned from thehelloFromLambdaHandleris equal to the valueHELLO_FROM_TESTwhich is the value we defined for the environment variableTEST_ENV_VARin the.envfile.Now run the test by running the command
npm run testin the root directory, it passes showing that we are reading from the.envfor test cases.To prove we are reading from the
template.ymlfile for production builds you can invoke a local test by runningsam buildto build the project and thensam local invokeall from the root directory.You can see we now get the value
Hello_From_Local_Invokewhich is the value we assigned to the environment variableTEST_ENV_VARin thetemplate.ymlfile.Hope this helps!