title: AWS SAM CLI Unit testing Lambda Functions NodeJS
body: <p>I am using the <a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html" rel="nofollow noreferrer">SAM CLI</a> 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 <code>test</code> directory and I can run the tests using the command <code>mocha --recursive</code>.</p>
  <p>The problem is I am using environment variables in the tests. My environemnt variables are defined in the   <code>template.yaml</code> file for the SAM CLI. It looks something like this:</p>
  <pre><code>  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
  </code></pre>
  <p>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 <code>sam local invoke</code> command which runs my lambda function locally using Docker.</p>
  <p>Is there anyway that anyone knows that the <code>sam local invoke</code> command can be used to run test cases locally instaed of running the lambda function? Or how to access my environment variables in the <code>template.yaml</code> file from test cases using mocha and chai?</p>