Skip to content

Commit

Permalink
Merge pull request #2 from gastonpereyra/vsat-1-Crear-package
Browse files Browse the repository at this point in the history
Vsat 1 crear package
  • Loading branch information
gastonpereyra committed Sep 26, 2021
2 parents 9862999 + ed18d47 commit 31f14cd
Show file tree
Hide file tree
Showing 7 changed files with 698 additions and 27 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.0.0] - 2021-09-25
- Added Function for Context
- Added Function for It
- Added Function for Handler
177 changes: 170 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vercel-serverless-api-tests
# Vercel Serverless API Tests

## Code Quality Status
![Build Status](https://github.com/gastonpereyra/vercel-serverless-api-tests/workflows/Build%20Status/badge.svg)
Expand All @@ -12,20 +12,183 @@ A Helper to make tests with vercel-serverless-api package
## Installation

```
npm i vercel-serverless-api-tests
npm i vercel-serverless-api-tests --save-dev
```

## Methods
## Sinon
The package offers Sinon to help you to realize the tests

## Usage
```js

const { sinon } = require('vercel-serverless-api-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-api-tests');

/*
output:
*/
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-api-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-api-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])
Expand Down
28 changes: 28 additions & 0 deletions lib/helpers/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

module.exports = class Response {

status(statusCode) {
this.statusCode = statusCode;
return this;
}

setHeader(header, value) {

if(!this.headers)
this.headers = {};

this.headers[header] = value;
return this;
}

json(value) {
this.body = value;
return this;
}

send(value) {
this.body = value;
return this;
}
};

0 comments on commit 31f14cd

Please sign in to comment.