Code Monkey home page Code Monkey logo

jest-environment-serverless's Introduction

jest-environment-serverless

Run your Serverless tests using Jest, quickly and easily! This project exposes a Lambda Wrapper to your tests in order to make it effortless and easy to test your lambdas.

Installing is as easy as:

npm install serverless jest jest-environment-serverless

Usage

Update your Jest configuration to set the testEnvironment to jest-environment-serverless:

package.json

{
  "name": "my-project",
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "testEnvironment": "jest-environment-serverless"
  }
}

Use Serverless in your tests:

describe('Hello Lambda', () => {
  let wrapper;

  beforeAll(async () => {
    wrapper = await LambdaWrapper.getWrapper('hello');
  });

  it('says hello', async () => {
    const event = { name: 'Bob' };
    const response = await wrapper.run(event);
    expect(response).toHaveProperty('message', 'Hi Bob!');
  });
});

Run your tests:

npm test

API

global.LambdaWrapper

This global variable provides convenient access to the lambda-wrapper module in order to simulate lambda executions for the given events and contexts, fully initialized with your Serverless configuration, providing a more accurate and thorough testing scenario than attempting to invoke and test your lambdas directly.

getWrapper()

When provided with a valid Serverless function name, getWrapper will return a configured, ready-to-run lambda wrapper.

With a simple serverless.yml

service: jest-test-suite
provider:
  name: aws
  runtime: nodejs8.10
  region: us-east-1
  environment:
    STAGE: prod-stage-test
functions:
  hello:
    handler: handler.hello
    environment:
      HELLO: "world ${self:provider.region}"

You can obtain a reference to the hello lambda in order to simulate various conditions and assert the expected behaviors:

describe('Hello Lambda', () => {
  let wrapper;

  beforeAll(async () => {
    wrapper = await LambdaWrapper.getWrapper('hello');
  });

  it('says hello', async () => {
    const event = { name: 'Bob' };
    const response = await wrapper.run(event);
    expect(response).toHaveProperty('message', 'Hi Bob!');
  });
});

global.ServerlessWrapper

This global variable provides access to a number of properties that can be safely ignored in nearly all but the most challenging test scenarios. These are primarily used by the LambdaWrapper in order to discover the service and configuration for a given lambda during testing.

Serverless

This provides direct access to the Serverless class.

rootDir

Exposes the root directory used by Serverless to load the configuration and plugins from. By default, this value is set to the cwd.

serverless

This provides direct access to the initialized Serverless instance in case it is needed to help assert or verify anything during testing. With this, you can access:

  • processed configuration
  • loaded services
  • loaded plugins
  • variables

You can also dynamically modify the configuration or services to assist while testing, without needing to manage multiple test projects or copying files to temporary directories.

getEnv()

Exposes the environment variables for the function defined in your Serverless Configuration. For example:

With a simple serverless.yml

service: jest-test-suite
provider:
  name: aws
  runtime: nodejs8.10
  region: us-east-1
  environment:
    STAGE: prod-stage-test
functions:
  hello:
    handler: handler.hello
    environment:
      HELLO: "world ${self:provider.region}"

Calling getEnv would return:

// This is the name of the lambda declared in the "functions" block in the
// serverless.yml
const envVars = ServerlessWrapper.getEnv('hello');
assert.deepEqual(envVars, { HELLO: 'world us-east-1' });

setEnv()

Updates process.env with the environment variables for the function defined in your Serverless Configuration. For example:

With a simple serverless.yml

service: jest-test-suite
provider:
  name: aws
  runtime: nodejs8.10
  region: us-east-1
  environment:
    STAGE: prod-stage-test
functions:
  hello:
    handler: handler.hello
    environment:
      HELLO: "world ${self:provider.region}"
  goodbye:
    handler: handler.goodbye
    environment:
      HELLO: "cruel world"
      GOODBYE: "world ${self:provider.region}"

Calling setEnv would return:

// All serverless env vars are initially loaded, but lambda env vars are
// not prioritized and may override one another
assert.deepEqual(process.env, {
  ...
  HELLO: 'cruel world',
  GOODBYE: 'world us-east-1',
  ...
});
// This is the name of the lambda declared in the "functions" block in the
// serverless.yml
const envVars = ServerlessWrapper.setEnv('hello');

// The "hello" lambda env vars have been loaded and take priority over all
// others
assert.deepEqual(process.env, {
  ...
  HELLO: 'world us-east-1',
  GOODBYE: 'world us-east-1',
  ...
});

Copyright (C) 2019 FireEye, Inc. All Rights Reserved. See LICENSE file.

jest-environment-serverless's People

Contributors

chrg1001 avatar dhm116 avatar dependabot[bot] avatar mend-for-github-com[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.