{"record":{"id":"fd8f733c57bbc515","repo":"denoland/deno","slug":"mock-exports-not-found-for-key","errorCode":null,"errorMessage":"mock exports not found for \"${key}\"","messagePattern":"mock exports not found for \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/testing.ts","lineNumber":3173,"sourceCode":"  if (\n    StringPrototypeStartsWith(key, \"http:\") ||\n    StringPrototypeStartsWith(key, \"https:\")\n  ) {\n    return \"module\";\n  }\n  return \"commonjs\";\n}\n\nfunction registryAccessSource() {\n  return \"globalThis[Symbol.for(\" + JSONStringify(kMockModuleRegistryName) +\n    \")]\";\n}\n\nfunction generateCjsSource(entry, key) {\n  let src = '\"use strict\";\\n';\n  src += \"const $e = \" + registryAccessSource() + \".get(\" +\n    JSONStringify(key) + \");\\n\";\n  src += \"if ($e === undefined) { throw new Error(\" +\n    JSONStringify('mock exports not found for \"' + key + '\"') + \"); }\\n\";\n  if (entry.hasDefaultExport) {\n    src += \"module.exports = $e.moduleExports.default;\\n\";\n  }\n  if (entry.exportNames.length > 0) {\n    src += \"if (module.exports === null || typeof module.exports !== \" +\n      '\"object\") { throw new Error(' + JSONStringify(kBadExportsMessage) +\n      \"); }\\n\";\n    for (let i = 0; i < entry.exportNames.length; i++) {\n      const name = entry.exportNames[i];\n      src += \"module.exports[\" + JSONStringify(name) + \"] = \" +\n        \"$e.moduleExports[\" + JSONStringify(name) + \"];\\n\";\n    }\n  }\n  return src;\n}\n\nfunction generateEsmSource(entry, key) {","sourceCodeStart":3155,"sourceCodeEnd":3191,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/testing.ts#L3155-L3191","documentation":"mock.module() registers the fake on a global registry (globalThis[Symbol.for('deno.internal.nodeTestMockModules')]) keyed by resolved specifier, and installs a generated CommonJS shim that looks the entry up at require() time. If the registry has no entry for that key when the shim runs, it throws this error. The entry is deleted on restore/reset, so the usual cause is requiring the module after the mock was torn down, or a specifier resolving to a different key.","triggerScenarios":"const m = mock.module('pkg'); m.restore(); require('pkg') afterwards; a lazily-required dependency executing after afterEach ran restoreAllMocks; mocking './util' while the code under test requires a path that resolves to a different key.","commonSituations":"Teardown ordering where a deferred require runs after afterEach; specifier spelling/relative-path mismatches between the mock call and the required id; cached compiled shims outliving their registry entry.","solutions":["Keep the mock active for the whole lifetime of the module instance - restore only in afterEach, after the test has finished requiring and using it","Mock the exact specifier the code under test requires so both resolve to the same key","Require the module inside the test body after mock.module(), not at top level before mocks exist","Re-register with mock.module() before requiring again in a later test"],"exampleFix":"// before\nm.restore();\nconst pkg = require('pkg'); // shim throws: mock exports not found\n\n// after\n// keep the mock active until the test is done\nconst pkg = require('pkg');\nm.restore();","handlingStrategy":"validation","validationCode":"const REGISTRY = Symbol.for('deno.internal.nodeTestMockModules');\nfunction isModuleMocked(spec: string): boolean {\n  const reg = (globalThis as Record<symbol, Map<string, unknown> | undefined>)[REGISTRY];\n  return reg?.get(spec) !== undefined; // internal key: resolution must match\n}","typeGuard":null,"tryCatchPattern":"try { require('pkg'); } catch (e) { if (/mock exports not found/.test(e.message)) { throw new Error('pkg required outside its mock lifetime'); } throw e; }","preventionTips":["Require mocked modules inside the test body, never at module top level","Restore module mocks only in afterEach after usage ends","Use one shared constant for specifier strings between mock and require"],"tags":["node-test","mock-module","commonjs","registry"],"backgroundTag":"module-not-registered","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}