|
| 1 | +import { execFile } from "child_process"; |
| 2 | +import { createRequire } from "module"; |
| 3 | +import { outputType } from "./helpers/esm.js"; |
| 4 | + |
| 5 | +const require = createRequire(import.meta.url); |
| 6 | + |
| 7 | +async function run(name) { |
| 8 | + return new Promise((res, rej) => { |
| 9 | + execFile( |
| 10 | + process.execPath, |
| 11 | + [require.resolve(`./fixtures/esm-cjs-integration/${name}`)], |
| 12 | + { env: process.env }, |
| 13 | + (error, stdout, stderr) => { |
| 14 | + if (error) rej(error); |
| 15 | + res({ stdout: stdout.toString(), stderr: stderr.toString() }); |
| 16 | + }, |
| 17 | + ); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +(outputType === "module" ? describe : describe.skip)("usage from cjs", () => { |
| 22 | + it("lazy plugin required", async () => { |
| 23 | + expect(await run("lazy-plugin-required.cjs")).toMatchInlineSnapshot(` |
| 24 | + Object { |
| 25 | + "stderr": "", |
| 26 | + "stdout": "\\"Replaced!\\"; |
| 27 | + ", |
| 28 | + } |
| 29 | + `); |
| 30 | + }); |
| 31 | + |
| 32 | + it("lazy plugin as config string", async () => { |
| 33 | + expect(await run("lazy-plugin-as-string.cjs")).toMatchInlineSnapshot(` |
| 34 | + Object { |
| 35 | + "stderr": "", |
| 36 | + "stdout": "\\"Replaced!\\"; |
| 37 | + ", |
| 38 | + } |
| 39 | + `); |
| 40 | + }); |
| 41 | + |
| 42 | + it("eager plugin required", async () => { |
| 43 | + await expect(run("eager-plugin-required.cjs")).rejects.toThrow( |
| 44 | + "The `types` export of @babel/core is only accessible from" + |
| 45 | + " the CommonJS version after that the ESM version is loaded.", |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it("eager plugin required after dynamic esm import", async () => { |
| 50 | + expect(await run("eager-plugin-required-after-dynamic-esm-import.cjs")) |
| 51 | + .toMatchInlineSnapshot(` |
| 52 | + Object { |
| 53 | + "stderr": "", |
| 54 | + "stdout": "\\"Replaced!\\"; |
| 55 | + ", |
| 56 | + } |
| 57 | + `); |
| 58 | + }); |
| 59 | + |
| 60 | + it("eager plugin required after static esm import", async () => { |
| 61 | + expect(await run("eager-plugin-required-after-static-esm-import.mjs")) |
| 62 | + .toMatchInlineSnapshot(` |
| 63 | + Object { |
| 64 | + "stderr": "", |
| 65 | + "stdout": "\\"Replaced!\\"; |
| 66 | + ", |
| 67 | + } |
| 68 | + `); |
| 69 | + }); |
| 70 | + |
| 71 | + it("eager plugin as config string", async () => { |
| 72 | + expect(await run("eager-plugin-as-string.cjs")).toMatchInlineSnapshot(` |
| 73 | + Object { |
| 74 | + "stderr": "", |
| 75 | + "stdout": "\\"Replaced!\\"; |
| 76 | + ", |
| 77 | + } |
| 78 | + `); |
| 79 | + }); |
| 80 | + |
| 81 | + it("transformSync", async () => { |
| 82 | + await expect(run("transform-sync.cjs")).rejects.toThrow( |
| 83 | + "The `transformSync` export of @babel/core is only callable from" + |
| 84 | + " the CommonJS version after that the ESM version is loaded.", |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("transformSync after dynamic esm import", async () => { |
| 89 | + expect(await run("transform-sync-after-dynamic-esm-import.cjs")) |
| 90 | + .toMatchInlineSnapshot(` |
| 91 | + Object { |
| 92 | + "stderr": "", |
| 93 | + "stdout": "REPLACE_ME; |
| 94 | + ", |
| 95 | + } |
| 96 | + `); |
| 97 | + }); |
| 98 | + |
| 99 | + it("transformSync after static esm import", async () => { |
| 100 | + expect(await run("transform-sync-after-static-esm-import.mjs")) |
| 101 | + .toMatchInlineSnapshot(` |
| 102 | + Object { |
| 103 | + "stderr": "", |
| 104 | + "stdout": "REPLACE_ME; |
| 105 | + ", |
| 106 | + } |
| 107 | + `); |
| 108 | + }); |
| 109 | +}); |
0 commit comments