{"id":13473343,"url":"https://github.com/gastonpereyra/vercel-serverless-api-tests","last_synced_at":"2025-04-14T01:31:10.045Z","repository":{"id":46782366,"uuid":"410280802","full_name":"gastonpereyra/vercel-serverless-api-tests","owner":"gastonpereyra","description":"A Helper to make tests with vercel-serverless-api package","archived":false,"fork":false,"pushed_at":"2021-09-26T20:38:53.000Z","size":145,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T15:47:45.229Z","etag":null,"topics":["node","serverless","sinon","tests","vercel"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/vercel-serverless-tests","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gastonpereyra.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-09-25T13:32:39.000Z","updated_at":"2023-05-12T17:05:25.000Z","dependencies_parsed_at":"2022-08-25T10:30:33.244Z","dependency_job_id":null,"html_url":"https://github.com/gastonpereyra/vercel-serverless-api-tests","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"gastonpereyra/npm-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api-tests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api-tests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api-tests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gastonpereyra%2Fvercel-serverless-api-tests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gastonpereyra","download_url":"https://codeload.github.com/gastonpereyra/vercel-serverless-api-tests/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248807498,"owners_count":21164694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node","serverless","sinon","tests","vercel"],"created_at":"2024-07-31T16:01:02.849Z","updated_at":"2025-04-14T01:31:09.558Z","avatar_url":"https://github.com/gastonpereyra.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Vercel Serverless API Tests\n\n## Code Quality Status\n![Build Status](https://github.com/gastonpereyra/vercel-serverless-api-tests/workflows/Build%20Status/badge.svg)\n[![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)\n\n![vercel-serverless-api-tests Banner](https://user-images.githubusercontent.com/39351850/134091893-712c5542-7d7c-4c51-8fba-3dd2ae1a8247.png)\n\n## Description\nA Helper to make tests with vercel-serverless-api package\n\n## Installation\n\n```\nnpm i vercel-serverless-tests --save-dev\n```\n\n## Sinon\nThe package offers Sinon to help you to realize the tests\n\n```js\n\nconst { sinon } = require('vercel-serverless-tests');\n```\n\n## Tests\n\nThe package has 3 levels to help you:\n\n- Handler\n- It\n- Context\n\n### Handler\nIn order to make easier test the Handler, offers the `testHandler` function, this has a response dummy.\n\n- `testHandler(request, response)`\n    - `request`\n        - `url` _optional_ | string\n        - `method` _optional_ | string\n        - `cookies` _optional_ | object\n        - `headers` _optional_ | object\n        - `queries` _optional_ | object\n        - `pathIds` _optional_ | object\n        - `body` _optional_ | object\n    - `response`\n        - `status` _optional_ | default: 200\n        - `headers` _optional_ | object\n        - `body` _optional_ | any\n\n```js\nconst VercelServerlessApiTests = require('vercel-serverless-tests');\n\nconst handler = require('../../api/test/get');\n\nconst ApiTest = new VercelServerlessApiTests(handler);\n\ndescribe('Test API', () =\u003e {\n    context('When try the It Block', () =\u003e {\n        it('Should return status code 200 with message body and x-custom header', async () =\u003e {\n\n            await ApiTest.testHandler({\n                url: 'api/test/10',\n                method: 'POST',\n                headers: { 'Content-Type': 'application/json' },\n                pathIds: { id: 10 },\n                body: { message: 'Try it!' }\n            },{\n                status: 201,\n                headers: { 'x-custom': true },\n                body: { message: 'Got It!' }\n            });\n        });\n    });\n});\n\n```\n\n### It\n\nIn order to try to make a Standard, an easy way to make an It block.\n\n- `itTest(properties)`\n    - `properties`\n        - `description` _optional_ | string | It description\n\t\t- `only` _optional_ | boolean | If you want to execute this only tests\n\t\t- `skip` _optional_ | string | If you want to skip the tests\n\t\t- `request` _optional_ | object | Request - Same as before\n\t\t- `response` _optional_ | object | Response - Same as before\n\t\t- `before` _optional_ | function | If want to do something before to trigger the handler\n\t\t- `after` _optional_ | function |  If want to do something after to trigger the handler\n\n```js\nconst { sinon, ...VercelServerlessApiTests} = require('vercel-serverless-tests');\n\nconst handler = require('../../api/test/get');\nconst Model = require('../../models/test);\n\nconst ApiTest = new VercelServerlessApiTests(handler);\n\ndescribe('Test API', () =\u003e {\n    context('When try the It Block', () =\u003e {\n        ApiTest.itTest({\n            description: 'Should return status code 200 with message body and x-custom header',\n            only: true,\n            request: {\n                url: 'api/test/10',\n                method: 'POST',\n                headers: { 'Content-Type': 'application/json' },\n                pathIds: { id: 10 },\n                body: { message: 'Try it!' }\n            },\n            response: {\n                status: 201,\n                headers: { 'x-custom': true },\n                body: { message: 'Got It!' }\n            },\n            before: () =\u003e {\n                sinon.stub(Model.prototype, 'get').resolves([]);\n            },\n            after: () =\u003e {\n                sinon.assert.calledOnce(Model.prototype.get);\n            }\n        });\n    });\n});\n```\n\n### Context\n\nTo make multiple It blocks under a context\n\n- `contextTest(description, tests, options)`\n    - `description` _required_ | string | Context descriptition\n    - `tests` _required_ | Array of `properties` (See [It](#It))\n    - `options` _optional_\n        - `skip` _optional_ | boolean | For skip whole context\n        - `only` _optional_ | boolean | For execute only this context\n        - `before` _optional_ | function | Do somenthing before every tests\n        - `beforeEach` _optional_ | function | Do somenthing before each tests\n        - `after` _optional_ | function | Do somenthing after every tests\n        - `afterEach` _optional_ | function | Do somenthing after each tests\n\n```js\nconst { sinon, ...VercelServerlessApiTests} = require('vercel-serverless-tests');\n\nconst handler = require('../../api/test/get');\nconst Model = require('../../models/test);\n\nconst ApiTest = new VercelServerlessApiTests(handler);\n\ndescribe('Test API', () =\u003e {\n    ApiTest.contextTest('When try the It Block', [\n        {\n            description: 'Should return status code 200 with message body and x-custom header',\n            request: {\n                url: 'api/test/10',\n                method: 'POST',\n                headers: { 'Content-Type': 'application/json' },\n                pathIds: { id: 10 },\n                body: { message: 'Try it!' }\n            },\n            response: {\n                status: 201,\n                headers: { 'x-custom': true },\n                body: { message: 'Got It!' }\n            },\n            before: () =\u003e {\n                sinon.stub(Model.prototype, 'get').resolves([]);\n            },\n            after: () =\u003e {\n                sinon.assert.calledOnce(Model.prototype.get);\n            }\n        }\n    ], {\n        only: true,\n        before: () =\u003e {\n            process.env.Secret = '100000';\n        },\n        beforeEach: () =\u003e {\n            sinon.stub(Model.prototype, 'save');\n        },\n        afterEach: () =\u003e {\n            sinon.assert.notCalled(Model.prototype, 'save');\n        },\n        after: () =\u003e {\n            process.env.Secret = undefined;\n        }\n    });\n```\n\n## Bug :bug:\n\n[Report Here](https://github.com/gastonpereyra/vercel-serverless-api-tests/issues/new?assignees=gastonpereyra\u0026labels=bug\u0026template=bug.md\u0026title=[BUG])\n\n## Idea :bulb:\n\n[Tell me](https://github.com/gastonpereyra/vercel-serverless-api-tests/issues/new?assignees=gastonpereyra\u0026labels=enhancement\u0026title=%5BIDEA%5D+-)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api-tests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api-tests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgastonpereyra%2Fvercel-serverless-api-tests/lists"}