(
from: string,
modulePath: string,
context: VMContext,
)
| 1270 | } |
| 1271 | |
| 1272 | private loadCjsAsEsm( |
| 1273 | from: string, |
| 1274 | modulePath: string, |
| 1275 | context: VMContext, |
| 1276 | ): SyntheticModule | Promise<VMModule> { |
| 1277 | const registry = this.registries.getActiveEsmRegistry(); |
| 1278 | const cached = registry.get(modulePath); |
| 1279 | if (cached) { |
| 1280 | return cached as SyntheticModule | Promise<VMModule>; |
| 1281 | } |
| 1282 | |
| 1283 | let synthetic: SyntheticModule; |
| 1284 | try { |
| 1285 | synthetic = this.buildCjsAsEsmSyntheticModule(from, modulePath, context); |
| 1286 | } catch (error) { |
| 1287 | if (!isCjsParseError(error)) { |
| 1288 | throw error; |
| 1289 | } |
| 1290 | // The file may contain ESM syntax with no ESM marker (.mjs / |
| 1291 | // "type":"module") - try loading as native ESM. If the ESM parser also |
| 1292 | // rejects it, the original CJS error was the genuine one. |
| 1293 | return this.loadEsmModule(modulePath).catch(esmError => { |
| 1294 | throw isError(esmError) && esmError.name === 'SyntaxError' |
| 1295 | ? error |
| 1296 | : esmError; |
| 1297 | }); |
| 1298 | } |
| 1299 | |
| 1300 | const evaluated = evaluateSyntheticModule(synthetic); |
| 1301 | registry.set(modulePath, evaluated); |
| 1302 | return evaluated; |
| 1303 | } |
| 1304 | |
| 1305 | private async importMock<T = unknown>( |
| 1306 | moduleName: string, |
no test coverage detected