{"id":15068375,"url":"https://github.com/shahradelahi/next-test-api-routes","last_synced_at":"2025-04-10T16:33:22.280Z","repository":{"id":279459994,"uuid":"571380776","full_name":"shahradelahi/next-test-api-routes","owner":"shahradelahi","description":"A Lightweight library for testing Next.js API routes","archived":false,"fork":false,"pushed_at":"2022-11-28T02:24:15.000Z","size":73,"stargazers_count":30,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T11:59:46.742Z","etag":null,"topics":["api","jest","library","nextjs","routes","testing"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/next-test-api-routes","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/shahradelahi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-28T01:53:26.000Z","updated_at":"2025-01-08T07:11:10.000Z","dependencies_parsed_at":"2025-02-25T17:42:39.641Z","dependency_job_id":"1c13bbfa-a9b9-4ca0-a26e-87c82df65108","html_url":"https://github.com/shahradelahi/next-test-api-routes","commit_stats":null,"previous_names":["shahradelahi/next-test-api-routes"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahradelahi%2Fnext-test-api-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahradelahi%2Fnext-test-api-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahradelahi%2Fnext-test-api-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shahradelahi%2Fnext-test-api-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shahradelahi","download_url":"https://codeload.github.com/shahradelahi/next-test-api-routes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248252718,"owners_count":21072701,"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":["api","jest","library","nextjs","routes","testing"],"created_at":"2024-09-25T01:34:37.745Z","updated_at":"2025-04-10T16:33:22.258Z","avatar_url":"https://github.com/shahradelahi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ⚡️ Next Test API Routes\n\n[![Tests](https://github.com/shahradelahi/next-test-api-routes/actions/workflows/tests.yml/badge.svg)](https://github.com/shahradelahi/next-test-api-routes/actions/workflows/tests.yml)\n[![npm version](https://badge.fury.io/js/next-test-api-routes.svg)](https://badge.fury.io/js/next-test-api-routes)\n[![npm](https://img.shields.io/npm/dt/next-test-api-routes)](https://www.npmjs.com/package/next-test-api-routes)\n\n## Installation\n\n```bash\nnpm install --save-dev next-test-api-routes \n```\n\n## Usage\n\n```js\nimport {createMocks, parseResponse} from 'next-test-api-routes'\n\nconst route = require('./api/hello')\n\ndescribe('api/hello', () =\u003e {\n   it('should return a 200 status code', async () =\u003e {\n      const {req, res} = createMocks({\n         method: 'GET',\n      })\n\n      const resp = parseResponse(await route(req, res))\n\n      expect(res.statusCode).toBe(200)\n   })\n\n   it('should return a JSON object', async () =\u003e {\n      const {req, res} = createMocks({\n         method: 'POST',\n         body: {\n            name: 'John Doe',\n         },\n      })\n\n      const resp = parseResponse(await route(req, res))\n\n      expect(resp.json().message).toBe('Hello John Doe')\n   })\n})\n```\n\n## Methods\n\n### createMocks\n\nCreates a mock request and response object.\n\n#### Parameters\n\n-  `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** \n   -  `options.method` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (optional, default `'GET'`)\n   -  `options.body` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**  (optional, default `undefined`)\n   -  `options.query` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)\n   -  `options.headers` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)\n   - `options.cookies` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)\n   - `options.url` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (optional, default `/`)\n   - `options.params` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**  (optional, default `{}`)\n\n#### Examples\n\n```js\nconst {req, res} = createMocks({\n   method: 'GET',\n    body: {\n        name: 'John Doe',\n    },\n    query: {\n        page: 1,\n    }\n})\n```\n\n### parseResponse\n\nParses the response object from the route handler.\n\n#### Parameters\n\n-  `response` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**\n\n#### Returns\n\n- `response` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**\n- `response.json` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Returns the parsed JSON object.\n- `response.body` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The response body as a string.\n- `response.headers` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** The response headers.\n- `response.statusCode` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The response status code.\n\n#### Examples\n\n```js\nconst {req, res} = createMocks({\n   method: 'GET',\n})\n\nconst resp = parseResponse(await route(req, res))\n\nexpect(resp.json().message).toBe('Hello World')\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahradelahi%2Fnext-test-api-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshahradelahi%2Fnext-test-api-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshahradelahi%2Fnext-test-api-routes/lists"}