Home

Awesome

Serverless Mocha Plugin

Build Status

A Serverless Plugin for the Serverless Framework, which adds support for test driven development using mocha

THIS PLUGIN REQUIRES SERVERLESS V1!

More familiar with Jest? Use serverless-jest-plugin.

Introduction

This plugin does the following:

Installation

In your service root, run:

npm install --save-dev serverless-mocha-plugin

Add the plugin to serverless.yml:

plugins:
  - serverless-mocha-plugin

Usage

Creating functions

Functions (and associated tests) can be created using the command

sls create function -f functionName --handler handler

e.g.

sls create function -f myFunction --handler functions/myFunction/index.handler

creates a new function myFunction into serverless.yml with a code template for the handler in functions/myFunction/index.js and a Javascript function module.exports.handler as the entrypoint for the Lambda function. A test template is also created into test/myFunction.js. Optionally tests can be created to specific folder using --path or -p switch, e.g.

sls create function -f myFunction --handler functions/myFunction/index.handler --path tests

To create an http event for the lambda, add the --httpEvent parameter, i.e.

sls create function -f myFunction --handler functions/myFunction/index.handler --httpEvent "[httpVerb] [relativePath]"

e.g.

sls create function -f myFunction --handler functions/myFunction/index.handler --httpEvent "post myResource" --httpEvent "get myResource"

Creating tests

Functions can also be added manually using the mocha-create command

sls create test -f functionName

If you want to run the tests against the real Lambda functions, you can initLiveModule() instead of getWrapper(). You can also use --live flag and then you don't need to change your tests.

  let wrapped = mochaPlugin.initLiveModule('myLambdaFunctionName');

initLiveModule() and getWrapper() are helper methods to initialize lambda-wrapper, which is used under the hood. Both methods return wrapped function, which can be invoked with .run({}) method and takes event object as an argument.

Running tests

Tests can be run directly using the "invoke test" command. This also initializes the environment variables based on your serverless.yml file and the SERVERLESS_TEST_ROOT variable that defines the root for the code to be tested. If you're running the tests locally (rather than on live Lambdas, as described below), it will also set the IS_LOCAL to 'true' to match the behavior of sls invoke local.

sls invoke test [--stage stage] [--region region] [-t timeout] [-f function1] [-f function2] [...]

To use a mocha reporter (e.g. json), use the -R switch. Reporter options can be passed with the -O switch.

If no function names are passed to "invoke test", all tests are run from the test/ directory and subdirectories.

The default timeout for tests is 6 seconds. In case you need to apply a different timeout, that can be done in the test file using using .timeout(milliseconds) with the define, after, before or it -blocks. e.g.

  it('implement tests here', () => {
    ...
  }).timeout(xxx);

To run test in specific folder use --path or -p switch.

To run tests live against the actual deployed Lambdas, use the '--live' or '-l' switch. Please note that this will work only for tests created with module version 1.4 or higher.

To run tests e.g. against built artefacts that reside in some other directory, use the '--root' or '-r' switch. e.g.

  sls webpack -o testBuild
  sls invoke test --root testBuild
  rm -rf testBuild

Using own template for a test file

The templates to use for new function Files can be determined with the custom testTemplate configuration in serverless.yml

custom:
  serverless-mocha-plugin:
    testTemplate: templates/myTest.js

Currently, there are three variables available for use in the template:

If you'd like to get more information on the template engine, you check documentation of the EJS project.

Using own template for function file

The templates to use for new function Files can be determined with the custom functionTemplate configuration in serverless.yml

custom:
  serverless-mocha-plugin:
    functionTemplate: templates/myFunction.js

Running commands before / after tests

The plugin can be configured to run commands before / after the tests. This is done by setting preTestCommands and postTestCommands in the plugin configuration.

For example, start serverless-offline before tests and stop it after tests using the following configuration:

custom:
  serverless-mocha-plugin:
    preTestCommands: 
      - bash startOffline.sh
    postTestCommands:
      - bash stopOffline.sh

Sample startOffline.sh:

TMPFILE=/var/tmp/offline$$.log
if [ -f .offline.pid ]; then
    echo "Found file .offline.pid. Not starting."
    exit 1
fi

serverless offline 2>1 > $TMPFILE &
PID=$!
echo $PID > .offline.pid

while ! grep "server ready" $TMPFILE
do sleep 1; done

rm $TMPFILE

Note: The example relies on the output of the serverless offline command. If the start script is not working for you, replace "server ready" with the string serverless offline prints as soon as the server is ready and listening.

Sample stopOffline.sh

kill `cat .offline.pid`
rm .offline.pid

Usage with babel register

If you use mocha with babel compiler e.g. sls invoke test --compilers js:@babel/register
Babel configuration can be determined with the custom babelOptions configuration in serverless.yml

custom:
  serverless-mocha-plugin:
    babelOptions:
      presets: [["@babel/env", { "targets": { "node": "8.10" }, "shippedProposals": true, "useBuiltIns": "usage" }]]
      plugins:
        - ["@babel/plugin-transform-runtime"]

Release History (1.x)

License

Copyright (c) 2017 Nordcloud, licensed for users and contributors under MIT license. https://github.com/nordcloud/serverless-mocha-plugin/blob/master/LICENSE