-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
411 lines (353 loc) 路 11.1 KB
/
index.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* eslint-env jest */
import * as babel from "@babel/core";
import { buildExternalHelpers } from "@babel/core";
import getFixtures from "@babel/helper-fixtures";
import sourceMap from "source-map";
import { codeFrameColumns } from "@babel/code-frame";
import defaults from "lodash.defaults";
import escapeRegExp from "lodash.escaperegexp";
import * as helpers from "./helpers";
import extend from "lodash.assignin";
import merge from "lodash.merge";
import resolve from "resolve";
import assert from "assert";
import fs from "fs";
import path from "path";
import vm from "vm";
import checkDuplicatedNodes from "babel-check-duplicated-nodes";
import diff from "jest-diff";
const moduleCache = {};
const testContext = vm.createContext({
...helpers,
process: process,
transform: babel.transform,
setTimeout: setTimeout,
setImmediate: setImmediate,
expect,
});
testContext.global = testContext;
// Initialize the test context with the polyfill, and then freeze the global to prevent implicit
// global creation in tests, which could cause things to bleed between tests.
runModuleInTestContext("@babel/polyfill", __filename);
// Populate the "babelHelpers" global with Babel's helper utilities.
runCodeInTestContext(buildExternalHelpers(), {
filename: path.join(__dirname, "babel-helpers-in-memory.js"),
});
/**
* A basic implementation of CommonJS so we can execute `@babel/polyfill` inside our test context.
* This allows us to run our unittests
*/
function runModuleInTestContext(id: string, relativeFilename: string) {
const filename = resolve.sync(id, {
basedir: path.dirname(relativeFilename),
});
// Expose Node-internal modules if the tests want them. Note, this will not execute inside
// the context's global scope.
if (filename === id) return require(id);
if (moduleCache[filename]) return moduleCache[filename].exports;
const module = (moduleCache[filename] = {
id: filename,
exports: {},
});
const dirname = path.dirname(filename);
const req = id => runModuleInTestContext(id, filename);
const src = fs.readFileSync(filename, "utf8");
const code = `(function (exports, require, module, __filename, __dirname) {\n${src}\n});`;
vm.runInContext(code, testContext, {
filename,
displayErrors: true,
lineOffset: -1,
}).call(module.exports, module.exports, req, module, filename, dirname);
return module.exports;
}
/**
* Run the given snippet of code inside a CommonJS module.
*
* Exposed for unit tests, not for use as an API.
*/
export function runCodeInTestContext(code: string, opts: { filename: string }) {
const filename = opts.filename;
const dirname = path.dirname(filename);
const req = id => runModuleInTestContext(id, filename);
const module = {
id: filename,
exports: {},
};
const oldCwd = process.cwd();
try {
if (opts.filename) process.chdir(path.dirname(opts.filename));
// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently
// rely on 'this === global'.
const src = `(function(exports, require, module, __filename, __dirname, opts) {\n${code}\n});`;
return vm.runInContext(src, testContext, {
filename,
displayErrors: true,
lineOffset: -1,
})(module.exports, req, module, filename, dirname, opts);
} finally {
process.chdir(oldCwd);
}
}
function wrapPackagesArray(type, names, optionsDir) {
return (names || []).map(function (val) {
if (typeof val === "string") val = [val];
// relative path (outside of monorepo)
if (val[0][0] === ".") {
if (!optionsDir) {
throw new Error(
"Please provide an options.json in test dir when using a " +
"relative plugin path.",
);
}
val[0] = path.resolve(optionsDir, val[0]);
} else {
const monorepoPath = __dirname + "/../../babel-" + type + "-" + val[0];
if (fs.existsSync(monorepoPath)) {
val[0] = monorepoPath;
}
}
return val;
});
}
function run(task) {
const {
actual,
expect: expected,
exec,
options: opts,
optionsDir,
validateLogs,
ignoreOutput,
stdout,
stderr,
} = task;
function getOpts(self) {
const newOpts = merge(
{
cwd: path.dirname(self.loc),
filename: self.loc,
filenameRelative: self.filename,
sourceFileName: self.filename,
sourceType: "script",
babelrc: false,
inputSourceMap: task.inputSourceMap || undefined,
},
opts,
);
newOpts.plugins = wrapPackagesArray("plugin", newOpts.plugins, optionsDir);
newOpts.presets = wrapPackagesArray(
"preset",
newOpts.presets,
optionsDir,
).map(function (val) {
if (val.length > 3) {
throw new Error(
"Unexpected extra options " +
JSON.stringify(val.slice(3)) +
" passed to preset.",
);
}
return val;
});
return newOpts;
}
let execCode = exec.code;
let result;
let resultExec;
if (execCode) {
const execOpts = getOpts(exec);
result = babel.transform(execCode, execOpts);
checkDuplicatedNodes(babel, result.ast);
execCode = result.code;
try {
resultExec = runCodeInTestContext(execCode, execOpts);
} catch (err) {
// Pass empty location to include the whole file in the output.
err.message =
`${exec.loc}: ${err.message}\n` + codeFrameColumns(execCode, {});
throw err;
}
}
const inputCode = actual.code;
const expectedCode = expected.code;
if (!execCode || inputCode) {
const actualLogs = { stdout: "", stderr: "" };
let restoreSpies = null;
if (validateLogs) {
const spy1 = jest.spyOn(console, "log").mockImplementation(msg => {
actualLogs.stdout += `${msg}\n`;
});
const spy2 = jest.spyOn(console, "warn").mockImplementation(msg => {
actualLogs.stderr += `${msg}\n`;
});
restoreSpies = () => {
spy1.mockRestore();
spy2.mockRestore();
};
}
result = babel.transform(inputCode, getOpts(actual));
if (restoreSpies) restoreSpies();
const outputCode = normalizeOutput(result.code);
checkDuplicatedNodes(babel, result.ast);
if (!ignoreOutput) {
if (
!expected.code &&
outputCode &&
!opts.throws &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
const expectedFile = expected.loc.replace(
/\.m?js$/,
result.sourceType === "module" ? ".mjs" : ".js",
);
console.log(`New test file created: ${expectedFile}`);
fs.writeFileSync(expectedFile, `${outputCode}\n`);
if (expected.loc !== expectedFile) {
try {
fs.unlinkSync(expected.loc);
} catch (e) {}
}
} else {
validateFile(outputCode, expected.loc, expectedCode);
if (inputCode) {
expect(expected.loc).toMatch(
result.sourceType === "module" ? /\.mjs$/ : /\.js$/,
);
}
}
}
if (validateLogs) {
validateFile(normalizeOutput(actualLogs.stdout), stdout.loc, stdout.code);
validateFile(normalizeOutput(actualLogs.stderr), stderr.loc, stderr.code);
}
}
if (task.sourceMap) {
expect(result.map).toEqual(task.sourceMap);
}
if (task.sourceMappings) {
const consumer = new sourceMap.SourceMapConsumer(result.map);
task.sourceMappings.forEach(function (mapping) {
const actual = mapping.original;
const expected = consumer.originalPositionFor(mapping.generated);
expect({ line: expected.line, column: expected.column }).toEqual(actual);
});
}
if (execCode && resultExec) {
return resultExec;
}
}
function validateFile(actualCode, expectedLoc, expectedCode) {
try {
expect(actualCode).toEqualFile({
filename: expectedLoc,
code: expectedCode,
});
} catch (e) {
if (!process.env.OVERWRITE) throw e;
console.log(`Updated test file: ${expectedLoc}`);
fs.writeFileSync(expectedLoc, `${actualCode}\n`);
}
}
function normalizeOutput(code) {
const projectRoot = path.resolve(__dirname, "../../../");
const cwdSymbol = "<CWD>";
let result = code
.trim()
// (non-win32) /foo/babel/packages -> <CWD>/packages
// (win32) C:\foo\babel\packages -> <CWD>\packages
.replace(new RegExp(escapeRegExp(projectRoot), "g"), cwdSymbol);
if (process.platform === "win32") {
result = result
// C:/foo/babel/packages -> <CWD>/packages
.replace(
new RegExp(escapeRegExp(projectRoot.replace(/\\/g, "/")), "g"),
cwdSymbol,
)
// C:\\foo\\babel\\packages -> <CWD>\\packages (in js string literal)
.replace(
new RegExp(escapeRegExp(projectRoot.replace(/\\/g, "\\\\")), "g"),
cwdSymbol,
);
}
return result;
}
const toEqualFile = () => ({
compare: (actual, { filename, code }) => {
const pass = actual === code;
return {
pass,
message: () => {
const diffString = diff(code, actual, {
expand: false,
});
return (
`Expected ${filename} to match transform output.\n` +
`To autogenerate a passing version of this file, delete the file and re-run the tests.\n\n` +
`Diff:\n\n${diffString}`
);
},
};
},
negativeCompare: () => {
throw new Error("Negation unsupported");
},
});
export default function (
fixturesLoc: string,
name: string,
suiteOpts = {},
taskOpts = {},
dynamicOpts?: Function,
) {
const suites = getFixtures(fixturesLoc);
for (const testSuite of suites) {
if (suiteOpts.ignoreSuites?.includes(testSuite.title)) continue;
describe(name + "/" + testSuite.title, function () {
jest.addMatchers({
toEqualFile,
});
for (const task of testSuite.tests) {
if (
suiteOpts.ignoreTasks?.includes(task.title) ||
suiteOpts.ignoreTasks?.includes(testSuite.title + "/" + task.title)
) {
continue;
}
const testFn = task.disabled ? it.skip : it;
testFn(
task.title,
function () {
function runTask() {
run(task);
}
defaults(task.options, {
sourceMap: !!(task.sourceMappings || task.sourceMap),
});
extend(task.options, taskOpts);
if (dynamicOpts) dynamicOpts(task.options, task);
const throwMsg = task.options.throws;
if (throwMsg) {
// internal api doesn't have this option but it's best not to pollute
// the options object with useless options
delete task.options.throws;
assert.throws(runTask, function (err) {
return throwMsg === true || err.message.indexOf(throwMsg) >= 0;
});
} else {
if (task.exec.code) {
const result = run(task);
if (result && typeof result.then === "function") {
return result;
}
} else {
runTask();
}
}
},
);
}
});
}
}