{"name":"@tapjs/intercept","version":"4.3.9","keywords":["tapjs plugin"],"author":{"name":"Isaac Z. Schlueter"},"license":"BlueOak-1.0.0","_id":"@tapjs/intercept@4.3.9","maintainers":[{"name":"ljharb","email":"ljharb@gmail.com"},{"name":"isaacs","email":"i@izs.me"}],"homepage":"https://github.com/tapjs/tapjs/tree/main/src/intercept#readme","bugs":{"url":"https://github.com/tapjs/tapjs/issues"},"tap":{"typecheck":false},"dist":{"shasum":"6f74a258da78bec9f8df5eb8c6a7f92e12a8f108","tarball":"http://registry-npm.ywwl.com/@tapjs/intercept/download/@tapjs/intercept-4.3.9.tgz","fileCount":13,"integrity":"sha512-QKF4UA5+Xc2d3oUBs0kbXnsBI7+O89a3IPhVPox7U2/pXABVIG4u4hNHfQQa259HCk1LeOMW4ZwQ7tW/aGdrmw==","signatures":[{"sig":"MEUCID3sPZ+/pCX9Waljz+LIcNWg7EFcOXTEDxpDyObLC/FMAiEAzSAGFVyhaIIKvn5mRyN+B2rvLixvZHGMooM5b9a9uaQ=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":102041,"size":13742,"noattachment":false,"key":"/@tapjs/intercept/-/@tapjs/intercept-4.3.9.tgz"},"main":"./dist/commonjs/index.js","tshy":{"main":true,"exports":{".":"./src/index.ts","./package.json":"./package.json"},"selfLink":false},"type":"module","types":"./dist/commonjs/index.d.ts","module":"./dist/esm/index.js","engines":{"node":"20 || >=22"},"exports":{".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}},"./package.json":"./package.json"},"gitHead":"0ab7f3cdf804a5d481a7ab78ffc00aee600c7092","scripts":{"snap":"tap","test":"tap","format":"prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache","prepare":"tshy","presnap":"npm run prepare","pretest":"npm run prepare","typedoc":"typedoc --tsconfig tsconfig/esm.json ./src/*.ts"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"repository":{"url":"git+ssh://git@github.com/tapjs/tapjs.git","path":"src/intercept","type":"git"},"_npmVersion":"11.12.1","description":"a built-in tap extension for t.intercept() and t.capture()","directories":{},"_nodeVersion":"26.0.0","dependencies":{"@tapjs/after":"3.3.9","@tapjs/stack":"4.3.3"},"_hasShrinkwrap":false,"peerDependencies":{"@tapjs/core":"4.5.7"},"_npmOperationalInternal":{"tmp":"tmp/intercept_4.3.9_1778712845261_0.148591407297892","host":"s3://npm-registry-packages-npm-production"},"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":1778712845430,"_cnpm_publish_time":1778712845430,"dist-tags":{"latest":"4.3.13","pre":"1.0.0"}}