Debian Codegraph @

packages/babel-preset-env/test/index.skip-bundled.js

raw symbols SCIP
1 // eslint-disable-next-line import/extensions
2 import compatData from "@babel/compat-data/plugins";
3
4 import * as babelPresetEnv from "../lib/index.js";
5
6 import _removeRegeneratorEntryPlugin from "../lib/polyfills/regenerator.js";
7 import _pluginLegacyBabelPolyfill from "../lib/polyfills/babel-polyfill.js";
8 import _transformations from "../lib/module-transformations.js";
9 import _availablePlugins from "../lib/available-plugins.js";
10 const removeRegeneratorEntryPlugin =
11 _removeRegeneratorEntryPlugin.default || _removeRegeneratorEntryPlugin;
12 const pluginLegacyBabelPolyfill =
13 _pluginLegacyBabelPolyfill.default || _pluginLegacyBabelPolyfill;
14 const transformations = _transformations.default || _transformations;
15 const availablePlugins = _availablePlugins.default || _availablePlugins;
16
17 // We need to load the correct plugins version (ESM or CJS),
18 // because our tests rely on function identity.
19 let pluginCoreJS2, pluginCoreJS3, pluginRegenerator;
20 import _pluginCoreJS2_esm from "babel-plugin-polyfill-corejs2";
21 import _pluginCoreJS3_esm from "babel-plugin-polyfill-corejs3";
22 import _pluginRegenerator_esm from "babel-plugin-polyfill-regenerator";
23 import { createRequire } from "module";
24 // eslint-disable-next-line @babel/development-internal/require-default-import-fallback
25 if (/* commonjs */ _transformations.default) {
26 const require = createRequire(import.meta.url);
27
28 pluginCoreJS2 = require("babel-plugin-polyfill-corejs2").default;
29 pluginCoreJS3 = require("babel-plugin-polyfill-corejs3").default;
30 pluginRegenerator = require("babel-plugin-polyfill-regenerator").default;
31 } else {
32 pluginCoreJS2 = _pluginCoreJS2_esm;
33 pluginCoreJS3 = _pluginCoreJS3_esm;
34 pluginRegenerator = _pluginRegenerator_esm;
35 }
36
37 describe("babel-preset-env", () => {
38 describe("transformIncludesAndExcludes", () => {
39 it("should return in transforms array", () => {
40 expect(
41 babelPresetEnv.transformIncludesAndExcludes([
42 "transform-arrow-functions",
43 ]),
44 ).toEqual({
45 all: ["transform-arrow-functions"],
46 plugins: new Set(["transform-arrow-functions"]),
47 builtIns: new Set(),
48 });
49 });
50
51 it("should return in built-ins array", () => {
52 expect(babelPresetEnv.transformIncludesAndExcludes(["es.map"])).toEqual({
53 all: ["es.map"],
54 plugins: new Set(),
55 builtIns: new Set(["es.map"]),
56 });
57 });
58 });
59 describe("getModulesPluginNames", () => {
60 describe("modules is set to false", () => {
61 it("returns only syntax plugins", () => {
62 expect(
63 babelPresetEnv.getModulesPluginNames({
64 modules: false,
65 transformations,
66 shouldTransformESM: false,
67 shouldTransformDynamicImport: false,
68 shouldTransformExportNamespaceFrom: false,
69 }),
70 ).toEqual(["syntax-dynamic-import", "syntax-export-namespace-from"]);
71 });
72 });
73 describe("modules is not set to false", () => {
74 describe("ESMs should not be transformed", () => {
75 it("returns syntax plugins", () => {
76 expect(
77 babelPresetEnv.getModulesPluginNames({
78 modules: "commonjs",
79 transformations,
80 shouldTransformESM: false,
81 shouldTransformDynamicImport: false,
82 shouldTransformExportNamespaceFrom: false,
83 }),
84 ).toEqual(["syntax-dynamic-import", "syntax-export-namespace-from"]);
85 });
86 });
87 describe("ESMs should be transformed", () => {
88 describe("dynamic imports should not be transformed", () => {
89 it("returns specified modules transform and syntax-dynamic-import", () => {
90 expect(
91 babelPresetEnv.getModulesPluginNames({
92 modules: "commonjs",
93 transformations,
94 shouldTransformESM: true,
95 shouldTransformDynamicImport: false,
96 shouldTransformExportNamespaceFrom: false,
97 }),
98 ).toEqual([
99 "transform-modules-commonjs",
100 "syntax-dynamic-import",
101 "syntax-export-namespace-from",
102 ]);
103 });
104 });
105 describe("dynamic imports should be transformed", () => {
106 it("returns specified modules transform and transform-dynamic-import", () => {
107 expect(
108 babelPresetEnv.getModulesPluginNames({
109 modules: "systemjs",
110 transformations,
111 shouldTransformESM: true,
112 shouldTransformDynamicImport: true,
113 shouldTransformExportNamespaceFrom: false,
114 }),
115 ).toEqual([
116 "transform-modules-systemjs",
117 "transform-dynamic-import",
118 "syntax-export-namespace-from",
119 ]);
120 });
121 describe("export namespace from should be transformed", () => {
122 it("works", () => {
123 expect(
124 babelPresetEnv.getModulesPluginNames({
125 modules: "systemjs",
126 transformations,
127 shouldTransformESM: true,
128 shouldTransformDynamicImport: true,
129 shouldTransformExportNamespaceFrom: true,
130 }),
131 ).toEqual([
132 "transform-modules-systemjs",
133 "transform-dynamic-import",
134 "transform-export-namespace-from",
135 ]);
136 });
137 });
138 });
139 });
140 });
141 });
142 describe("getPolyfillPlugins", () => {
143 const staticProps = {
144 polyfillTargets: [],
145 include: new Set(),
146 exclude: new Set(),
147 proposals: false,
148 shippedProposals: false,
149 debug: false,
150 };
151 describe("useBuiltIns is false", () => {
152 it("returns no polyfill plugins", () => {
153 expect(
154 babelPresetEnv.getPolyfillPlugins(
155 Object.assign(
156 {
157 useBuiltIns: false,
158 corejs: false,
159 regenerator: false,
160 },
161 staticProps,
162 ),
163 ),
164 ).toEqual([]);
165 });
166 });
167 describe("useBuiltIns is not false", () => {
168 describe("corejs is not given", () => {
169 it("returns no polyfill plugins", () => {
170 expect(
171 babelPresetEnv.getPolyfillPlugins(
172 Object.assign(
173 {
174 useBuiltIns: "usage",
175 corejs: false,
176 regenerator: false,
177 },
178 staticProps,
179 ),
180 ),
181 ).toEqual([]);
182 });
183 });
184 describe("useBuiltIns is set to usage", () => {
185 describe("using corejs 2", () => {
186 it("returns an array with core js 2 usage plugin", () => {
187 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
188 Object.assign(
189 {
190 useBuiltIns: "usage",
191 corejs: { major: 2 },
192 regenerator: false,
193 },
194 staticProps,
195 ),
196 );
197 expect(polyfillPlugins.length).toBe(2);
198 expect(polyfillPlugins[0][0]).toEqual(pluginCoreJS2);
199 expect(polyfillPlugins[1][0]).toEqual(pluginLegacyBabelPolyfill);
200 });
201 });
202 describe("using corejs 3", () => {
203 describe("regenerator is set to false", () => {
204 it("returns an array with core js 3 usage plugin", () => {
205 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
206 Object.assign(
207 {
208 useBuiltIns: "usage",
209 corejs: { major: 3 },
210 regenerator: false,
211 },
212 staticProps,
213 ),
214 );
215 expect(polyfillPlugins.length).toBe(2);
216 expect(polyfillPlugins[0][0]).toEqual(pluginCoreJS3);
217 expect(polyfillPlugins[1][0]).toEqual(pluginLegacyBabelPolyfill);
218 });
219 });
220
221 describe("regenerator is set to true", () => {
222 it("returns an array with core js 3 usage plugin and add regenerator usage plugin", () => {
223 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
224 Object.assign(
225 {
226 useBuiltIns: "usage",
227 corejs: { major: 3 },
228 regenerator: true,
229 },
230 staticProps,
231 ),
232 );
233 expect(polyfillPlugins.length).toBe(3);
234 expect(polyfillPlugins[0][0]).toEqual(pluginCoreJS3);
235 expect(polyfillPlugins[1][0]).toEqual(pluginLegacyBabelPolyfill);
236 expect(polyfillPlugins[2][0]).toEqual(pluginRegenerator);
237 });
238 });
239 });
240 });
241 describe("useBuiltIns is set to entry", () => {
242 describe("using corejs 2", () => {
243 it("returns an array with core js 2 entry plugin", () => {
244 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
245 Object.assign(
246 {
247 useBuiltIns: "entry",
248 corejs: { major: 2 },
249 regenerator: true,
250 },
251 staticProps,
252 ),
253 );
254 expect(polyfillPlugins.length).toBe(2);
255 expect(polyfillPlugins[0][0]).toEqual(pluginLegacyBabelPolyfill);
256 expect(polyfillPlugins[1][0]).toEqual(pluginCoreJS2);
257 });
258 });
259 describe("using corejs 3", () => {
260 describe("regenerator is set to true", () => {
261 it("returns an array with core js 3 entry plugin", () => {
262 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
263 Object.assign(
264 {
265 useBuiltIns: "entry",
266 corejs: { major: 3 },
267 regenerator: true,
268 },
269 staticProps,
270 ),
271 );
272 expect(polyfillPlugins.length).toBe(2);
273 expect(polyfillPlugins[0][0]).toEqual(pluginCoreJS3);
274 expect(polyfillPlugins[1][0]).toEqual(pluginLegacyBabelPolyfill);
275 });
276 });
277
278 describe("regenerator is set to false", () => {
279 it("returns an array with core js 3 entry plugin and remove regenerator entry plugin", () => {
280 const polyfillPlugins = babelPresetEnv.getPolyfillPlugins(
281 Object.assign(
282 {
283 useBuiltIns: "entry",
284 corejs: { major: 3 },
285 regenerator: false,
286 },
287 staticProps,
288 ),
289 );
290 expect(polyfillPlugins.length).toBe(3);
291 expect(polyfillPlugins[0][0]).toEqual(pluginCoreJS3);
292 expect(polyfillPlugins[1][0]).toEqual(pluginLegacyBabelPolyfill);
293 expect(polyfillPlugins[2][0]).toEqual(
294 removeRegeneratorEntryPlugin,
295 );
296 });
297 });
298 });
299 });
300 });
301 });
302
303 it("available-plugins is in sync with @babel/compat-data", () => {
304 const arrAvailablePlugins = Object.keys(availablePlugins).sort();
305 const arrCompatData = Object.keys(compatData)
306 // TODO(Babel 8): Remove this .map
307 .map(name => name.replace("proposal-", "transform-"))
308 .sort();
309
310 expect(arrAvailablePlugins).toEqual(expect.arrayContaining(arrCompatData));
311 });
312 });