{"name":"@tapjs/mock","version":"4.0.6","keywords":["tapjs plugin"],"author":{"url":"https://blog.izs.me","name":"Isaac Z. Schlueter","email":"i@izs.me"},"license":"BlueOak-1.0.0","_id":"@tapjs/mock@4.0.6","maintainers":[{"name":"ljharb","email":"ljharb@gmail.com"},{"name":"isaacs","email":"i@izs.me"}],"homepage":"https://github.com/tapjs/tapjs#readme","bugs":{"url":"https://github.com/tapjs/tapjs/issues"},"tap":{"typecheck":false,"coverage-map":"map.js"},"dist":{"shasum":"7f7b8bf1e13115227813738688a8bb95e7b23dc5","tarball":"http://registry-npm.ywwl.com/@tapjs/mock/download/@tapjs/mock-4.0.6.tgz","fileCount":93,"integrity":"sha512-OXYZdCVkPLekw9zyTL+ysfA8tAA6lg5xOx40V9xQuwQWBhnYyUWaz9WYHUXeQKQtFE00zFAJ9dNWUzTEknePEA==","signatures":[{"sig":"MEUCIQCPL/bZhixHsquLJIjxv4I5F4f6KvEluTBrqvzKIrGdSQIgCxpOMF6ieCDCJ/OPnA+7YBK+3Bh5T7FzKUK0tXKJBWI=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":220930,"size":37519,"noattachment":false,"key":"/@tapjs/mock/-/@tapjs/mock-4.0.6.tgz"},"main":"./dist/commonjs/index.js","tshy":{"main":true,"exports":{".":"./src/index.ts","./import":"./src/import.mts","./loader":"./src/legacy-loader.mts","./package.json":"./package.json"}},"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"}},"./import":{"import":{"types":"./dist/esm/import.d.mts","default":"./dist/esm/import.mjs"}},"./loader":{"import":{"types":"./dist/esm/legacy-loader.d.mts","default":"./dist/esm/legacy-loader.mjs"}},"./package.json":"./package.json"},"funding":{"url":"https://github.com/sponsors/isaacs"},"gitHead":"1cd81f7f18ae2e4539fade0080522bb091863890","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+https://github.com/tapjs/tapjs.git","type":"git"},"_npmVersion":"11.6.2","description":"tap plugin adding t.mockRequire() and t.mockImport()","directories":{},"_nodeVersion":"24.4.1","dependencies":{"@tapjs/after":"3.0.6","@tapjs/stack":"4.0.2","walk-up-path":"^4.0.0","resolve-import":"^2.1.1"},"_hasShrinkwrap":false,"peerDependencies":{"@tapjs/core":"4.0.6"},"_npmOperationalInternal":{"tmp":"tmp/mock_4.0.6_1763575937969_0.03228568838886736","host":"s3://npm-registry-packages-npm-production"},"readme":"# `@tapjs/mock`\n\nA default tap plugin adding `t.mockRequire()`, `t.mockImport()`,\nand `t.createMock()`\n\n## USAGE\n\nThis plugin is installed with tap by default. If you had\npreviously removed it, you can `tap plugin add @tapjs/mock` to\nbring it back.\n\nThis is the way to do dependency injection at the module level.\nWhen the loaded module, or anything it loads, loads something\nthat you've mocked, it'll get your mock instead of the real\nthing. Useful for getting into those hard to trigger code paths.\n\n```ts\n// test.mts\nimport t from 'tap'\n\nt.test('handls stat failure by throwing', async t => {\n  const mockStatSync = (p: string) => {\n    t.equal(p, 'filename.txt')\n    throw Object.assign(new Error('expected error'), {\n      code: 'ENOENT',\n    })\n  }\n  // supply type param so that TS knows what it returns\n  const thingThatDoesStat = await t.mockImport<\n    typeof import('../dist/my-statty-thing.js')\n  >('../dist/my-statty-thing.js', {\n    'node:fs': { statSync: mockStatSync },\n  })\n\n  t.throws(() => thingThatDoesStat('filename.txt'), {\n    message: 'expected error',\n    code: 'ENOENT',\n  })\n})\n```\n\n### `t.mockImport(module, [mocks]): Promise<any>`\n\nLoad the module with `import()`. If any mocks are provided, then\nthey'll override the module's imported deps. This works for both\nESM and CommonJS modules.\n\n### `t.mockRequire(module, [mocks]): any`\n\nSame as `t.mockImport()`, but synchronously using `require()`\ninstead. This only works with CommonJS, and only mocks CommonJS\nmodules loaded.\n\n### `t.mockAll(mocks?: Record<string,any> | null): Record<string, any>`\n\nConvenience method to set the mocks for all subsequent calls to\n`t.mockRequire` or `t.mockImport` for the remainder of the test.\n\nMocks added with `mockAll` are overridden by any explicit mocks\nset in the `t.mockRequire` or `t.mockImport` call.\n\nRepeated calls to `t.mockAll()` will _add_ mocks to the set. If the same\nname is used again, it will replace the previous value, not merge.\n\nIf a key is set to `undefined` or `null`, then it will be removed from\nthe `mockAll` set.\n\nReset by calling `t.mockAll(null)`\n\nCall with no args to return the current `mockAll` object.\n\n### `t.createMock(originalModule, mockOverrides): mockedModule`\n\nSometimes you only want to override one function or property,\nperhaps buried deep within a module's exports, but leave all the\nrest of it intact.\n\nThis function makes it easy to do that.\n\n```ts\nimport * from 'tap'\nimport * as FS from 'node:fs'\n\nt.test('situation where we get a bogus file descriptor', async t => {\n  const { thing } = await t.mockImport<typeof import('../dist/my-thing.js')>(\n    '../dist/my-thing.js',\n    { 'node:fs': t.createMock(FS, { openSync: () => true }) }\n  )\n  t.throws(() => thing(), {\n    // imagine this is the error we get for some reason\n    message: 'got non-numeric file descriptor: true',\n  })\n})\n```\n\n## Loader\n\nThe `t.mockImport()` function relies on the `@tapjs/mock/loader`\nloader being used, which this plugin adds to tap's set of\nloaders.\n\nIf you run tests directly with node, and they use `t.mockImport`\nthen you'll have to include `--loader=@tapjs/mock/loader` to the\ncommand line arguments ahead of the main script filename.\n","publish_time":1763575938151,"_cnpm_publish_time":1763575938151,"dist-tags":{"latest":"4.4.11","pre":"1.0.0"}}