Home

Awesome

Description

Jest Integration Test for serverless-aws-lambda

Requirements

Installation

yarn add -D serverless-aws-lambda-jest
# or
npm install -D serverless-aws-lambda-jest

Recommendations

Some recommendations to speed up jest integration tests by avoiding double bundeling of your Lambdas by Jest and serverless-aws-lambda.


Usage

use serverless-aws-lambda's defineConfig to import this plugin

// config.js
const { defineConfig } = require("serverless-aws-lambda/defineConfig");
const { jestPlugin } = require("serverless-aws-lambda-jest");

module.exports = defineConfig({
  plugins: [
    jestPlugin({
      configFile: "./jest.it.config.js",
      oneshot: false,
      coverage: {
        outDir: "./coverage/",
        json: true,
        badge: true,
        threshold: 60,
      },
    }),
  ],
});

Testing async events

The plugin exposes multiple global functions to wait for handler async invokation responses.

Simple example of implementation for a SQS event:

import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({
  region: "eu-west-3",
  endpoint: `http://localhost:${process.env.LOCAL_PORT}/@sqs`,
});
const cmd = new SendMessageCommand({
  QueueUrl: "MyQueueName",
  MessageBody: JSON.stringify({
    hello: {
      message: "world",
      firstVisit: true,
    },
  }),
});
test("Single SQS", async () => {
  const res = await client.send(cmd);
  const handlerResponse = await sqsResponse(res.MessageId);
  expect(handlerResponse.success).toBe(true);
});

see more examples.

Coverage

Supported events

Notes