/
helper.spec.js
49 lines (42 loc) 路 1.43 KB
/
helper.spec.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
import * as babel from "@babel/core";
import fs from "fs";
import { createRequire } from "module";
import transformTypeofSymbol from "..";
const require = createRequire(import.meta.url);
const readFile = path =>
new Promise((resolve, reject) =>
fs.readFile(path, "utf8", (err, contents) => {
if (err) reject(err);
else resolve(contents);
}),
);
describe("@babel/plugin-transform-typeof-symbol", () => {
test.each`
runtime | type
${"@babel/runtime"} | ${"esm"}
${"@babel/runtime"} | ${"cjs"}
${"@babel/runtime-corejs2"} | ${"esm"}
${"@babel/runtime-corejs2"} | ${"cjs"}
${"@babel/runtime-corejs3"} | ${"esm"}
${"@babel/runtime-corejs3"} | ${"cjs"}
`(
"shouldn't transpile the $type $runtime helper",
async ({ type, runtime }) => {
let path = require.resolve(`${runtime}/helpers/typeof`);
if (runtime === "esm") path = path.replace(".js", ".mjs");
const src = await readFile(path);
const ast = babel.parseSync(src, {
configFile: false,
sourceType: type === "esm" ? "module" : "script",
});
const withPlugin = babel.transformFromAstSync(ast, src, {
configFile: false,
plugins: [transformTypeofSymbol],
});
const withoutPlugin = babel.transformFromAstSync(ast, src, {
configFile: false,
});
expect(withPlugin.code).toBe(withoutPlugin.code);
},
);
});