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');
// in project root
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');
});
});
Kind: global class
Param | Type | Default | Description |
---|---|---|---|
[blueprintFile] | string |
null |
api blueprint |
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 |
Kind: global class
- HandlerTester
- new HandlerTester(handler, [statusCode], [httpMethod], [route], [api])
- .queryStringParameters(query) ⇒
this
- .body(body) ⇒
this
- .headers(headers) ⇒
this
- .pathParameters(params) ⇒
this
- .run() ⇒
Promise.<Object>
- .verify() ⇒
Promise.<Object>
Param | Type | Default |
---|---|---|
handler | function |
|
[statusCode] | number | null |
|
[httpMethod] | string | null |
null |
[route] | string | null |
null |
[api] | ApiBlueprint | null |
|
Sets query string
Kind: instance method of HandlerTester
Param | Type | Default | Description |
---|---|---|---|
query | Object | null |
|
the query string |
Sets request body
Kind: instance method of HandlerTester
Param | Type | Default | Description |
---|---|---|---|
body | Object | string |
|
request body |
Set request headers
Kind: instance method of HandlerTester
Param | Type | Default |
---|---|---|
headers | Object | null |
|
Kind: instance method of HandlerTester
Param | Type | Default |
---|---|---|
params | Object | null |
|
Send request
Kind: instance method of HandlerTester
Send request
Kind: instance method of HandlerTester