{"id":16812203,"url":"https://github.com/flet/tape-nock","last_synced_at":"2025-09-05T10:45:41.286Z","repository":{"id":57159922,"uuid":"51475503","full_name":"Flet/tape-nock","owner":"Flet","description":":vhs: Automatically record and playback HTTP calls for each tape test.","archived":false,"fork":false,"pushed_at":"2023-11-28T07:14:36.000Z","size":149,"stargazers_count":62,"open_issues_count":0,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-27T18:37:48.335Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Flet.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","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":"2016-02-10T21:57:34.000Z","updated_at":"2024-05-23T13:24:36.000Z","dependencies_parsed_at":"2024-06-18T22:57:06.250Z","dependency_job_id":null,"html_url":"https://github.com/Flet/tape-nock","commit_stats":{"total_commits":57,"total_committers":11,"mean_commits":5.181818181818182,"dds":0.7192982456140351,"last_synced_commit":"ee32c957794556bb30807a6066f28685573b055b"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flet%2Ftape-nock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flet%2Ftape-nock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flet%2Ftape-nock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Flet%2Ftape-nock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Flet","download_url":"https://codeload.github.com/Flet/tape-nock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232797464,"owners_count":18577981,"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-10-13T10:21:03.867Z","updated_at":"2025-01-06T22:50:39.615Z","avatar_url":"https://github.com/Flet.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tape-nock\n\n[![npm][npm-image]][npm-url]\n[![npm-next][npm-next]][npm-url]\n[![travis][travis-image]][travis-url]\n[![standard][standard-image]][standard-url]\n[![Package Quality][pack-quality-image]][pack-quality-url]\n[![Coverage Status - Master][coveralls-image]][coveralls-url]\n\n\n[npm-image]: https://img.shields.io/npm/v/tape-nock.svg?style=flat-square\n[npm-next]: https://img.shields.io/npm/v/tape-nock/next.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/tape-nock\n[travis-image]: https://img.shields.io/travis/Flet/tape-nock.svg?style=flat-square\n[travis-url]: https://travis-ci.org/Flet/tape-nock\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[standard-url]: http://npm.im/standard\n[pack-quality-image]: http://npm.packagequality.com/shield/tape-nock.svg\n[pack-quality-url]: http://packagequality.com/#?package=tape-nock\n[coveralls-image]: https://coveralls.io/repos/github/Flet/tape-nock/badge.svg\n[coveralls-url]: https://coveralls.io/github/Flet/tape-nock\n\nAutomatically record and playback HTTP calls for each tape test. This package is really just a decorator that wraps each test individual test with nock's [Nock Back](https://github.com/pgte/nock#nock-back) feature. This helps avoid the all `nockBack` wrapping code which can make tests less clear.\n\n## Install\n\n```\nnpm install tape-nock --save-dev\n```\n\n## Usage\n\n```js\nvar path = require('path')\nvar tape = require('tape') // still need to require tape\nvar tapeNock = require('tape-nock')\n\n// call tapeNock with tape and an options object\nvar test = tapeNock(tape, { //options object to be passed to nock, not required\n  fixtures: path.join(__dirname, 'fixtures'), // this is the default path\n  mode: 'dryrun', // this is the default mode\n  defaultTestOptions: { // optionally provide default options to nockBack for each test\n    before: function () {\n      console.log('a preprocessing function, gets called before nock.define')\n    },\n    after: function () {\n      console.log('a postprocessing function, gets called after nock.define')\n    }\n  }\n})\n```\n\n(see [nockBack test options](https://github.com/node-nock/nock#options-1) for more information on *before*, *after*, and *afterRecord* functions used in `defaultTestOptions`)\n\n\nNow just write your tape tests as usual:\n```js\nvar request = require('request')\n\ntest('do it live on the internet', function(t) {\n  request.get('https://registry.npmjs.org', function (err, res) {\n    t.error(err)\n    t.ok(res)\n    t.end()\n})\n```\nin `dryrun` mode, the above test will go to the internet and nothing will be recorded.\n\nOnce your tests are perfected, Create fixture files by using the `record` mode. This captures all HTTP calls per test and saves it to the `fixtures` directory.\n\nTo record, set the `NOCK_BACK_MODE` environment variable:\n```bash\nNOCK_BACK_MODE=record npm test\n```\n...or do it programatically via the `tape-nock` options object:\n```js\nvar test = tapeNock(tape, {\n  fixtures: path.join(__dirname, 'fixtures'),\n  mode: 'record' // record mode!\n})\n```\nA new file called `do it live on the internet.json` will be created in the `fixtures` directory. It will contain an array of HTTP calls that were made during the test. Each test will have its own JSON.\n\nOnce a fixture exists for a test, it will be used every time in `dryrun` mode. To re-record, you'll need to delete the JSON file.\n\nTo make this easier, add scripts to your `package.json` for easy recording/running:\n```js\n{\n  \"scripts\" {\n    \"test\": \"tape test/*js\"\n    \"test:record\": \"NOCK_BACK_MODE=record npm test\",\n    \"test:wild\": \"NOCK_BACK_MODE=wild npm test\",\n    \"test:lockdown\": \"NOCK_BACK_MODE=lockdown npm test\",\n    \"test:overwrite\": \"rm test/fixtures/*.json \u0026 npm run test:record\"\n  }\n}\n```\n\n### Can still mock things\nIt is also possible to manually mock at the same time so a request NEVER hits a URL.\n\nJust get a copy of nock from `tape-nock` via `.nock` and use it:\n```js\nvar tape = require('tape')\nvar tapeNock = require('tape-nock')\nvar test = tapeNock(tape, {\n  fixtures: path.join(__dirname, 'fixtures') //defaults to this path\n})\n\nvar request = require('request')\ntest('able to get a copy of nock from test.nock and use it', function (t) {\n  // get a copy of nock\n  var nock = test.nock\n\n  // use it to mock a URL. This mock will live even if NOCK_BACK_MODE=wild\n  nock('http://registry.npmjs.org').get('/clockmoji').reply(200, {'yep': 'it works'})\n\n  request.get('http://registry.npmjs.org/clockmoji', process)\n\n  function process (err, resp) {\n    t.error(err, 'no error')\n    t.equals(JSON.parse(resp.body).yep, 'it works', 'able to mock directly with nock instance')\n    t.end()\n  }\n})\n\n```\n\n\n### NOCK_BACK_MODE\nUse the `NOCK_BACK_MODE` environment variable ([details](https://github.com/pgte/nock#modes)) to control the mode of nockBack.\n\nHere is a recap\n- **wild:** all requests go out to the internet, don't replay anything, doesn't record anything\n- **dryrun:** The default, use recorded nocks, allow http calls, doesn't record anything, useful for writing new tests\n- **record:** use recorded nocks, record new nocks\n- **lockdown:** use recorded nocks, disables all http calls even when not nocked, doesn't record\n\n### Nock Back options\nIts also possible to pass [nockBack options](https://github.com/pgte/nock#options-1) through tape's options object.\n\nThis is helpful for doing `filteringPath` or `filteringRequestBody` (check out the [PROTIP](https://github.com/pgte/nock#protip)... that can be done in an \"after\" function).\n\nFor Example, this test will use the `after` function passed in, which will make all \"time-based-param\" params in the path and replace them with 123. This will ensure time-based parameters will still be mock-able (the path in the fixture JSON will need to be manually updated to match).\n```js\nvar after = function (scope) {\n  scope.filteringPath(/time-based-param=[^\u0026]*/g, 'time-based-param=123')\n}\n\ntest('pass through opts to nockback', {after: after}, function (t) {\n  request.get('http://registry.npmjs.com?time-based-param=1455231758348', function (err, resp) {\n    t.error(err)\n    t.equals(JSON.parse(resp.body).haha, 'no secrets for you', 'secrets are protected')\n    t.end()\n  })\n})\n\n```\n\n### Using supertest with tape-nock\n\nSince nockBack will mock all HTTP requests, using [supertest](https://github.com/visionmedia/supertest) can be tricky. Here is an example of how to avoid mocking/recording local connections when using `supertest`.\n\nHere is our application. It simply hits http://httpbin.org/get which echos info back. We want to have nockBack record/mock the httpbin request but still allow `supertest` http requests to 127.0.0.1 to pass through for all tests. This is done by leveraging the `defaultTestOptions`.\n\n**app.js**\n```js\nconst express = require('express')\nconst request = require('superagent')\n\nvar app = express()\n\napp.get('/myapp/version', function (req, res) {\n  superagent\n    .get('http://httpbin.org/get')\n    .end(function (err, response) {\n      res.status(200).json({\n        version: '0.1.0',\n        url: response.body.url\n      })\n    })\n})\n\nmodule.exports = app\n```\n\n**test.js**\n```js\nconst supertest = require('supertest');\nconst app = require('../app.js');\n\nconst tape = require('tape');\nconst tapeNock = require('tape-nock');\nconst nock = tapeNock.nock;\n\nconst opts = {\n  // after recording the fixtures, remove any scopes that hit 127.0.0.1\n  // this is not necessary with our before function below, but it makes it a bit cleaner.\n  afterRecord: function (scopes) {\n    var localhost = /http:\\/\\/127\\.0\\.0\\.1.*/;\n    scopes = scopes.filter(function (s) {\n      return !localhost.test(s.scope);\n    });\n\n    return scopes;\n  },\n  before: function () {\n    // allow connections to 127.0.0.1 even when NOCK_BACK_MODE=lockdown\n    nock.enableNetConnect('127.0.0.1');\n  }\n};\n\n// call tapeNock with tape and an options object\nconst test = tapeNock(tape, { defaultTestOptions: opts });\n\n// note that we're passing in test.options here\n// which has our special \"afterRecord\" and \"before\" functions\ntest('hit version url', function (t) {\n  supertest(app)\n    .get('/myapp/version')\n    .expect(200, {\n      url: 'http://httpbin.org/get',\n      version: '0.1.0'\n    })\n    .end(function (err, res) {\n      t.error(err, 'no error');\n      t.equals(res.body.url, 'http://httpbin.org/get', 'url is correct');\n      t.end();\n    });\n});\n\n```\n\n## Contributing\n\nContributions welcome! Please read the [contributing guidelines](CONTRIBUTING.md) first.\n\n## License\n\n[ISC](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflet%2Ftape-nock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflet%2Ftape-nock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflet%2Ftape-nock/lists"}