Debian Codegraph @

babel-preset-modules/test/index.test.js

raw symbols SCIP
1 import path from "path";
2 import {
3 babel,
4 readdir,
5 readFile,
6 compressedSize,
7 compareFixturesToPresetEnv,
8 } from "./_util";
9 import chalk from "chalk";
10 import preset from "..";
11
12 jest.setTimeout(30000);
13
14 function dedent(strings, ...expr) {
15 const s = strings.reduce((acc, s, i) => acc + expr[i - 1] + s);
16 const dent = s.match(/^(\t| {2,})+/m)[0];
17 return s.trim().replace(new RegExp("^" + dent, "gm"), "");
18 }
19
20 function babelPretty(code) {
21 return babel(code, { presets: [preset], compact: false, retainLines: true });
22 }
23
24 describe("@babel/preset-modules", () => {
25 it("should assert Babel 7", () => {
26 expect(preset).toEqual(expect.any(Function));
27 const assertVersion = jest.fn();
28 preset(
29 {
30 assertVersion,
31 },
32 {}
33 );
34 expect(assertVersion).toHaveBeenCalledWith(7);
35 });
36
37 it("should return a configuration object with plugins", () => {
38 expect(preset).toEqual(expect.any(Function));
39 const config = preset(
40 {
41 assertVersion() {},
42 },
43 {}
44 );
45 expect(config).toHaveProperty("plugins");
46 expect(config.plugins).toEqual(expect.any(Array));
47 });
48
49 it("should pass a smoke test", () => {
50 expect(
51 babelPretty(dedent`
52 const foo = async (tag, { a = 1 }, { data: { props = 1 } }, ...children) => {
53 return a;
54 };
55
56 const ignoreMe = () => {
57 const { a = 1 } = {};
58 };
59 `)
60 ).toMatchSnapshot();
61 });
62
63 it("should produce minimal output", async () => {
64 for (const f of await readdir(path.join(__dirname, "fixtures"))) {
65 if (!/\.js$/.test(f)) continue;
66 const file = await readFile(path.join(__dirname, "fixtures", f), "utf-8");
67 const out = babel(file, { presets: [preset] });
68 const sizeBefore = await compressedSize(file);
69 const size = await compressedSize(out);
70 const fixture = f.replace(/\.js$/, "");
71 const title = `Fixture - ${fixture}:`;
72 const delta = size - sizeBefore;
73 const diff = (((size - sizeBefore) / (sizeBefore || 1)) * 100) | 0;
74 const multiplier = (size / (sizeBefore || 1)).toFixed(2);
75 const sign = delta >= 0 ? "+" : "";
76 const stats = delta
77 ? dedent`
78 ${title}
79 • ${size}b (${sign}${delta}b from original ${sizeBefore}b)
80 • ${multiplier}x original size, a ${diff}% difference
81 `
82 : dedent`
83 ${title}
84 • ${size}b (no change)
85 `;
86 const snap = stats + `\nCode:\n${out.replace(/^/gm, "> ")}\n`;
87 expect(snap).toMatchSnapshot(fixture);
88 }
89 });
90
91 it("should be smaller than preset-env default", async () => {
92 const out = await compareFixturesToPresetEnv(
93 path.join(__dirname, "browser/fixtures"),
94 {
95 compile: ({ file }) => babel(file, { presets: [preset] }),
96 compileEnv: ({ file }) =>
97 babel(file, { presets: ["@babel/preset-env"] }),
98 assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv),
99 }
100 );
101 console.log(
102 chalk.blueBright(`Compared to @babel/preset-env's default output:`) + out
103 );
104 });
105
106 it("should be smaller than preset-env targets.esmodules", async () => {
107 const out = await compareFixturesToPresetEnv(
108 path.join(__dirname, "browser/fixtures"),
109 {
110 compile: ({ file }) => babel(file, { presets: [preset] }),
111 compileEnv: ({ file }) =>
112 babel(file, {
113 presets: [
114 [
115 "@babel/preset-env",
116 {
117 targets: { esmodules: true },
118 },
119 ],
120 ],
121 }),
122 assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv),
123 }
124 );
125 console.log(
126 chalk.blueBright(
127 `Compared to @babel/preset-env's targets.esmodules output:`
128 ) + out
129 );
130 });
131
132 it("should be smaller than preset-env targets.esmodules in loose mode", async () => {
133 const out = await compareFixturesToPresetEnv(
134 path.join(__dirname, "browser/fixtures"),
135 {
136 compile: ({ file }) => babel(file, { presets: [preset] }),
137 compileEnv: ({ file }) =>
138 babel(file, {
139 presets: [
140 [
141 "@babel/preset-env",
142 {
143 targets: { esmodules: true },
144 loose: true,
145 },
146 ],
147 ],
148 }),
149 assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv),
150 }
151 );
152 console.log(
153 chalk.blueBright(
154 `Compared to @babel/preset-env's targets.esmodules ${chalk.bold`loose`} output:`
155 ) + out
156 );
157 });
158 });