uire-commonjs') ? 'commonjs' : original.format; const result = { __proto__: null, format, shortCircuit: true, source: await createSourceFromMock(mock, format), }; debug('load hook finished, result = %o', result); return result; } async function createSourceFromMock(mock, format) { // Create mock implementation from provided exports. const { exportNames, hasDefaultExport, url } = mock; const useESM = format === 'module' || format === 'module-typescript'; const source = `${testImportSource(useESM)} if (!$__test.mock._mockExports.has(${JSONStringify(url)})) { throw new Error(${JSONStringify(`mock exports not found for "${url}"`)}); } const $__exports = $__test.mock._mockExports.get(${JSONStringify(url)}); ${defaultExportSource(useESM, hasDefaultExport)} ${namedExportsSource(useESM, exportNames)} `; return source; } function testImportSource(useESM) { if (useESM) { return "import $__test from 'node:test';"; } return "const $__test = require('node:test');"; } function defaultExportSource(useESM, hasDefaultExport) { if (!hasDefaultExport) { return ''; } else if (useESM) { return 'export default $__exports.defaultExport;'; } return 'module.exports = $__exports.defaultExport;'; } function namedExportsSource(useESM, exportNames) { let source = ''; if (!useESM && exportNames.length > 0) { source += ` if (module.exports === null || typeof module.exports !== 'object') { throw new Error('${JSONStringify(kBadExportsMessage)}'); } `; } for (let i = 0; i < exportNames.length; ++i) { const name = exportNames[i]; if (useESM) { source += `export let ${name} = $__exports.namedExports[${JSONStringify(name)}];\n`; } else { source += `module.exports[${JSONStringify(name)}] = $__exports.namedExports[${JSONStringify(name)}];\n`; } } return source; } function sendAck(buf, status = kMockSuccess) { AtomicsStore(buf, 0, status); AtomicsNotify(buf, 0); } module.exports = { initialize, load, resolve };