{"name":"@tapjs/intercept","version":"4.3.12","description":"a built-in tap extension for t.intercept() and t.capture()","tshy":{"main":true,"exports":{"./package.json":"./package.json",".":"./src/index.ts"},"selfLink":false},"type":"module","main":"./dist/commonjs/index.js","module":"./dist/esm/index.js","types":"./dist/commonjs/index.d.ts","exports":{"./package.json":"./package.json",".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}}},"scripts":{"prepare":"tshy","pretest":"npm run prepare","presnap":"npm run prepare","test":"tap","snap":"tap","format":"prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache","typedoc":"typedoc --tsconfig tsconfig/esm.json ./src/*.ts"},"author":{"name":"Isaac Z. Schlueter"},"license":"BlueOak-1.0.0","dependencies":{"@tapjs/after":"3.3.12","@tapjs/stack":"4.3.3"},"peerDependencies":{"@tapjs/core":"4.6.0"},"tap":{"typecheck":false},"repository":{"type":"git","url":"git+ssh://git@github.com/tapjs/tapjs.git","path":"src/intercept"},"homepage":"https://github.com/tapjs/tapjs/tree/main/src/intercept#readme","bugs":{"url":"https://github.com/tapjs/tapjs/issues"},"keywords":["tapjs plugin"],"engines":{"node":"20 || >=22"},"gitHead":"8ab84eb1daae6dedbfcf07b8e0bf2c2a965ad81f","_id":"@tapjs/intercept@4.3.12","_nodeVersion":"26.5.0","_npmVersion":"11.17.0","dist":{"integrity":"sha512-r8sMdyY5DScTOmxTWFHQz10myQvPrUG49WcNLGEaHmQ1+08q4h0/xwrLvjOOeHTb6Jh5qI2WISz80v3Yj/eWAQ==","shasum":"db899940a8c302219f2a6a8565f3370dacd10c93","tarball":"http://registry-npm.ywwl.com/@tapjs/intercept/download/@tapjs/intercept-4.3.12.tgz","fileCount":13,"unpackedSize":102043,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQDTrDioPowTupCJdpV4vwn45UCh8+ptAyisJC3N28+ZEwIgIz5fENuO75H3yRyLEJF6VklxNMzPwo6vrONRcQ9pcqs="}],"size":13742,"noattachment":false,"key":"/@tapjs/intercept/-/@tapjs/intercept-4.3.12.tgz"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"directories":{},"maintainers":[{"name":"ljharb","email":"ljharb@gmail.com"},{"name":"isaacs","email":"i@izs.me"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/intercept_4.3.12_1788477680150_0.18203852425938516"},"_hasShrinkwrap":false,"readme":"# `@tapjs/intercept`\n\nA default tap plugin for doing object/global property/method\ninterception and observing. These are sometimes refered to as\n\"spy\" or \"mock\" methods (though the term \"mock\" is extremely\noverloaded, and in tap is usually used to refer to [dependency\ninjection mocking](https://node-tap.org/plugins/mock)).\n\n## Usage\n\n```js\nimport t from 'tap'\n\nconst functionThatLogs = (n: number) => {\n  console.log('the number is', n)\n}\n\nt.test('some child test', t => {\n  // track console.log calls\n  const results = t.capture(console, 'log')\n  functionThatLogs(10)\n  functionThatLogs(5)\n  // results() returns the list of what was called, and resets\n  // the store.\n  t.match(results(), [\n    { args: ['the number is', 10], returned: undefined },\n    { args: ['the number is', 5], returned: undefined },\n  ])\n  functionThatLogs(1)\n  t.match(results(), [\n    { args: ['the number is', 1], returned: undefined },\n  ])\n  // when the test ends, the original is restored\n  t.end()\n})\n\nt.test('capture with an implementation', t => {\n  const results = t.capture(console, 'log', () => {\n    throw new Error('thrown from stub')\n  })\n  t.throws(\n    () => {\n      functionThatLogs(3)\n    },\n    { message: 'thrown from stub' }\n  )\n  t.match(results(), { args: ['the number is', 3], threw: true })\n  t.end()\n})\n\nt.test('capture and still call the function', t => {\n  // to do this, we just pass the original in as the third arg\n  const results = t.capture(console, 'log', console.log)\n  // actually logs to the console\n  functionThatLogs(1)\n  t.match(results(), [\n    { args: ['the number is', 1], returned: undefined },\n  ])\n  t.end()\n})\n\nt.test('intercept a property set/get', t => {\n  // the last arg is a propertyDescriptor, but configurable: true\n  // forced on it even if not provided, so that we can restore\n  // it at the end of the test.\n  // If a value is provided, then we still actually set to a\n  // setter/getter so that we can track accesses.\n  const results = t.intercept(process, 'version', {\n    value: '1.2.3',\n  })\n  t.equal(process.version, '1.2.3')\n  process.version = '2.4.6'\n  // we didn't make it writable, so this didn't do anything.\n  t.equal(process.version, '1.2.3')\n  t.match(results(), [\n    { receiver: process, type: 'get', value: '1.2.3', success: true },\n    {\n      receiver: process,\n      type: 'set',\n      value: '2.4.6',\n      success: false,\n    },\n    { receiver: process, type: 'get', value: '1.2.3', success: true },\n  ])\n})\n```\n\n## API\n\n- `Type ResultsFunction` A function that returns data about the\n  intercepted calls, and resets the tracking array.\n  `results.restore()` will restore the method to its original\n  state.\n\n- `t.capture(obj, method, implementation = () => {}): CaptureResultFunction`\n\n  Replaces `obj[method]` with the supplied implementation.\n\n  The `results()` method will return an array of objects with a\n  `receiver` property indicating the `this`-context of the method\n  call, an `args` array, an `at` CallSiteLike object, and either\n  `threw: true` or `returned: <value>`.\n\n  If `t.teardown()` is available (ie, if the `@tapjs/after`\n  plugin is not disabled) then it will be automatically\n  restored on test teardown. Otherwise, `results.restore()`\n  must be called to restore the original method.\n\n  Returned method also has a `calls` array which contains the\n  results.\n\n- `t.intercept(obj, property, desc?: PropertyDescriptor, strictMode: boolean = true): InterceptResultsFunction`\n\n  Similar to `t.capture()`, but can be used to track get/set\n  operations for any arbitrary property. The results function\n  returns a list of objects with:\n\n  - `receiver` the object where the set/get is happening\n  - `type` 'get' for get operations, 'set' for set operations\n  - `value` The value that was returned by a get, or set in a\n    set.\n  - `at` call site where the get/set occurred.\n  - `threw` whether or not the call threw.\n  - `success` whether or not the call was sucessful.\n\n- `t.captureFn(original: (...a:any[]) => any): WrappedFunction`\n\n  Similar to `t.capture()`, but just wraps the function and\n  returns it.\n\n  Returned function has a `calls` array property containing the\n  results.\n","publish_time":1788477680291,"_cnpm_publish_time":1788477680291,"dist-tags":{"latest":"4.3.13","pre":"1.0.0"}}