1.0.1 • Published 3 years ago
vercel-serverless-tests v1.0.1
Vercel Serverless API Tests
Code Quality Status
Description
A Helper to make tests with vercel-serverless-api package
Installation
npm i vercel-serverless-tests --save-dev
Sinon
The package offers Sinon to help you to realize the tests
const { sinon } = require('vercel-serverless-tests');
Tests
The package has 3 levels to help you:
- Handler
- It
- Context
Handler
In order to make easier test the Handler, offers the testHandler
function, this has a response dummy.
testHandler(request, response)
request
url
optional | stringmethod
optional | stringcookies
optional | objectheaders
optional | objectqueries
optional | objectpathIds
optional | objectbody
optional | object
response
status
optional | default: 200headers
optional | objectbody
optional | any
const VercelServerlessApiTests = require('vercel-serverless-tests');
const handler = require('../../api/test/get');
const ApiTest = new VercelServerlessApiTests(handler);
describe('Test API', () => {
context('When try the It Block', () => {
it('Should return status code 200 with message body and x-custom header', async () => {
await ApiTest.testHandler({
url: 'api/test/10',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
pathIds: { id: 10 },
body: { message: 'Try it!' }
},{
status: 201,
headers: { 'x-custom': true },
body: { message: 'Got It!' }
});
});
});
});
It
In order to try to make a Standard, an easy way to make an It block.
itTest(properties)
properties
description
optional | string | It description -only
optional | boolean | If you want to execute this only tests -skip
optional | string | If you want to skip the tests -request
optional | object | Request - Same as before -response
optional | object | Response - Same as before -before
optional | function | If want to do something before to trigger the handler -after
optional | function | If want to do something after to trigger the handler
const { sinon, ...VercelServerlessApiTests} = require('vercel-serverless-tests');
const handler = require('../../api/test/get');
const Model = require('../../models/test);
const ApiTest = new VercelServerlessApiTests(handler);
describe('Test API', () => {
context('When try the It Block', () => {
ApiTest.itTest({
description: 'Should return status code 200 with message body and x-custom header',
only: true,
request: {
url: 'api/test/10',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
pathIds: { id: 10 },
body: { message: 'Try it!' }
},
response: {
status: 201,
headers: { 'x-custom': true },
body: { message: 'Got It!' }
},
before: () => {
sinon.stub(Model.prototype, 'get').resolves([]);
},
after: () => {
sinon.assert.calledOnce(Model.prototype.get);
}
});
});
});
Context
To make multiple It blocks under a context
contextTest(description, tests, options)
description
required | string | Context descriptitiontests
required | Array ofproperties
(See It)options
optionalskip
optional | boolean | For skip whole contextonly
optional | boolean | For execute only this contextbefore
optional | function | Do somenthing before every testsbeforeEach
optional | function | Do somenthing before each testsafter
optional | function | Do somenthing after every testsafterEach
optional | function | Do somenthing after each tests
const { sinon, ...VercelServerlessApiTests} = require('vercel-serverless-tests');
const handler = require('../../api/test/get');
const Model = require('../../models/test);
const ApiTest = new VercelServerlessApiTests(handler);
describe('Test API', () => {
ApiTest.contextTest('When try the It Block', [
{
description: 'Should return status code 200 with message body and x-custom header',
request: {
url: 'api/test/10',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
pathIds: { id: 10 },
body: { message: 'Try it!' }
},
response: {
status: 201,
headers: { 'x-custom': true },
body: { message: 'Got It!' }
},
before: () => {
sinon.stub(Model.prototype, 'get').resolves([]);
},
after: () => {
sinon.assert.calledOnce(Model.prototype.get);
}
}
], {
only: true,
before: () => {
process.env.Secret = '100000';
},
beforeEach: () => {
sinon.stub(Model.prototype, 'save');
},
afterEach: () => {
sinon.assert.notCalled(Model.prototype, 'save');
},
after: () => {
process.env.Secret = undefined;
}
});