{"id":13452483,"url":"https://github.com/ericclemmons/node-recorder","last_synced_at":"2025-04-21T11:33:34.654Z","repository":{"id":34293836,"uuid":"172167801","full_name":"ericclemmons/node-recorder","owner":"ericclemmons","description":"Simple recording \u0026 replaying of HTTP requests for predictable development \u0026 testing.","archived":false,"fork":false,"pushed_at":"2023-01-07T03:19:09.000Z","size":15750,"stargazers_count":97,"open_issues_count":34,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T18:06:56.733Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ericclemmons.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}},"created_at":"2019-02-23T03:44:05.000Z","updated_at":"2023-05-10T09:13:35.000Z","dependencies_parsed_at":"2023-01-15T06:00:47.962Z","dependency_job_id":null,"html_url":"https://github.com/ericclemmons/node-recorder","commit_stats":null,"previous_names":["ericclemmons/back-to-the-fixture"],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fnode-recorder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fnode-recorder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fnode-recorder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericclemmons%2Fnode-recorder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericclemmons","download_url":"https://codeload.github.com/ericclemmons/node-recorder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249521670,"owners_count":21285469,"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":[],"created_at":"2024-07-31T07:01:25.426Z","updated_at":"2025-04-21T11:33:32.746Z","avatar_url":"https://github.com/ericclemmons.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"node-recorder logo\" src=\"./logo.gif\" width=\"50%\"\u003e\n\u003c/p\u003e\n\n- Automatically record new HTTP(s) requests.\n- Replay recordings when testing.\n- Customize responses.\n- Works well with [supertest](https://github.com/visionmedia/supertest).\n- Predictable, deterministic filepaths.\n- Normalize the `request` \u0026 `response`.\n- Associate session-based cookies \u0026 OAuth tokens to users.\n- Ignore requests you don't want to record.\n\n---\n\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"node-recorder demo\" src=\"./demo.gif\"\u003e\n\u003c/p\u003e\n\n---\n\n## Installation\n\n```shell\n$ yarn add node-recorder --dev\n# or\n$ npm install node-recorder --save-dev\n```\n\n## Getting Started\n\n- By simply including `node-recorder`, **all HTTP(s) requests are intercepted**.\n- By default, `RECORD` mode records new recordings, and replays existing fixures.\n- When in `NODE_ENV=test` or `CI=true`, `REPLAY` mode replays existing recordings, and throws an error when one doesn't exist.\n  _(So that local tests don't suddenly fail in CI)_\n\n### Recorder Modes\n\n- `bypass` - All network requests bypass the recorder and respond as usual.\n- `record` - Record only new network requests (i.e. those without recordings), while replaying existing recordings.\n- `replay` - Replay all network requests using recordings. **If a recording is missing, an error is thrown**.\n- `rerecord` - Re-record all network requests.\n\n### Using `node --require`\n\n```shell\n$ node -r node-recorder path/to/server.js\n```\n\n_(This also works with `mocha`!)_\n\n### Setting the `mode` via `RECORDER=...`\n\n```shell\n$ RECORDER=ignore node -r node-recorder path/to/server.js\n```\n\n### Using Jest\n\nIncluded is a `jest-preset` that will automatically include `node-recorder` and a custom plugin to make toggling modes easier.\n\n```js\n// jest.config.js\nmodule.exports = {\n  preset: \"node-recorder/jest-preset\"\n};\n```\n\nNow, running `jest --watch` will add a new `r` option:\n\n```\nWatch Usage\n › Press a to run all tests.\n › Press f to run only failed tests.\n › Press p to filter by a filename regex pattern.\n › Press t to filter by a test name regex pattern.\n › Press q to quit watch mode.\n › Press r to change recording mode from \"REPLAY\".\n › Press Enter to trigger a test run.\n```\n\nPressing `r` will toggle between the various modes:\n\n```\n  ╭─────────────────────────────╮\n  │                             │\n  │   node-recorder:  RECORD    │\n  │                             │\n  ╰─────────────────────────────╯\n```\n\n### Configuring `recorder.config.js`\n\nWithin your project, you can create a `recorder.config.js` that exports:\n\n```js\n// recorder.conig.js\nmodule.exports = {\n  identify(request, response) {...},\n  ignore(request) {...},\n  normalize(request, response) {...}\n}\n```\n\n- `request` is the same as the recording (e.g. `body`, `headers`, `href`, `method`), but\n  with an additional `url` property from https://github.com/unshiftio/url-parse to simplify conditional logic.\n- `response` contains `body`, `headers`, \u0026 `statusCode`.\n\n#### `identify` a `request` or `response\n\nThis is useful when network requests are stateful, in that they rely on an authorization call first, then they pass along a token/cookie to subsequent calls:\n\n1. Suppose you login by calling `/login?user=foo\u0026password=bar`.\n2. The response contains `{ \"token\": \"abc123\" }3. Now, to get data, you call`/api?token=abc123`.\n\nWhen recording recordings, the token `abc123` isn't clearly associated with the user `foo`.\n\nTo address this, you can `identify` the `request` and `response`, so that the recordings are aliased accordingly:\n\n```js\nidentify(request, response) {\n  const { user, token } = request.query\n\n  if (request.href.endsWith(\"/login\")) {\n    // We know the user, but not the token yet\n    if (!response) {\n      return user\n    }\n\n    // Upon login, associate this `user` with the `token`\n    return [user, response.body.token]\n  }\n\n  // API calls supply a `token`, which has been associated with a `user`\n  if (request.href.endsWith(\"/api\")) {\n    return token\n  }\n}\n```\n\nNow, when recorded recordings will look like:\n\n- `127.0.0.1/login/${hash}.${user}.json`\n- `127.0.0.1/api/${hash}.${user}.json`\n\nThis way, similar-looking network requests (e.g. login \u0026 GraphQL) can be differentiated and easily searched for.\n\n#### `ignore` a `request`\n\nTypically, you don't want to record recordings for things like analytics or reporting.\n\n```js\n// recorder.conig.js\nmodule.exports = {\n  ignore(request) {\n    if (request.href.includes(\"www.google-analytics.com\")) {\n      return true;\n    }\n\n    return false;\n  }\n};\n```\n\n#### `normalize` a `request` or `response`\n\nRecordings are meant to make development \u0026 testing _easier_, so modification is necessary.\n\n- **Changing `request` changes the filename `hash` of the recording**. You may need to `record` again.\n- `normalize` is called **before** the network request and **after**. This means that `response` may be `undefined`!\n- You can **change `response` by hand, or via `normalize` without affecting the filename `hash` of the recording**.\n\n```js\nmodule.exports = {\n  normalize(request, response) {\n    // Suppose you never care about `user-agent`\n    delete request.headers[\"user-agent\"];\n\n    // We may not have a response (yet)\n    if (response) {\n      // ...or the `date`\n      delete response;\n    }\n  }\n};\n```\n\n## MIT License\n\n## Author\n\n- Eric Clemmons\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fnode-recorder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericclemmons%2Fnode-recorder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericclemmons%2Fnode-recorder/lists"}