{"id":15413407,"url":"https://github.com/i-like-robots/express-request-mock","last_synced_at":"2025-04-14T03:14:50.939Z","repository":{"id":18131500,"uuid":"83441057","full_name":"i-like-robots/express-request-mock","owner":"i-like-robots","description":"🖖 A convenient wrapper for node-mocks-http which makes testing Express controllers and middleware easy.","archived":false,"fork":false,"pushed_at":"2024-12-09T21:28:01.000Z","size":296,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T03:14:41.741Z","etag":null,"topics":["express","nodejs","testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/express-request-mock","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/i-like-robots.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-28T14:21:32.000Z","updated_at":"2025-04-09T10:11:43.000Z","dependencies_parsed_at":"2023-02-12T23:01:39.850Z","dependency_job_id":"e8f7e3aa-31e5-423a-ae9a-499af0dab864","html_url":"https://github.com/i-like-robots/express-request-mock","commit_stats":{"total_commits":86,"total_committers":4,"mean_commits":21.5,"dds":0.4534883720930233,"last_synced_commit":"433611bc3daf2c1ac134441bbd4236671e942171"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i-like-robots%2Fexpress-request-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i-like-robots%2Fexpress-request-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i-like-robots%2Fexpress-request-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/i-like-robots%2Fexpress-request-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/i-like-robots","download_url":"https://codeload.github.com/i-like-robots/express-request-mock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248813803,"owners_count":21165634,"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":["express","nodejs","testing"],"created_at":"2024-10-01T16:56:56.500Z","updated_at":"2025-04-14T03:14:50.871Z","avatar_url":"https://github.com/i-like-robots.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-request-mock\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/i-like-robots/express-request-mock/blob/main/LICENSE)\n![build status](https://github.com/i-like-robots/express-request-mock/actions/workflows/test.yml/badge.svg?branch=main) [![npm version](https://img.shields.io/npm/v/express-request-mock.svg?style=flat)](https://www.npmjs.com/package/express-request-mock)\n\nA convenient wrapper for [node-mocks-http][1] which makes testing Express controllers and middleware easy.\n\n```js\nimport requestMock from 'express-request-mock'\nimport handler from '../routes/animals'\n\nit('returns a 200 response', async () =\u003e {\n  const { response } = await requestMock(handler, options)\n  expect(response.statusCode).toEqual(200)\n})\n```\n\n[1]: https://github.com/howardabrams/node-mocks-http\n\n## Installation\n\nThis is a [Node.js][node] module available through the [npm][npm] registry. Node.js 18 or higher is required.\n\n```sh\n$ npm install --save-dev express-request-mock\n```\n\n[node]: https://nodejs.org/en/\n[npm]: https://www.npmjs.com/\n\n## Usage\n\nThis package provides one function which accepts three arguments:\n\n1. The route handler to test (a function which accepts a request, response, and optional fallthrough function.)\n2. An options object for `createRequest` (the options are [documented here][2].)\n3. An object containing extra properties to decorate to the request and response objects.\n\n[2]: https://github.com/howardabrams/node-mocks-http#createrequest\n\nThe function returns a promise which will resolve when the response is ended or the fallthrough function (`next()`) is called. The promise will reject if the underlying code throws an error or the fallthrough function is called with an error.\n\nWhen resolved the promise will to an object with the following keys:\n\n1. `request`: The request object created by `createRequest`\n2. `response`: The response object created by `createResponse`\n\nBelow is a complete example demonstrating the use of `express-request-mock` to test an Express route handler:\n\n```js\nimport { describe, it } from 'node:test'\nimport assert from 'node:assert'\nimport requestMock from 'express-request-mock'\nimport handler from '../routes/animals'\n\ndescribe('Controllers - Animals', () =\u003e {\n  describe('when a valid species is requested', () =\u003e {\n    const options = { query: { species: 'dog' } }\n\n    it('returns a 200 response', async () =\u003e {\n      const { response } = await requestMock(handler, options)\n      assert.equal(response.statusCode, 200)\n    })\n  })\n\n  describe('when a non-existent species is requested', () =\u003e {\n    const options = { query: { species: 'unicorn' } }\n\n    it('returns a 404 response', async () =\u003e {\n      const { response } = await requestMock(handler, options)\n      assert.equal(response.statusCode, 404)\n    })\n  })\n\n  describe('when an invalid request is received', () =\u003e {\n    const options = { query: {} }\n\n    it('calls the fallthrough function with an error', async () =\u003e {\n      const call = () =\u003e requestMock(handler, options)\n\n      await assert.rejects(call, {\n        name: 'NoSpeciesProvided',\n        message: 'You must provide a species',\n      })\n    })\n  })\n})\n```\n\n## License\n\nexpress-request-mock is MIT licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fi-like-robots%2Fexpress-request-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fi-like-robots%2Fexpress-request-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fi-like-robots%2Fexpress-request-mock/lists"}