Debian Codegraph @

packages/babel-preset-env/test/normalize-options.skip-bundled.js

raw symbols SCIP
1 import _normalizeOptions, {
2 checkDuplicateIncludeExcludes,
3 validateModulesOption,
4 validateUseBuiltInsOption,
5 normalizePluginName,
6 } from "../lib/normalize-options.js";
7 const normalizeOptions = _normalizeOptions.default || _normalizeOptions;
8
9 describe("normalize-options", () => {
10 describe("normalizeOptions", () => {
11 it("should return normalized `include` and `exclude`", () => {
12 const normalized = normalizeOptions({
13 include: [
14 "babel-plugin-transform-spread",
15 "transform-classes",
16 "@babel/plugin-transform-unicode-regex",
17 "@babel/transform-block-scoping",
18 ],
19 exclude: [
20 "babel-plugin-transform-for-of",
21 "transform-parameters",
22 "@babel/plugin-transform-regenerator",
23 "@babel/transform-new-target",
24 ],
25 });
26 expect(normalized.include).toEqual([
27 "transform-spread",
28 "transform-classes",
29 "transform-unicode-regex",
30 "transform-block-scoping",
31 ]);
32 expect(normalized.exclude).toEqual([
33 "transform-for-of",
34 "transform-parameters",
35 "transform-regenerator",
36 "transform-new-target",
37 ]);
38 });
39
40 it("should not normalize babel-plugin with prefix", () => {
41 const normalized = normalizePluginName("prefix-babel-plugin-postfix");
42 expect(normalized).toBe("prefix-babel-plugin-postfix");
43 });
44
45 test.each`
46 include | exclude
47 ${["babel-plugin-transform-spread"]} | ${["transform-spread"]}
48 ${["@babel/plugin-transform-spread"]} | ${["transform-spread"]}
49 ${["transform-spread"]} | ${["babel-plugin-transform-spread"]}
50 ${["transform-spread"]} | ${["@babel/plugin-transform-spread"]}
51 ${["babel-plugin-transform-spread"]} | ${["@babel/plugin-transform-spread"]}
52 ${["@babel/plugin-transform-spread"]} | ${["babel-plugin-transform-spread"]}
53 ${["@babel/plugin-transform-spread"]} | ${["@babel/transform-spread"]}
54 ${["@babel/transform-spread"]} | ${["@babel/plugin-transform-spread"]}
55 ${["babel-plugin-transform-spread"]} | ${["@babel/transform-spread"]}
56 ${["@babel/transform-spread"]} | ${["babel-plugin-transform-spread"]}
57 `(
58 "should throw if with includes $include and excludes $exclude",
59 ({ include, exclude }) => {
60 expect(() => normalizeOptions({ include, exclude })).toThrow(
61 /were found in both/,
62 );
63 },
64 );
65
66 it("should not throw if corejs version is valid", () => {
67 [2, 2.1, 3, 3.5].forEach(corejs => {
68 ["entry", "usage"].forEach(useBuiltIns => {
69 expect(() => normalizeOptions({ useBuiltIns, corejs })).not.toThrow();
70 });
71 });
72 });
73
74 it("should throw if corejs version is invalid", () => {
75 [1, 1.2, 4, 4.5].forEach(corejs => {
76 ["entry", "usage"].forEach(useBuiltIns => {
77 expect(() => normalizeOptions({ useBuiltIns, corejs })).toThrow(
78 /The version passed to `corejs` is invalid./,
79 );
80 });
81 });
82 });
83
84 it("throws when including module plugins", () => {
85 expect(() =>
86 normalizeOptions({ include: ["proposal-dynamic-import"] }),
87 ).toThrow();
88 expect(() =>
89 normalizeOptions({ include: ["transform-modules-amd"] }),
90 ).toThrow();
91 });
92
93 it("allows exclusion of module plugins", () => {
94 expect(() =>
95 normalizeOptions({ exclude: ["proposal-dynamic-import"] }),
96 ).not.toThrow();
97 expect(() =>
98 normalizeOptions({ exclude: ["transform-modules-commonjs"] }),
99 ).not.toThrow();
100 });
101 });
102
103 describe("Config format validation", () => {
104 it("should throw if top-level option not found", () => {
105 const unknownTopLevelOption = () => {
106 normalizeOptions({ unknown: "option" });
107 };
108 expect(unknownTopLevelOption).toThrow();
109 });
110 });
111
112 describe("RegExp include/exclude", () => {
113 it("should not allow invalid plugins in `include` and `exclude`", () => {
114 const normalizeWithNonExistingPlugin = () => {
115 normalizeOptions({
116 include: ["non-existing-plugin"],
117 });
118 };
119 expect(normalizeWithNonExistingPlugin).toThrow(Error);
120 });
121
122 it("should expand regular expressions in `include` and `exclude`", () => {
123 const normalized = normalizeOptions({
124 include: ["^[a-z]*-spread", "babel-plugin-transform-classes"],
125 });
126 expect(normalized.include).toEqual([
127 "transform-spread",
128 "transform-classes",
129 ]);
130 });
131
132 it("should expand regular expressions in `include` and `exclude`", () => {
133 const normalized = normalizeOptions({
134 useBuiltIns: "entry",
135 corejs: 3,
136 exclude: ["es.math.log.*"],
137 });
138 expect(normalized.exclude).toEqual([
139 "es.math.log10",
140 "es.math.log1p",
141 "es.math.log2",
142 ]);
143 });
144
145 it("should work both with proposal-* and transform-*", () => {
146 expect(
147 normalizeOptions({ include: ["proposal-.*-regex"] }).include,
148 ).toEqual([
149 "transform-dotall-regex",
150 "transform-unicode-property-regex",
151 "transform-named-capturing-groups-regex",
152 "transform-sticky-regex",
153 "transform-unicode-regex",
154 ]);
155
156 expect(
157 normalizeOptions({ include: ["transform-.*-regex"] }).include,
158 ).toEqual([
159 "transform-dotall-regex",
160 "transform-unicode-property-regex",
161 "transform-named-capturing-groups-regex",
162 "transform-sticky-regex",
163 "transform-unicode-regex",
164 ]);
165 });
166
167 it("should not allow the same modules in `include` and `exclude`", () => {
168 const normalizeWithNonExistingPlugin = () => {
169 normalizeOptions({
170 useBuiltIns: "entry",
171 corejs: 3,
172 include: ["es.math.log2"],
173 exclude: ["es.math.log.*"],
174 });
175 };
176 expect(normalizeWithNonExistingPlugin).toThrow(Error);
177 });
178
179 it("should not do partial match if not explicitly defined `include` and `exclude`", () => {
180 const normalized = normalizeOptions({
181 useBuiltIns: "entry",
182 corejs: 3,
183 include: ["es.reflect.set-prototype-of"],
184 exclude: ["es.reflect.set"],
185 });
186 expect(normalized.include).toEqual(["es.reflect.set-prototype-of"]);
187 expect(normalized.exclude).toEqual(["es.reflect.set"]);
188 });
189 });
190
191 describe("checkDuplicateIncludeExcludes", function () {
192 it("should throw if duplicate names in both", function () {
193 expect(() => {
194 checkDuplicateIncludeExcludes(
195 ["transform-regenerator", "map"],
196 ["transform-regenerator", "map"],
197 );
198 }).toThrow();
199 });
200
201 it("should not throw if no duplicate names in both", function () {
202 expect(() => {
203 checkDuplicateIncludeExcludes(["transform-regenerator"], ["map"]);
204 }).not.toThrow();
205 });
206 });
207
208 describe("validateModulesOption", () => {
209 it("`undefined` option returns auto", () => {
210 expect(validateModulesOption()).toBe("auto");
211 });
212
213 it("`false` option returns false", () => {
214 expect(validateModulesOption(false)).toBe(false);
215 });
216
217 it("auto option is valid", () => {
218 expect(validateModulesOption("auto")).toBe("auto");
219 });
220
221 it("commonjs option is valid", () => {
222 expect(validateModulesOption("commonjs")).toBe("commonjs");
223 });
224
225 it("systemjs option is valid", () => {
226 expect(validateModulesOption("systemjs")).toBe("systemjs");
227 });
228
229 it("amd option is valid", () => {
230 expect(validateModulesOption("amd")).toBe("amd");
231 });
232
233 it("umd option is valid", () => {
234 expect(validateModulesOption("umd")).toBe("umd");
235 });
236
237 it("`true` option is invalid", () => {
238 expect(() => {
239 validateModulesOption(true);
240 }).toThrow();
241 });
242
243 it("`'false'` option is invalid", () => {
244 expect(() => {
245 validateModulesOption("false");
246 }).toThrow();
247 });
248
249 it("array option is invalid", () => {
250 expect(() => {
251 validateModulesOption([]);
252 }).toThrow();
253 });
254 });
255
256 describe("validateUseBuiltInsOptions", () => {
257 it("usage option is valid", () => {
258 expect(validateUseBuiltInsOption("usage")).toBe("usage");
259 });
260
261 it("entry option is valid", () => {
262 expect(validateUseBuiltInsOption("entry")).toBe("entry");
263 });
264
265 it("`false` option returns false", () => {
266 expect(validateUseBuiltInsOption(false)).toBe(false);
267 });
268
269 it("`'false'` option is invalid", () => {
270 expect(() => {
271 validateUseBuiltInsOption("false");
272 }).toThrow();
273 });
274 });
275 });