{"dist":{"shasum":"c08d2af4b0eb1adb95bd87d98c713718827fd43c","tarball":"https://registry.npmjs.org/@node-loaders/mock/-/mock-1.1.0.tgz","fileCount":63,"integrity":"sha512-PHtvJSjLv2wrn0I+p8tC4/lZlSqdAyETBelfHrukuMlKp5NPBQ64gf8jRvpZfQWUYP0LLRSSZNn2wwyCjuBFyw==","signatures":[{"sig":"MEYCIQDVy2fwjsJ6CQ7G7KONCeorAOAluTdKg50zlyFdMYLhbQIhAPx9lOT1J7Q2SR5CglCR//S1OV8aN3Mas8wPQY/nWelO","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":92294},"name":"@node-loaders/mock","type":"module","types":"./dist/index-default.d.ts","readme":"# @node-loaders/mock\n\nLoader that mocks imported/required modules.\n\n## Features\n\n- ESM support\n- CJS support\n- Mixed CJS/ESM support\n- Framework agnostic\n- Typescript support (using a typescript loader, except CJS)\n- Delegates the actual module import to the chain, keeping compatibility with others loaders (like typescript)\n- No cache manipulation is necessary, no drawbacks like [why-is-proxyquire-messing-with-my-require-cache](https://github.com/thlorenz/proxyquire#why-is-proxyquire-messing-with-my-require-cache)\n- Compatible with Typescript's `esModuleInterop`\n- Full or partial mock support\n- Unused mock detection\n- Configurable mocked module import deep using `maxDepth` (symbol) property.\n- Trivial to use\n\n## Design\n\nMock uses an unique approach.\nImporting using mock will create an alternative version of the module using mocked file names.\nESM supportes url, mock adds necessary metadata to the url search parameters. CJS uses absolute paths, the metadata is stored and passed using a uuid appended to the file path folowing a `.mock` extension.\nThe mocked files will continue until `maxDepth` is reached (-1 by default, deep import).\n\nPrior art: [proxyquire](https://github.com/thlorenz/proxyquire), [esmock](https://github.com/iambumblehead/esmock).\n\n## Usage\n\nFor configuration tools, refer to [usage](https://github.com/node-loaders/loaders#usage)\n\nWhen using in combination with others @node-loaders modules make sure to use [@node-loaders/auto](https://github.com/node-loaders/loaders/tree/main/workspaces/auto#node-loadersauto) for better interoperability .\n\n### Loaders\n\n- `@node-loaders/mock` provides esm(import) and commonjs(require) mocking.\n- `@node-loaders/mock/esm` provides esm(import) mocking only.\n\n### ESM\n\nImporting a module with mocked dependencies:\n\n```js\nimport { importMock, checkMocks } from '@node-loaders/mock';\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(async () => {\n    const defaultSpy = spy();\n    const joinSpy = spy();\n\n    const mockedModule = await importMock('./module.js', {\n      '../src/path.js': {\n        default: defaultSpy,\n        join: joinSpy,\n      },\n    });\n\n    expect(joinSpy).toBeCalled();\n    expect(defaultSpy).toBeCalled();\n  });\n});\n```\n\nFull mocks:\n\n```js\nimport { importMock, checkMocks, fullMock } from '@node-loaders/mock';\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(async () => {\n    const joinSpy = spy();\n\n    const mockedModule = await importMock('./module.js', {\n      '../src/path.js': {\n        [fullMock]: true, // If a name other than 'join' is imported, a 'Module does not provide an export named' is thrown.\n        join: joinSpy,\n      },\n    });\n  });\n});\n```\n\nMock check:\n\n```js\nimport { importMock, checkMocks, checkMock, ignoreUnused } from '@node-loaders/mock';\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(async () => {\n    const joinSpy = spy();\n    const resolveSpy = spy();\n\n    const mockedModule = await importMock('./module.js', {\n      '../src/path.js': {\n        join: joinSpy,\n      },\n      'node:path': {\n        [ignoreUnused]: true,\n        resolve: resolveSpy,\n      },\n    });\n\n    checkMock(mockedModule); // throws error if '../src/path.js' was not imported but not on 'node:path'\n  });\n});\n```\n\nMax depth:\n\n```js\nimport { importMock, checkMocks, maxDepth } from '@node-loaders/mock';\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(async () => {\n    const joinSpy = spy();\n    const resolveSpy = spy();\n\n    const mockedModule = await importMock('./module.js', {\n      '../src/path.js': {\n        join: joinSpy, // Mocked when imported from './module.js' file only\n      },\n      'node:path': {\n        [maxDepth]: -1, // Mocked a configurable deep\n        resolve: resolveSpy,\n      },\n    });\n  });\n});\n```\n\n### CommonJS\n\nFrom an esm module:\n\n```js\nimport { mockRequire, checkMocks } from '@node-loaders/mock';\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(() => {\n    const defaultSpy = spy();\n    const joinSpy = spy();\n\n    const mockedModule = mockRequire('./module.cjs', {\n      '../src/path.cjs': {\n        default: defaultSpy,\n        join: joinSpy,\n      },\n    });\n\n    expect(joinSpy).toBeCalled();\n    expect(defaultSpy).toBeCalled();\n  });\n});\n```\n\nFrom a cjs module:\n\n```cjs\nconst { mockRequire } = require('@node-loaders/mock/require');\n\ndescribe(() => {\n  afterAll(() => {\n    checkMocks(); // Detects for unused mocks to avoid mistakes on import changes.\n  });\n\n  it(() => {\n    const defaultSpy = spy();\n    const joinSpy = spy();\n\n    const mockedModule = mockRequire('./module.cjs', {\n      '../src/path.cjs': {\n        default: defaultSpy,\n        join: joinSpy,\n      },\n    });\n\n    expect(joinSpy).toBeCalled();\n    expect(defaultSpy).toBeCalled();\n  });\n});\n```\n\n### Known issues\n\nCommonJS (require) mocking doesn't mock typescript modules.\n\n## License\n\nMIT\n","engines":{"node":"^16.13.0 || >=18.12.0"},"exports":{".":{"types":"./dist/index-default.d.ts","default":"./dist/index-default.js"},"./require":{"types":"./dist/export-require.d.cts","require":"./dist/export-require.cjs"}},"gitHead":"3375740be1456ec28ff7e3c6acffdbc94dfd062c","scripts":{"fix":"xo --fix","test":"mocha","build":"tsc","clean":"rimraf dist","pretest":"xo","clean-all":"npm run clean && rimraf node_modules","test-coverage":"c8 mocha"},"_npmUser":{"name":"mshima","email":"marceloshima@gmail.com"},"keywords":["esm","import","loader","node","mock","esmock","proxyquire","rewire"],"_npmVersion":"lerna/6.6.2/node@v16.20.0+arm64 (darwin)","description":"Loader that mocks imported/required modules.","directories":{},"maintainers":[{"name":"mshima","email":"marceloshima@gmail.com"}],"_nodeVersion":"16.20.0","dependencies":{"stack-utils":"^2.0.6","@node-loaders/core":"^1.0.2"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"readmeFilename":"README.md","devDependencies":{"@types/stack-utils":"^2.0.1"},"_npmOperationalInternal":{"tmp":"tmp/mock_1.1.0_1684197421214_0.5366729763147504","host":"s3://npm-registry-packages"},"_id":"@node-loaders/mock@1.1.0","version":"1.1.0"}