Debian Codegraph @

packages/babel-core/test/option-manager.js

raw
1 import * as babel from "../lib/index.js";
2 import path from "path";
3 import { fileURLToPath } from "url";
4
5 const cwd = path.dirname(fileURLToPath(import.meta.url));
6
7 function loadOptions(opts) {
8 return babel.loadOptionsSync({ cwd, ...opts });
9 }
10
11 function loadOptionsAsync(opts) {
12 return babel.loadOptionsAsync({ cwd, ...opts });
13 }
14
15 describe("option-manager", () => {
16 it("throws for babel 5 plugin", () => {
17 return expect(() => {
18 loadOptions({
19 plugins: [({ Plugin }) => new Plugin("object-assign", {})],
20 });
21 }).toThrow(/Babel 5 plugin is being run with an unsupported Babel/);
22 });
23
24 describe("config plugin/preset flattening and overriding", () => {
25 function makePlugin() {
26 const calls = [];
27 const plugin = (api, opts) => {
28 calls.push(opts);
29 return {};
30 };
31 return { plugin, calls };
32 }
33
34 it("should throw when an option is provided as a preset", () => {
35 expect(() => {
36 loadOptions({
37 presets: [
38 "./fixtures/option-manager/babel-preset-bar",
39 { useBuiltIns: "entry" },
40 ],
41 });
42 }).toThrowErrorMatchingSnapshot();
43 });
44
45 it("should throw when an option is provided as a plugin", () => {
46 expect(() => {
47 loadOptions({
48 plugins: [
49 "./fixtures/option-manager/babel-plugin-foo",
50 { useSpread: true },
51 ],
52 });
53 }).toThrowErrorMatchingSnapshot();
54 });
55
56 it("should throw when an option is following a preset", () => {
57 expect(() => {
58 loadOptions({
59 presets: [
60 "./fixtures/option-manager/babel-plugin-foo",
61 "./fixtures/option-manager/babel-preset-bar",
62 { useSpread: true },
63 ],
64 });
65 }).toThrowErrorMatchingSnapshot();
66 });
67
68 it("should not throw when a preset string followed by valid preset object", async () => {
69 const { plugin } = makePlugin();
70 expect(
71 await loadOptionsAsync({
72 presets: [
73 "@babel/env",
74 { plugins: [[plugin, undefined, "my-plugin"]] },
75 ],
76 }),
77 ).toBeTruthy();
78 });
79
80 it("should throw if a plugin name is repeated, with information about the repeated plugin", () => {
81 const { calls, plugin } = makePlugin();
82
83 expect(() => {
84 loadOptions({
85 plugins: [
86 [plugin, undefined, "my-plugin"],
87 [plugin, undefined, "my-plugin"],
88 ],
89 });
90 }).toThrow(
91 /Duplicate plugin\/preset detected.*Duplicates detected are.*my-plugin.*my-plugin/ms,
92 );
93 expect(calls).toEqual([]);
94 });
95
96 it("throws for null options", () => {
97 const { calls, plugin } = makePlugin();
98 expect(() => {
99 loadOptions({
100 plugins: [[plugin, null]],
101 });
102 }).toThrow(".plugins[0][1] must be an object, false, or undefined");
103
104 expect(calls).toEqual([]);
105 });
106
107 it("should not throw if a repeated plugin has a different name", () => {
108 const { calls, plugin } = makePlugin();
109
110 loadOptions({
111 plugins: [
112 [plugin, { arg: 1 }],
113 [plugin, { arg: 2 }, "some-name"],
114 ],
115 });
116 expect(calls).toEqual([{ arg: 1 }, { arg: 2 }]);
117 });
118
119 it("should merge .env[] plugins with parent presets", () => {
120 const { calls: calls1, plugin: plugin1 } = makePlugin();
121 const { calls: calls2, plugin: plugin2 } = makePlugin();
122
123 loadOptions({
124 envName: "test",
125 plugins: [[plugin1, { arg: 1 }]],
126 env: {
127 test: {
128 plugins: [
129 [plugin1, { arg: 3 }],
130 [plugin2, { arg: 2 }],
131 ],
132 },
133 },
134 });
135 expect(calls1).toEqual([{ arg: 3 }]);
136 expect(calls2).toEqual([{ arg: 2 }]);
137 });
138
139 it("should throw if a preset is repeated", () => {
140 const { calls, plugin: preset } = makePlugin();
141
142 expect(() => {
143 loadOptions({
144 presets: [preset, preset],
145 });
146 }).toThrow(/Duplicate plugin\/preset detected/);
147 expect(calls).toEqual([]);
148 });
149
150 it("should not throw if a repeated preset has a different name", () => {
151 const { calls, plugin: preset } = makePlugin();
152
153 loadOptions({
154 presets: [
155 [preset, { arg: 1 }],
156 [preset, { arg: 2 }, "some-name"],
157 ],
158 });
159 expect(calls).toEqual([{ arg: 1 }, { arg: 2 }]);
160 });
161 it("should merge .env[] presets with parent presets", () => {
162 const { calls: calls1, plugin: preset1 } = makePlugin();
163 const { calls: calls2, plugin: preset2 } = makePlugin();
164
165 loadOptions({
166 envName: "test",
167 presets: [[preset1, { arg: 1 }]],
168 env: {
169 test: {
170 presets: [
171 [preset1, { arg: 3 }],
172 [preset2, { arg: 2 }],
173 ],
174 },
175 },
176 });
177 expect(calls1).toEqual([{ arg: 3 }]);
178 expect(calls2).toEqual([{ arg: 2 }]);
179 });
180
181 it("should not merge .env[] presets with parent presets when passPerPreset", () => {
182 const { calls: calls1, plugin: preset1 } = makePlugin();
183 const { calls: calls2, plugin: preset2 } = makePlugin();
184
185 loadOptions({
186 envName: "test",
187 passPerPreset: true,
188 presets: [[preset1, { arg: 1 }]],
189 env: {
190 test: {
191 presets: [
192 [preset1, { arg: 3 }],
193 [preset2, { arg: 2 }],
194 ],
195 },
196 },
197 });
198 expect(calls1).toEqual([{ arg: 1 }, { arg: 3 }]);
199 expect(calls2).toEqual([{ arg: 2 }]);
200 });
201 });
202
203 describe("mergeOptions", () => {
204 it("throws for removed babel 5 options", () => {
205 return expect(() => {
206 loadOptions({
207 randomOption: true,
208 });
209 }).toThrow(/Unknown option: .randomOption/);
210 });
211
212 it("throws for removed babel 5 options", () => {
213 return expect(() => {
214 loadOptions({
215 auxiliaryComment: true,
216 blacklist: true,
217 });
218 }).toThrow(
219 // eslint-disable-next-line max-len
220 /Using removed Babel 5 option: .auxiliaryComment - Use `auxiliaryCommentBefore` or `auxiliaryCommentAfter`/,
221 );
222 });
223
224 it("throws for resolved but erroring preset", () => {
225 return expect(() => {
226 loadOptions({
227 presets: [path.join(cwd, "fixtures/option-manager/not-a-preset")],
228 });
229 }).toThrow(
230 /While processing: .*option-manager(?:\/|\\\\)not-a-preset\.js/,
231 );
232 });
233 });
234
235 describe("presets", function () {
236 it.each([
237 "es5_function",
238 "es5_object",
239 "es2015_default_function",
240 "es2015_default_object",
241 ])("%p should work", async name => {
242 const options = await loadOptionsAsync({
243 presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
244 });
245
246 expect(Array.isArray(options.plugins)).toBe(true);
247 expect(options.plugins).toHaveLength(1);
248 expect(options.presets).toHaveLength(0);
249 });
250
251 it.each([
252 ["es2015_named", /Must export a default export when using ES6 modules/],
253 ["es2015_invalid", /Unsupported format: string/],
254 ["es5_invalid", /Unsupported format: string/],
255 ])("%p should throw %p", async (name, msg) => {
256 await expect(
257 loadOptionsAsync({
258 presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
259 }),
260 ).rejects.toThrow(msg);
261 });
262 });
263 });