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