Laconia

Laconia

  • Getting Started
  • API
  • Help

›Guides

Introduction

  • Getting Started
  • Motivation
  • Core Concepts
  • Philosophy
  • Examples
  • FAQ
  • Support Me

Laconia vs. Other

  • Claudia
  • Express, Koa, Hapi, etc.
  • Middy
  • Serverless, SAM, Apex, etc.

Guides

  • Injecting Dependencies
  • Unit Testing
  • Adapting Events
  • Creating API Endpoints
  • Retrieving Secrets
  • Retrieving Config
  • Warming Up
  • Invoking Other Lambdas
  • Long Running Tasks
  • Creating Middleware

API Reference

  • API Reference: Intro
  • core
  • event
  • adapter
  • adapter-api
  • invoker
  • config
  • batch
  • middleware-lambda-warmer
  • middleware-serverless-plugin-warmup
Edit

Unit Testing

As your dependencies are made available via plain Object properties from LaconiaContext, you will be able to easily inject your dependencies without involving Laconia.

Lambda handler code:

const instances = ({ env }) => ({
  orderRepository: new DynamoDbOrderRepository(env.ORDER_TABLE_NAME)
});

exports.app = async (event, { orderRepository }) => {
  const order = event;
  await orderRepository.save(order);
};

exports.handler = laconia(exports.app).register(instances);

Unit test code:

const app = require("../src/place-order").app; // Import app, not handler

beforeEach(() => {
  orderRepository = {
    save: jest.fn().mockReturnValue(Promise.resolve()) // Creates a mock orderRepository
  };
});

it("should store order to order table", async () => {
  await app(event, { orderRepository }); // Plain JS object is injected

  expect(lc.orderRepository.save).toBeCalledWith(
    expect.objectContaining(order)
  );
});
← Injecting DependenciesAdapting Events →
Laconia
Docs
Getting StartedCore ConceptsAPI Reference
Community
Stack OverflowChat
More
TwitterGitHubStar
Copyright © 2022 Wisen Tanasa
Logo designed by Suzie Nam