O'Reilly logo

Building Serverless Web Applications by Diego Zanon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit testing Lambda functions

Since a Lambda function is defined in a common JavaScript file, you just need to set your testing tool to load this file and test the function locally. To simulate the input data that is set by the API Gateway or another trigger, you need to set the event variable of your test according to the expected input.

Let's perform the following steps to see how to unit test a Lambda function:

  1. First, let's create a new serverless project by running the following command:
      serverless create --template aws-nodejs --name testing
  1. Now, let's modify the serverless.yml file to the following:
       service: testing-service       provider:         name: aws         runtime: nodejs6.10       functions:         hello:           handler: functions/greetings.hello

 

  1. In this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required