1.0.1 • Published 3 years ago

vercel-serverless-tests v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Vercel Serverless API Tests

Code Quality Status

Build Status Coverage Status

vercel-serverless-api-tests Banner

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 | string
      • method optional | string
      • cookies optional | object
      • headers optional | object
      • queries optional | object
      • pathIds optional | object
      • body optional | object
    • response
      • status optional | default: 200
      • headers optional | object
      • body 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 descriptition
    • tests required | Array of properties (See It)
    • options optional
      • skip optional | boolean | For skip whole context
      • only optional | boolean | For execute only this context
      • before optional | function | Do somenthing before every tests
      • beforeEach optional | function | Do somenthing before each tests
      • after optional | function | Do somenthing after every tests
      • afterEach 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;
        }
    });

Bug :bug:

Report Here

Idea :bulb:

Tell me