Debian Codegraph @

packages/babel-core/test/errors-stacks.js

raw
1 import * as babel from "../lib/index.js";
2
3 import { fileURLToPath } from "url";
4 import path from "path";
5
6 const replaceAll = "".replaceAll
7 ? Function.call.bind("".replaceAll)
8 : (str, find, replace) => str.split(find).join(replace);
9
10 function expectError(run) {
11 try {
12 run();
13 } catch (e) {
14 let { stack } = e;
15 // Normalize windows paths
16 stack = stack.replace(/\\/g, "/");
17 // Remove absolute URLs
18 stack = replaceAll(stack, process.cwd().replace(/\\/g, "/"), "<CWD>");
19 stack = stack.replace(/file:\/\/\/?<CWD>/g, "<CWD>");
20 // Remove jest-related stack frames.
21 // The `at async Promise.all` frame comes from inside jest-light-runner and is only
22 // visible when using --run-in-band, comes from inside jest but doesn't have an
23 // associated file path.
24 stack = stack.replace(
25 /(?:\n\s*at [^\n]+?node_modules\/(?:@?jest|piscina)[^\n]+|\n\s*at async Promise.all[^\n]+)+/g,
26 "\n at ... internal jest frames ...",
27 );
28 // Remove node internal frames, since they change by version
29 stack = stack.replace(
30 /(?:\n\s*at ((?:async )?[\w.]+)? ?\((?:node:)?internal\/[^\n]+)+/g,
31 "\n at $1 (... internal node frames ...)",
32 );
33 // Replace line/column numbers, since they are affected by how
34 // the code is compiled. The first optional ?\d+ is added by Jest.
35 stack = stack.replace(/(?:\?\d+)?:\d+:\d+(\)?)$/gm, ":_:_$1");
36
37 // This is only needed because Node.js < 16 (and old Jest) stack traces
38 // are quite different from newer stack traces.
39 // TODO(Babel 8): Delete this code
40 {
41 // Node.js <= 10
42 stack = replaceAll(stack, "Object.parseSync", "Module.parseSync");
43 stack = stack.replace(
44 /(?:run|Object\.<anonymous>) \((<CWD>[^)]+)\)/g,
45 "$1",
46 );
47 stack = replaceAll(
48 stack,
49 "at ... internal jest frames ...\n at Module.parseSync",
50 "at require (... internal node frames ...)\n at Module.parseSync",
51 );
52 stack = replaceAll(
53 stack,
54 "\n at ... internal jest frames ...\n at new Promise (<anonymous>)",
55 "",
56 );
57 // Node.js 8
58 stack = stack.replace(/\n\s*at <anonymous>$/g, "");
59 // Node.js 6
60 stack = stack.replace(
61 /(at (\w+) \([^)]+\)\n\s*at) next \(native\)/g,
62 "$1 $2.next (<anonymous>)",
63 );
64 }
65
66 // expectError is a customized expect wrapper
67 // eslint-disable-next-line jest/valid-expect
68 return expect(stack);
69 }
70 throw new Error("It should have thrown an error.");
71 }
72
73 const fixture = name =>
74 path.join(
75 path.dirname(fileURLToPath(import.meta.url)),
76 "fixtures/errors",
77 name,
78 );
79
80 describe("@babel/core errors", function () {
81 it("error inside config function", function () {
82 expectError(() => {
83 babel.parseSync("foo;", {
84 root: fixture("error-config-function"),
85 });
86 }).toMatchInlineSnapshot(`
87 "Error: Error inside config!
88 at myConfig (<CWD>/packages/babel-core/test/fixtures/errors/error-config-function/babel.config.js:_:_)
89 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
90 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
91 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
92 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
93 at ... internal jest frames ..."
94 `);
95 });
96
97 it("error inside config function with more frames", function () {
98 expectError(() => {
99 babel.parseSync("foo;", {
100 root: fixture("error-config-function-more-frames"),
101 });
102 }).toMatchInlineSnapshot(`
103 "Error: Error inside config!
104 at f (<CWD>/packages/babel-core/test/fixtures/errors/error-config-function-more-frames/babel.config.js:_:_)
105 at g (<CWD>/packages/babel-core/test/fixtures/errors/error-config-function-more-frames/babel.config.js:_:_)
106 at myConfig (<CWD>/packages/babel-core/test/fixtures/errors/error-config-function-more-frames/babel.config.js:_:_)
107 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
108 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
109 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
110 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
111 at ... internal jest frames ..."
112 `);
113 });
114
115 it("error inside config file", function () {
116 expectError(() => {
117 babel.parseSync("foo;", {
118 root: fixture("error-config-file"),
119 });
120 }).toMatchInlineSnapshot(`
121 "Error: Error inside config!
122 at <CWD>/packages/babel-core/test/fixtures/errors/error-config-file/babel.config.js:_:_
123 at require (... internal node frames ...)
124 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
125 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
126 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
127 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
128 at ... internal jest frames ..."
129 `);
130 });
131
132 it("error inside config file with more frames", function () {
133 expectError(() => {
134 babel.parseSync("foo;", {
135 root: fixture("error-config-file-more-frames"),
136 });
137 }).toMatchInlineSnapshot(`
138 "Error: Error inside config!
139 at f (<CWD>/packages/babel-core/test/fixtures/errors/error-config-file-more-frames/babel.config.js:_:_)
140 at g (<CWD>/packages/babel-core/test/fixtures/errors/error-config-file-more-frames/babel.config.js:_:_)
141 at <CWD>/packages/babel-core/test/fixtures/errors/error-config-file-more-frames/babel.config.js:_:_
142 at require (... internal node frames ...)
143 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
144 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
145 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
146 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
147 at ... internal jest frames ..."
148 `);
149 });
150
151 it("invalid JSON config file", function () {
152 expectError(() => {
153 babel.parseSync("foo;", {
154 root: fixture("invalid-json"),
155 });
156 }).toMatchInlineSnapshot(`
157 "Error: Error while parsing config - JSON5: invalid character '}' at 3:1
158 at <CWD>/packages/babel-core/test/fixtures/errors/invalid-json/babel.config.json
159 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
160 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
161 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
162 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
163 at ... internal jest frames ..."
164 `);
165 });
166
167 it("use 'exclude' without filename", function () {
168 expectError(() => {
169 babel.parseSync("foo;", {
170 root: fixture("use-exclude"),
171 });
172 }).toMatchInlineSnapshot(`
173 "Error: Configuration contains string/RegExp pattern, but no filename was passed to Babel
174 at <CWD>/packages/babel-core/test/fixtures/errors/use-exclude/babel.config.js
175 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
176 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
177 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
178 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
179 at ... internal jest frames ..."
180 `);
181 });
182
183 it("use 'exclude' without filename in programmatic options", function () {
184 expectError(() => {
185 babel.parseSync("foo;", {
186 configFile: false,
187 exclude: /node_modules/,
188 });
189 }).toMatchInlineSnapshot(`
190 "Error: Configuration contains string/RegExp pattern, but no filename was passed to Babel
191 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
192 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
193 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
194 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
195 at ... internal jest frames ..."
196 `);
197 });
198
199 it("use 'exclude' without filename in preset", function () {
200 expectError(() => {
201 babel.parseSync("foo;", {
202 root: fixture("use-exclude-in-preset"),
203 });
204 }).toMatchInlineSnapshot(`
205 "Error: [BABEL] unknown file: Preset /* your preset */ requires a filename to be set when babel is called directly,
206 \`\`\`
207 babel.transformSync(code, { filename: 'file.ts', presets: [/* your preset */] });
208 \`\`\`
209 See https://babeljs.io/docs/en/options#filename for more information.
210 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
211 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
212 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
213 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
214 at ... internal jest frames ..."
215 `);
216 });
217
218 it("invalid option", function () {
219 expectError(() => {
220 babel.parseSync("foo;", {
221 root: fixture("invalid-option"),
222 });
223 }).toMatchInlineSnapshot(`
224 "Error: .sourceType must be \\"module\\", \\"script\\", \\"unambiguous\\", or undefined
225 at <CWD>/packages/babel-core/test/fixtures/errors/invalid-option/babel.config.json
226 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
227 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
228 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
229 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
230 at ... internal jest frames ..."
231 `);
232 });
233
234 it("invalid option in programmatic options", function () {
235 expectError(() =>
236 babel.parseSync("foo;", {
237 root: fixture("valid"),
238 sourceType: "foo",
239 }),
240 ).toMatchInlineSnapshot(`
241 "Error: .sourceType must be \\"module\\", \\"script\\", \\"unambiguous\\", or undefined
242 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
243 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
244 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
245 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
246 at ... internal jest frames ..."
247 `);
248 });
249
250 it("internal errors have the full stack trace", function () {
251 expectError(() => {
252 const { map } = Array.prototype;
253 try {
254 Array.prototype.map = () => {
255 throw new Error("Internal error! This is a fake bug :)");
256 };
257 babel.parseSync("foo;", {
258 root: fixture("valid"),
259 });
260 } finally {
261 Array.prototype.map = map;
262 }
263 }).toMatchInlineSnapshot(`
264 "Error: Internal error! This is a fake bug :)
265 at Array.map (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
266 at loadOneConfig (<CWD>/packages/babel-core/lib/config/files/configuration.js:_:_)
267 at loadOneConfig.next (<anonymous>)
268 at buildRootChain (<CWD>/packages/babel-core/lib/config/config-chain.js:_:_)
269 at buildRootChain.next (<anonymous>)
270 at loadPrivatePartialConfig (<CWD>/packages/babel-core/lib/config/partial.js:_:_)
271 at loadPrivatePartialConfig.next (<anonymous>)
272 at loadFullConfig (<CWD>/packages/babel-core/lib/config/full.js:_:_)
273 at loadFullConfig.next (<anonymous>)
274 at parse (<CWD>/packages/babel-core/lib/parse.js:_:_)
275 at parse.next (<anonymous>)
276 at evaluateSync (<CWD>/node_modules/gensync/index.js:_:_)
277 at sync (<CWD>/node_modules/gensync/index.js:_:_)
278 at stopHiding - secret - don't use this - v1 (<CWD>/packages/babel-core/lib/errors/rewrite-stack-trace.js:_:_)
279 at Module.parseSync (<CWD>/packages/babel-core/lib/parse.js:_:_)
280 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
281 at expectError (<CWD>/packages/babel-core/test/errors-stacks.js:_:_)
282 at <CWD>/packages/babel-core/test/errors-stacks.js:_:_
283 at ... internal jest frames ..."
284 `);
285 });
286 });