{"id":18427626,"url":"https://github.com/manosim/mocr","last_synced_at":"2025-04-07T16:33:39.114Z","repository":{"id":56062364,"uuid":"306770893","full_name":"manosim/mocr","owner":"manosim","description":"A mock http server to be used where you can't mock http requests","archived":false,"fork":false,"pushed_at":"2022-01-11T09:27:13.000Z","size":143,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-13T03:21:42.058Z","etag":null,"topics":["http","mock","node","server"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/manosim.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":"2020-10-23T23:53:58.000Z","updated_at":"2021-06-23T22:00:09.000Z","dependencies_parsed_at":"2022-08-15T12:30:39.987Z","dependency_job_id":null,"html_url":"https://github.com/manosim/mocr","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manosim%2Fmocr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manosim%2Fmocr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manosim%2Fmocr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manosim%2Fmocr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/manosim","download_url":"https://codeload.github.com/manosim/mocr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223286383,"owners_count":17120000,"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":["http","mock","node","server"],"created_at":"2024-11-06T05:11:19.734Z","updated_at":"2024-11-06T05:11:20.449Z","avatar_url":"https://github.com/manosim.png","language":"TypeScript","readme":"# mocr [![npm][npm-image]][npm-url] [![github-ci][github-ci-image]][github-ci-url]\n\n\u003e A mock http server used in tests\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/6333409/99885443-dcd40e00-2c2c-11eb-9261-6cfd3d7de5a0.png\" alt=\"Mocr Logo\" width=\"125\" height=\"125\" /\u003e\n\u003c/p\u003e\n\n## Features\n\n- Easy to use, mock http server\n- Spy/track requests received by the server\n- Designed to work with end-to-end \u0026 unit tests\n- Strongly typed, types included\n- Zero dependencies\n\n## Installation\n\n```\nyarn add -D mocr\n# or\nnpm install --save-dev mocr\n```\n\n## Configuration\n\nAll config options mentioned below are **_optional_**.\n\n| Name  | Default | Description                                |\n| ----- | ------- | ------------------------------------------ |\n| debug | false   | When set to true, logging will be enabled. |\n| port  | 9091    | The port that the server will be running.  |\n\n## Usage\n\n```js\nimport mocr, { createRequestSpy } from 'mocr';\n\ndescribe('my tests', () =\u003e {\n  const mockServer = mocr({\n    /* Configuration */\n  });\n\n  beforeAll(async () =\u003e {\n    // Start the server\n    await mockServer.start();\n  });\n\n  beforeEach(async () =\u003e {\n    // Reset the request spy\n    mockServer.requestSpy.reset();\n  });\n\n  afterAll(async () =\u003e {\n    // Stop the server\n    await mockServer.stop();\n  });\n\n  it('should make a call to the backend when pressing the button', () =\u003e {\n    // Press the button\n\n    const { request, body } = requestSpy.calls[0];\n\n    expect(mockServer.requestSpy).toHaveBeenCalledTimes(1);\n    expect(request.method).toBe(/* Expected Method, ie. POST, PUT */);\n    expect(body).toHaveBeenCalledWith(/* Expected Request Body */);\n  });\n});\n```\n\n## Methods\n\n### mocr\n\n```js\nconst mockServer = mocr(/* Optional Config */);\n\nawait mockServer.start();\n// Run your test case\nawait mockServer.stop();\n```\n\nUsed to create an instance of _mocr_ - it accepts _optional_ configuration. You can have as many _mocr_ servers running in parallel as long as they run on a [different port](#configuration). See [example](#usage) for a complete example.\n\n### start()\n\n```js\nconst mockServer = mocr(/* Optional Config */);\n\nawait mockServer.start();\n```\n\nStarts the http server.\n\n### stop()\n\n```js\nconst mockServer = mocr(/* Optional Config */);\n\nawait mockServer.stop();\n```\n\nStops the server of the `mocr` instance.\n\n### mockNextResponse({ data })\n\n```js\nconst { start, stop, mockNextResponse } = mocr(/* Optional Config */);\n\nmockNextResponse({ foo: 'bar' });\n```\n\nUsed to return a mock/stubbed response from the server. Will only use that response once and will then fall back to the default `Hello World` server response. For mocking multiple requests, see [mockNextResponses](#mockNextResponses) below.\n\n### mockNextResponses([ { data } ])\n\n```js\nconst { start, stop, mockNextResponses } = mocr(/* Optional Config */);\n\nmockNextResponses([{ id: '123' }, { id: '456' }]);\n```\n\nSimilar to `mockNextResponse` but expects an array of data. The data will be return for each response in the order they appear in the array.\n\n## Properties\n\n### requestSpy\n\n```js\nconst { start, stop, requestSpy } = mocr(/* Optional Config */);\n\nexpect(requestSpy.calls).toHaveLength(1);\n```\n\nHolds a records/tracks all _incoming_ requests to the mock server along with their body/data(if any). To be used for validating requests/content leaving your application. Below you can find all available methods for a RequestSpy. See [example](#usage) above.\n\n| Name  | Description                                                                   |\n| ----- | ----------------------------------------------------------------------------- |\n| calls | An array of all the calls. `[ {request: IncomingMessage. body: string {} } ]` |\n| reset | Empties the `calls` array.                                                    |\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n\n[github-ci-image]: https://github.com/manosim/mocr/workflows/Run%20Tests/badge.svg\n[github-ci-url]: https://github.com/manosim/mocr/actions\n[npm-image]: https://badge.fury.io/js/mocr.svg\n[npm-url]: https://www.npmjs.com/package/mocr\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanosim%2Fmocr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanosim%2Fmocr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanosim%2Fmocr/lists"}