# vercel-serverless-tests

> A Helper to make tests with vercel-serverless-api package

Latest version **1.0.1** (published 2021-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install vercel-serverless-tests
pnpm add vercel-serverless-tests
yarn add vercel-serverless-tests
bun add vercel-serverless-tests
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-09-26 |
| First published | 2021-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Gastón Pereyra |
| Maintainers | gaston.pereyra |
| Keywords | vercel, serverless, tests |

## Links

- npm: https://www.npmjs.com/package/vercel-serverless-tests
- Repository: https://github.com/gastonpereyra/vercel-serverless-api-tests
- Homepage: https://github.com/gastonpereyra/vercel-serverless-api-tests#readme
- Issues: https://github.com/gastonpereyra/vercel-serverless-api-tests/issues?labels=bug&template=bug.md&title=[BUG]
- npm.io page: https://npm.io/package/vercel-serverless-tests

## Dependencies (1)

- [sinon](https://npm.io/package/sinon.md) ^11.1.2

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-09-26
- 1.0.0 — 2021-09-26

## README

# Vercel Serverless API Tests

## Code Quality Status
![Build Status](https://github.com/gastonpereyra/vercel-serverless-api-tests/workflows/Build%20Status/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/gastonpereyra/vercel-serverless-api-tests/badge.svg?branch=master)](https://coveralls.io/github/gastonpereyra/vercel-serverless-api-tests?branch=master)

![vercel-serverless-api-tests Banner](https://user-images.githubusercontent.com/39351850/134091893-712c5542-7d7c-4c51-8fba-3dd2ae1a8247.png)

## 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

```js

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

```js
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

```js
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](#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

```js
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](https://github.com/gastonpereyra/vercel-serverless-api-tests/issues/new?assignees=gastonpereyra&labels=bug&template=bug.md&title=[BUG])

## Idea :bulb:

[Tell me](https://github.com/gastonpereyra/vercel-serverless-api-tests/issues/new?assignees=gastonpereyra&labels=enhancement&title=%5BIDEA%5D+-)

---
_Source: https://npm.io/package/vercel-serverless-tests · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
