Debian Codegraph @

packages/babel-core/test/async.js

raw
1 import path from "path";
2 import { fileURLToPath } from "url";
3 import * as babel from "../lib/index.js";
4
5 import {
6 spawnTransformAsync,
7 spawnTransformSync,
8 supportsESM,
9 } from "./helpers/esm.js";
10
11 const nodeGte8 = (...args) => {
12 // "minNodeVersion": "8.0.0" <-- For Ctrl+F when dropping node 6
13 const testFn = process.version.slice(0, 3) === "v6." ? it.skip : it;
14 testFn(...args);
15 };
16
17 describe("asynchronicity", () => {
18 const base = path.join(
19 path.dirname(fileURLToPath(import.meta.url)),
20 "fixtures",
21 "async",
22 );
23 let cwd;
24
25 beforeEach(function () {
26 cwd = process.cwd();
27 process.chdir(base);
28 });
29
30 afterEach(function () {
31 process.chdir(cwd);
32 });
33
34 describe("config file", () => {
35 describe("async function", () => {
36 nodeGte8("called synchronously", () => {
37 process.chdir("config-file-async-function");
38
39 expect(() =>
40 babel.transformSync(""),
41 ).toThrowErrorMatchingInlineSnapshot(
42 `"You appear to be using an async configuration, which your current version of Babel does` +
43 ` not support. We may add support for this in the future, but if you're on the most recent` +
44 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
45 ` return your config."`,
46 );
47 });
48
49 nodeGte8("called asynchronously", async () => {
50 process.chdir("config-file-async-function");
51
52 await expect(
53 babel.transformAsync(""),
54 ).rejects.toThrowErrorMatchingInlineSnapshot(
55 `"You appear to be using an async configuration, which your current version of Babel does` +
56 ` not support. We may add support for this in the future, but if you're on the most recent` +
57 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
58 ` return your config."`,
59 );
60 });
61 });
62
63 describe("promise", () => {
64 it("called synchronously", () => {
65 process.chdir("config-file-promise");
66
67 expect(() =>
68 babel.transformSync(""),
69 ).toThrowErrorMatchingInlineSnapshot(
70 `"You appear to be using an async configuration, which your current version of Babel does` +
71 ` not support. We may add support for this in the future, but if you're on the most recent` +
72 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
73 ` return your config."`,
74 );
75 });
76
77 it("called asynchronously", async () => {
78 process.chdir("config-file-promise");
79
80 await expect(
81 babel.transformAsync(""),
82 ).rejects.toThrowErrorMatchingInlineSnapshot(
83 `"You appear to be using an async configuration, which your current version of Babel does` +
84 ` not support. We may add support for this in the future, but if you're on the most recent` +
85 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
86 ` return your config."`,
87 );
88 });
89 });
90
91 describe("cache.using", () => {
92 nodeGte8("called synchronously", () => {
93 process.chdir("config-cache");
94
95 expect(() =>
96 babel.transformSync(""),
97 ).toThrowErrorMatchingInlineSnapshot(
98 `"You appear to be using an async cache handler, which your current version of Babel does` +
99 ` not support. We may add support for this in the future, but if you're on the most recent` +
100 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
101 ` handle your caching logic."`,
102 );
103 });
104
105 nodeGte8("called asynchronously", async () => {
106 process.chdir("config-cache");
107
108 await expect(
109 babel.transformAsync(""),
110 ).rejects.toThrowErrorMatchingInlineSnapshot(
111 `"You appear to be using an async cache handler, which your current version of Babel does` +
112 ` not support. We may add support for this in the future, but if you're on the most recent` +
113 ` version of @babel/core and still seeing this error, then you'll need to synchronously` +
114 ` handle your caching logic."`,
115 );
116 });
117 });
118 });
119
120 describe("plugin", () => {
121 describe("factory function", () => {
122 nodeGte8("called synchronously", () => {
123 process.chdir("plugin");
124
125 expect(() => babel.transformSync("")).toThrow(
126 `[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel` +
127 ` has been called synchronously`,
128 );
129 });
130
131 nodeGte8("called asynchronously", async () => {
132 process.chdir("plugin");
133
134 await expect(babel.transformAsync("")).resolves.toMatchObject({
135 code: `"success"`,
136 });
137 });
138 });
139
140 describe(".pre", () => {
141 nodeGte8("called synchronously", () => {
142 process.chdir("plugin-pre");
143
144 expect(() =>
145 babel.transformSync(""),
146 ).toThrowErrorMatchingInlineSnapshot(
147 `"unknown file: You appear to be using an plugin with an async .pre, which your current version` +
148 ` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
149 ` @babel/core version."`,
150 );
151 });
152
153 nodeGte8("called asynchronously", async () => {
154 process.chdir("plugin-pre");
155
156 await expect(
157 babel.transformAsync(""),
158 ).rejects.toThrowErrorMatchingInlineSnapshot(
159 `"unknown file: You appear to be using an plugin with an async .pre, which your current version` +
160 ` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
161 ` @babel/core version."`,
162 );
163 });
164 });
165
166 describe(".post", () => {
167 nodeGte8("called synchronously", () => {
168 process.chdir("plugin-post");
169
170 expect(() =>
171 babel.transformSync(""),
172 ).toThrowErrorMatchingInlineSnapshot(
173 `"unknown file: You appear to be using an plugin with an async .post, which your current version` +
174 ` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
175 ` @babel/core version."`,
176 );
177 });
178
179 nodeGte8("called asynchronously", async () => {
180 process.chdir("plugin-post");
181
182 await expect(
183 babel.transformAsync(""),
184 ).rejects.toThrowErrorMatchingInlineSnapshot(
185 `"unknown file: You appear to be using an plugin with an async .post, which your current version` +
186 ` of Babel does not support. If you're using a published plugin, you may need to upgrade your` +
187 ` @babel/core version."`,
188 );
189 });
190 });
191
192 describe("inherits", () => {
193 nodeGte8("called synchronously", () => {
194 process.chdir("plugin-inherits");
195
196 expect(() => babel.transformSync("")).toThrow(
197 `[BABEL] unknown file: You appear to be using an async plugin/preset, but Babel has been` +
198 ` called synchronously`,
199 );
200 });
201
202 nodeGte8("called asynchronously", async () => {
203 process.chdir("plugin-inherits");
204
205 await expect(babel.transformAsync("")).resolves.toMatchObject({
206 code: `"success 2"\n"success"`,
207 });
208 });
209 });
210
211 (supportsESM ? describe : describe.skip)(".mjs files", () => {
212 it("called synchronously", async () => {
213 process.chdir("plugin-mjs-native");
214
215 await expect(spawnTransformSync()).rejects.toThrow(
216 `[BABEL]: You appear to be using a native ECMAScript module plugin, which is` +
217 ` only supported when running Babel asynchronously.`,
218 );
219 });
220
221 it("called asynchronously", async () => {
222 process.chdir("plugin-mjs-native");
223
224 await expect(spawnTransformAsync()).resolves.toMatchObject({
225 code: `"success"`,
226 });
227 });
228 });
229 });
230
231 describe("preset", () => {
232 describe("factory function", () => {
233 nodeGte8("called synchronously", () => {
234 process.chdir("preset");
235
236 expect(() => babel.transformSync("")).toThrow(
237 `[BABEL] unknown file: You appear to be using an async plugin/preset, ` +
238 `but Babel has been called synchronously`,
239 );
240 });
241
242 nodeGte8("called asynchronously", async () => {
243 process.chdir("preset");
244
245 await expect(babel.transformAsync("")).resolves.toMatchObject({
246 code: `"success"`,
247 });
248 });
249 });
250
251 describe("plugins", () => {
252 nodeGte8("called synchronously", () => {
253 process.chdir("preset-plugin-promise");
254
255 expect(() => babel.transformSync("")).toThrow(
256 `[BABEL] unknown file: You appear to be using a promise as a plugin, which your` +
257 ` current version of Babel does not support. If you're using a published` +
258 ` plugin, you may need to upgrade your @babel/core version. As an` +
259 ` alternative, you can prefix the promise with "await".`,
260 );
261 });
262
263 nodeGte8("called asynchronously", async () => {
264 process.chdir("preset-plugin-promise");
265
266 await expect(babel.transformAsync("")).rejects.toThrow(
267 `[BABEL] unknown file: You appear to be using a promise as a plugin, which your` +
268 ` current version of Babel does not support. If you're using a published` +
269 ` plugin, you may need to upgrade your @babel/core version. As an` +
270 ` alternative, you can prefix the promise with "await".`,
271 );
272 });
273 });
274
275 (supportsESM ? describe : describe.skip)(".mjs files", () => {
276 it("called synchronously", async () => {
277 process.chdir("preset-mjs-native");
278
279 await expect(spawnTransformSync()).rejects.toThrow(
280 `[BABEL]: You appear to be using a native ECMAScript module preset, which is` +
281 ` only supported when running Babel asynchronously.`,
282 );
283 });
284
285 it("called asynchronously", async () => {
286 process.chdir("preset-mjs-native");
287
288 await expect(spawnTransformAsync()).resolves.toMatchObject({
289 code: `"success"`,
290 });
291 });
292
293 it("must use the 'default' export", async () => {
294 process.chdir("preset-mjs-named-exports-native");
295
296 await expect(spawnTransformAsync()).rejects.toThrow(
297 `Unexpected falsy value: undefined`,
298 );
299 });
300 });
301 });
302 });