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