5.0.0 • Published 2 years ago
@janiscommerce/event-listener-test v5.0.0
Event Listener Test
Installation
npm install --save-dev @janiscommerce/event-listener-testAPI
EventListenerTest(handler, rules, extraParameters)
handler: <function> Required. The Serverless handler (a function that receives a serverlesseventand handles the listener request)rules: <Rule> Required. An array of rules to define what needs to be testedextraParameters: <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 receivessinonas the first argument.
after: <function> A function to be called after every test case is executed. It receivessinonas 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'struethe 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 sessionclientgetter. Only works ifsessionis set.responseCode: <number> The response http status code expected. Defaults to200.before: <function> A function to be called before this test case is executed. It receivessinonas the first argument.after: <function> A function to be called after this test case is executed. It receivessinonas 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);
}
}
]);