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@tapjs/mock@4.3.0/dist/commonjs/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { plugin as AfterPlugin } from '@tapjs/after'\nimport { TapPlugin, TestBase } from '@tapjs/core'\nimport * as stack from '@tapjs/stack'\nimport { isBuiltin } from 'node:module'\nimport { mockRequire } from './mock-require.js'\nimport { MockService } from './mock-service.js'\n\n/**\n * Implementation class providing the\n * {@link @tapjs/mock!index.TapMock#mockRequire},\n * {@link @tapjs/mock!index.TapMock#mockImport}, and\n * {@link @tapjs/mock!index.TapMock#createMock} methods.\n */\nexport class TapMock {\n  #t: TestBase\n  #didTeardown: boolean = false\n  #mocks: MockService[] = []\n\n  #allMock: Record<string, any>\n  static #refs = new Map<TestBase, TapMock>()\n\n  constructor(t: TestBase) {\n    this.#t = t\n    TapMock.#refs.set(t, this)\n    // inherit #allMock\n    const p = t.parent && TapMock.#refs.get(t.parent)\n    this.#allMock = Object.assign(Object.create(null), p ? p.#allMock : {})\n  }\n\n  /**\n   * Convenience method to create a mock from an existing object by\n   * overriding some (possibly deeply nested) methods or properties.\n   *\n   * Example:\n   *\n   * ```ts\n   * import * as fs from 'node:fs'\n   * const mockedThing = t.mockRequire('./module.js', t.createMock(\n   *   { fs },\n   *   { fs: { statSync: myMockedStatSync }}\n   * )\n   * ```\n   *\n   * This can also appear anywhere in the object hierarchy, which may\n   * be more convenient in some cases:\n   *\n   * ```ts\n   * import * as blah from '@long-name/blah-api'\n   * const mockedThing = t.mockRequire('./module.js', {\n   *   fs: t.createMock(fs, { statSync: myMockedStatSync }),\n   *   child_process: t.createMock(child_process, { spawn: mockSpawn }),\n   *   '@long-name/blah-api': t.createMock(blah, {\n   *     some: {\n   *       nested: {\n   *         prop: true\n   *       }\n   *     }\n   *   })\n   * })\n   * ```\n   *\n   * To *remove* a property, set it as undefined in the override.\n   *\n   * @group Spies, Mocks, and Fixtures\n   */\n  createMock<\n    B extends { [k: PropertyKey]: any } | Array<any>,\n    O extends { [k: string]: any } | Array<any>,\n  >(bases: B, overrides: O): MockedObject<B, O> {\n    if (Array.isArray(overrides))\n      return overrides as unknown as MockedObject<B, O>\n    const mockedObject = Object.fromEntries(\n      Object.entries(bases)\n        .map(([k, v]) => {\n          if (k in overrides) {\n            const bobj = !!v && typeof v === 'object'\n            const oobj = !!overrides[k] && typeof overrides[k] === 'object'\n            if (oobj && bobj) {\n              return [k, this.createMock(v, overrides[k])]\n            } else {\n              return [k, overrides[k]]\n            }\n          }\n          return [k, v]\n        })\n        .concat(\n          Object.entries(overrides).filter(\n            ([k]) => !Object.hasOwnProperty.call(bases, k),\n          ),\n        ),\n    ) as MockedObject<B, O>\n\n    if (Object.getPrototypeOf(bases) !== Object.prototype) {\n      Object.setPrototypeOf(mockedObject, Object.getPrototypeOf(bases))\n    }\n\n    return mockedObject\n  }\n\n  #builtTest?: TestBase & TapMock\n  #bt() {\n    return (this.#builtTest ??= this.#t.t as unknown as TestBase & TapMock)\n  }\n\n  /**\n   * Deprecated alias for {@link @tapjs/mock!index.TapMock#mockRequire}\n   *\n   * Prints a warning to stderr the first time it used, otherwise\n   * identical.\n   *\n   * @group Spies, Mocks, and Fixtures\n   *\n   * @deprecated\n   */\n  mock<T = any>(module: string, mocks: { [k: string]: any } = {}): T {\n    /* c8 ignore start */\n    const { mock: m } = this.#bt()\n    const at = stack.at(m)?.toJSON() || ''\n    /* c8 ignore stop */\n    console.error(\n      't.mock() is now t.mockRequire(). Please update your tests.',\n      at,\n    )\n    return mockRequire(module, mocks, m) as T\n  }\n\n  /**\n   * Load the supplied module asynchronously using import(),\n   * replacing any of the referenced modules with the mocks provided.\n   *\n   * Works with either ESM or CommonJS modules, but as with `import()` of\n   * CommonJS modules, the `module.exports` value will be set as the\n   * `default` property on the resolved object, making\n   * {@link @tapjs/mock!index.TapMock#mockRequire} somewhat more intuitive in\n   * those cases.\n   *\n   * For type info, cast using `as typeof import(...)` or use the type\n   * parameter, as TypeScript lacks a way to infer imports dynamically.\n   *\n   * For example:\n   *\n   * ```ts\n   * const myThing = await t.mockImport<\n   *   typeof import('../my-thing.js')\n   * >('../my-thing.js', {\n   *   some: { tricky: 'mocks' },\n   * })\n   * ```\n   *\n   * Note: The terms \"mock\" and \"import\" are unfortunately very overloaded in\n   * the testing space. This is **not** \"mock all imports of this module\". It's\n   * \"load this module, but with its imports mocked\". The code of the target\n   * module is run normally, but its dependencies are injected with the\n   * supplied values, which is useful for triggering hard-to-reach error cases\n   * and other situations.\n   *\n   * It is also useful for just loading a fresh copy of a module in your tests,\n   * if for example your program behaves differently based on environment\n   * variables or other system settings. For example:\n   *\n   * ```ts\n   * t.test('windows behavior', async t => {\n   *   t.intercept(process, 'platform', { value: 'win32' })\n   *   const myThing = t.mockImport('../my-thing.js')\n   *   t.equal(myThing.separator, '\\\\')\n   * })\n   * t.test('posix behavior', async t => {\n   *   t.intercept(process, 'platform', { value: 'linux' })\n   *   const myThing = t.mockImport('../my-thing.js')\n   *   t.equal(myThing.separator, '/')\n   * })\n   * ```\n   *\n   * @group Spies, Mocks, and Fixtures\n   */\n  async mockImport<T = any>(\n    module: string,\n    mocks: Record<string, any> = {},\n  ): Promise<T> {\n    if (isBuiltin(module)) {\n      this.#t.t.currentAssert = this.#bt().mockImport\n      this.#t.t.fail(\n        'Node built-in modules cannot have their imports mocked',\n      )\n      return {} as T\n    }\n    mocks = Object.assign({}, this.#allMock, mocks)\n    if (!this.#didTeardown && this.#t.t.pluginLoaded(AfterPlugin)) {\n      this.#didTeardown = true\n      this.#t.t.teardown(() => this.unmock())\n    }\n    const { mockImport } = this.#bt()\n    const service = await MockService.create(module, mocks, mockImport)\n    this.#mocks.push(service)\n    return Promise.resolve(service.module).then(s => import(s))\n  }\n\n  /**\n   * Load the supplied module synchronously using `require()`,\n   * replacing any of the referenced modules with the mocks provided.\n   *\n   * Only works with CommonJS modules.\n   *\n   * For type info, cast using `as typeof import(...)` or use the type\n   * parameter, as TypeScript lacks a way to infer imports dynamically.\n   *\n   * For example:\n   *\n   * ```ts\n   * const myThing = t.mockRequire<\n   *   typeof import('../my-thing.js')\n   * >('../my-thing.js', {\n   *   some: { tricky: 'mocks' },\n   * })\n   * ```\n   *\n   * @group Spies, Mocks, and Fixtures\n   */\n  mockRequire<T = any>(\n    module: string,\n    mocks: Record<string, any> = {},\n  ): T {\n    if (isBuiltin(module)) {\n      this.#t.t.currentAssert = this.#bt().mockRequire\n      this.#t.t.fail(\n        'Node built-in modules cannot have their imports mocked',\n      )\n      return {} as T\n    }\n    mocks = Object.assign({}, this.#allMock, mocks)\n    const { mockRequire: mr } = this.#bt()\n    return mockRequire(module, mocks, mr) as T\n  }\n\n  /**\n   * Set some mocks that will be used for all subsequent\n   * {@link @tapjs/mock!index.TapMock#mockImport} and\n   * {@link @tapjs/mock!index.TapMock#mockRequire} calls made by this test.\n   *\n   * Mocks added with `mockAll` are overridden by any explicit mocks set in the\n   * `t.mockRequire` or `t.mockImport` call.\n   *\n   * Repeated calls to `t.mockAll()` will *add* mocks to the set. If the same\n   * name is used again, it will replace the previous value, not merge.\n   *\n   * If a key is set to `undefined` or `null`, then it will be removed from\n   * the `mockAll` set.\n   *\n   * Reset by calling `t.mockAll(null)`\n   *\n   * Call with no args to return the current `mockAll` object.\n   */\n  mockAll(mocks?: Record<string, any> | null): Record<string, any> {\n    if (mocks === null) this.#allMock = Object.create(null)\n    else if (mocks !== undefined) {\n      this.#allMock = Object.assign(this.#allMock, mocks)\n      for (const [k, v] of Object.entries(this.#allMock)) {\n        if (v === undefined || v === null) delete this.#allMock[k]\n      }\n    }\n    return this.#allMock\n  }\n\n  /**\n   * Unwind the mocks and free up the memory at the end of the test.\n   *\n   * Called automatically if the `@tapjs/after` plugin is not disabled.\n   *\n   * @group Spies, Mocks, and Fixtures\n   */\n  unmock() {\n    for (const m of this.#mocks) {\n      m.unmock()\n    }\n  }\n}\n\n/**\n * Utility type, overrides the properties in B with the properties\n * in O, deeply nested.\n */\nexport type MockedObject<B, O> =\n  O extends Array<any> ? O\n  : B extends { [k: PropertyKey]: any } ?\n    O extends Function ? O\n    : O extends { [k: string]: any } ?\n      {\n        [k in keyof B]: k extends keyof O ? MockedObject<B[k], O[k]> : B[k]\n      }\n    : O\n  : O\n\n/**\n * Loader that supports {@link @tapjs/mock!index.TapMock#mockImport}\n */\nexport const loader = '@tapjs/mock/loader'\n\n/**\n * Importer for use with node --import\n */\nexport const importLoader = '@tapjs/mock/import'\n\n/**\n * plugin method that instantiates {@link @tapjs/mock!index.TapMock}\n */\nexport const plugin: TapPlugin<TapMock> = (t: TestBase) => new TapMock(t)\n"]}