@janiscommerce/event-listener-test

5.0.0 • Public • Published

Event Listener Test

Build Status Coverage Status npm version

Installation

npm install --save-dev @janiscommerce/event-listener-test

API

EventListenerTest(handler, rules, extraParameters)

  • handler: <function> Required. The Serverless handler (a function that receives a serverless event and handles the listener request)
  • rules: <[Rule]> Required. An array of rules to define what needs to be tested
  • extraParameters: <object> An object of key-value properties that configure the test execution. It accepts the following properties:
    • before: <function> A function to be called before any test case is executed. It receives sinon as the first argument.
    • after: <function> A function to be called after every test case is executed. It receives sinon as the first argument.
    • printResponse: <boolean> Indicates if every test case response should be printed in the console (good for debugging). (Also available within a rule for more granularity)

Rule

A rule is an object that defines a test case. It has the following properties:

  • description: <string> Required. The test case description.
  • only: <boolean> If it's set to true, only this rule will be executed. Useful to debug when a test fails.
  • event: <object> Required. The JANIS event to test.
  • session: <boolean|object> Indicates if the test should inject a session. If it's true the a default session is injected. If it's an object, it's injected as a session.
  • client: <object> Used to mock the service client, injecting it in the session client getter. Only works if session is set.
  • responseCode: <number> The response http status code expected. Defaults to 200.
  • before: <function> A function to be called before this test case is executed. It receives sinon as the first argument.
  • after: <function> A function to be called after this test case is executed. It receives sinon as the first argument.
  • printResponse: <boolean> Indicates if this test case response should be printed in the console (good for debugging).

Examples

'use strict';

const EventListenerTest = require('@janiscommerce/event-listener-test');

const MyServerlessHandler = require('./handler');
const MyModel = require('./model');

EventListenerTest(MyServerlessHandler, [
	{
		description: 'It should return a 200 and do nothing',
		event: {
			service: 'demo',
			entity: 'someEntity',
			event: 'somethingHappened'
		}
	},
	{
		description: 'It fail with a 500 status code if event is errorHappened',
		event: {
			service: 'demo',
			entity: 'someEntity',
			event: 'errorHappened'
		},
		responseCode: 500
	},
	{
		description: 'It should update a record and return a 200',
		event: {
			service: 'demo',
			entity: 'someEntity',
			event: 'somethingHappened'
		},
		before: sinon => {
			sinon.stub(MyModel.prototype, 'update')
				.returns(true);
		},
		after: sinon => {
			sinon.assert.calledOnce(MyModel.prototype.update);
		}
	}
]);

Readme

Keywords

none

Package Sidebar

Install

npm i @janiscommerce/event-listener-test

Weekly Downloads

550

Version

5.0.0

License

ISC

Unpacked Size

12 kB

Total Files

6

Last publish

Collaborators

  • janiscommerce