{"record":{"id":"54a82cb4a7b07b6a","repo":"denoland/deno","slug":"cannot-create-mock-because-named-exports-cannot-be","errorCode":null,"errorMessage":"Cannot create mock because named exports cannot be applied to the provided default export.","messagePattern":"Cannot create mock because named exports cannot be applied to the provided default export\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/testing.ts","lineNumber":3180,"sourceCode":"}\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) {\n  let 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.isCjs) {\n    // For a CommonJS module the named exports are applied onto the default\n    // export object, mirroring how Deno exposes a required CJS module to an","sourceCodeStart":3162,"sourceCodeEnd":3198,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/testing.ts#L3162-L3198","documentation":"When a module mock supplies both named exports and a default export, the generated CommonJS shim sets module.exports = defaultExport and then copies each named export onto it, which only works if the default is an object. The shim's guard (module.exports === null || typeof module.exports !== 'object') throws kBadExportsMessage, so a function, primitive, or null default combined with namedExports fails at require() time.","triggerScenarios":"mock.module('pkg', { defaultExport: () => {}, namedExports: { version: '1' } }); defaultExport: 'text' with namedExports; defaultExport: null plus namedExports.","commonSituations":"Mocking CommonJS packages whose real module.exports is a function while also wanting named members; converting ESM-style mock fixtures to CJS-shaped ones.","solutions":["Expose the function as a named export instead: namedExports: { connect: fn, ... }","Omit defaultExport entirely so the shim builds its own empty object to carry named exports","Keep the function/primitive defaultExport but remove namedExports from options","Wrap everything in a plain-object defaultExport and have consumers destructure it"],"exampleFix":"// before\nmock.module('pg', { defaultExport: function query() {}, namedExports: { Pool: class {} } });\n\n// after\nmock.module('pg', { namedExports: { query: () => {}, Pool: class {} } });","handlingStrategy":"validation","validationCode":"function mockCjsModule(spec: string, defaultExport: unknown, namedExports: Record<string, unknown> | undefined): void {\n  const hasNamed = namedExports !== undefined && Object.keys(namedExports).length > 0;\n  if (hasNamed && (defaultExport === null || typeof defaultExport !== 'object')) {\n    throw new Error('namedExports need an object defaultExport; move the function to a named export');\n  }\n  mock.module(spec, defaultExport === undefined ? { namedExports } : { defaultExport, namedExports });\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine a callable default with namedExports in a module mock","Keep CJS function-style packages as named-export mocks","Add a setup-time assertion that the default export is an object when named exports are present"],"tags":["node-test","mock-module","commonjs","exports"],"backgroundTag":"invalid-module-export","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"}