Lambda test
Simple Serverless tester, which works like this:
const lambdaTest = require('lambda-test');
const { getById } = require('../../routes/users.js');
describe('GET /users/{id}', () => {
it('should get user by id', async () => {
const response = await lambdaTest(getById)
.pathParameters({ id: 123 })
.run();
})
});
or much more sophisticated with Api Blueprint check
const assert = require('assert');
const { LambdaTest } = require('lambda-test');
const { updateById } = require('../../routes/users.js');
const tester = new LambdaTest('./apiBlueprint.apib');
describe('UPDATE /users/{id}', () => {
it('should get user by id', async () => {
const response = await tester.test(updateById, '/users/{id}', 'UPDATE', 200)
.pathParameters({ id: 123 })
.queryStringParameters({ fields: 'name' })
.headers({ Authorization: 'secret' })
.body({ name: 'John Doe' })
.verify();
assert.equal(response.body.name, 'John Doe');
});
});
API
Classes
- LambdaTest
- HandlerTester
LambdaTest
Kind: global class
new LambdaTest([blueprintFile])
Param | Type | Default | Description |
---|
[blueprintFile] | string | null | api blueprint |
lambdaTest._getBlueprint() ⇒ ApiBlueprint
Kind: instance method of LambdaTest
lambdaTest.test(handler, [routeOrStatus], [httpMethod], [statusCode]) ⇒ HandlerTester
Create test and checks for status code
when first parameter is API path, response is checked against api blueprint
Kind: instance method of LambdaTest
Param | Type | Default | Description |
---|
handler | function | | function to test |
[routeOrStatus] | number | string | 200 | route path for blueprint or status code |
[httpMethod] | string | null | http method to use |
[statusCode] | number | null |
| expected status code |
HandlerTester
Kind: global class
new HandlerTester(handler, [statusCode], [httpMethod], [route], [api])
Param | Type | Default |
---|
handler | function | |
[statusCode] | number | null |
|
[httpMethod] | string | null | null |
[route] | string | null | null |
[api] | ApiBlueprint | null |
|
handlerTester.queryStringParameters(query) ⇒ this
Sets query string
Kind: instance method of HandlerTester
Param | Type | Default | Description |
---|
query | Object | null |
| the query string |
handlerTester.body(body) ⇒ this
Sets request body
Kind: instance method of HandlerTester
Param | Type | Default | Description |
---|
body | Object | string |
| request body |
handlerTester.headers(headers) ⇒ this
Set request headers
Kind: instance method of HandlerTester
Param | Type | Default |
---|
headers | Object | null |
|
handlerTester.pathParameters(params) ⇒ this
Kind: instance method of HandlerTester
Param | Type | Default |
---|
params | Object | null |
|
handlerTester.run() ⇒ Promise.<Object>
Send request
Kind: instance method of HandlerTester
handlerTester.verify() ⇒ Promise.<Object>
Send request
Kind: instance method of HandlerTester