Skip to content

Commit 2d989a9

Browse files
Run tests in a native Node.js ESM environment (#13966)
1 parent 578ab22 commit 2d989a9

File tree

159 files changed

+1283
-611
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+1283
-611
lines changed

.eslintrc.cjs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ const path = require("path");
44

55
const cjsGlobals = ["__dirname", "__filename", "require", "module", "exports"];
66

7+
const testFiles = [
8+
"packages/*/test/**/*.js",
9+
"codemods/*/test/**/*.js",
10+
"eslint/*/test/**/*.js",
11+
];
12+
const sourceFiles = exts => [
13+
`packages/*/src/**/*.{${exts}}`,
14+
`codemods/*/src/**/*.{${exts}}`,
15+
`eslint/*/src/**/*.{${exts}}`,
16+
];
17+
718
module.exports = {
819
root: true,
920
plugins: [
1021
"import",
22+
"node",
1123
"jest",
1224
"prettier",
1325
"@babel/development",
@@ -36,11 +48,7 @@ module.exports = {
3648
},
3749
},
3850
{
39-
files: [
40-
"packages/*/src/**/*.{js,ts,cjs}",
41-
"codemods/*/src/**/*.{js,ts,cjs}",
42-
"eslint/*/src/**/*.{js,ts,cjs}",
43-
],
51+
files: sourceFiles("js,ts,cjs,mjs"),
4452
rules: {
4553
"@babel/development/no-undefined-identifier": "error",
4654
"@babel/development/no-deprecated-clone": "error",
@@ -51,9 +59,7 @@ module.exports = {
5159
},
5260
{
5361
files: [
54-
"packages/*/test/**/*.js",
55-
"codemods/*/test/**/*.js",
56-
"eslint/*/test/**/*.js",
62+
...testFiles,
5763
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
5864
"test/**/*.js",
5965
],
@@ -68,20 +74,21 @@ module.exports = {
6874
"jest/no-test-callback": "off",
6975
"jest/valid-describe": "off",
7076
"import/extensions": ["error", "always"],
77+
"import/no-extraneous-dependencies": "off",
7178
"no-restricted-imports": ["error", { patterns: ["**/src/**"] }],
7279
},
7380
},
7481
{
75-
files: [
76-
"packages/*/src/**/*.{js,ts}",
77-
"codemods/*/src/**/*.{js,ts}",
78-
"eslint/*/src/**/*.{js,ts}",
79-
"packages/*/test/**/*.js",
80-
"codemods/*/test/**/*.js",
81-
"eslint/*/test/**/*.js",
82-
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
83-
"test/**/*.js",
84-
],
82+
files: testFiles,
83+
rules: {
84+
"node/no-unsupported-features": [
85+
"error",
86+
{ version: "12.17.0", ignores: ["modules"] },
87+
],
88+
},
89+
},
90+
{
91+
files: [...sourceFiles("js,ts,mjs"), ...testFiles, "test/**/*.js"],
8592
excludedFiles: [
8693
// @babel/register is the require() hook, so it will always be CJS-based
8794
"packages/babel-register/**/*.{js,ts}",

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ package-lock.json
5252
/packages/babel-runtime-corejs3/core-js/**/*.js
5353
/packages/babel-runtime-corejs3/core-js-stable/**/*.js
5454

55-
/packages/babel-register/test/.babel
55+
/packages/babel-register/test/.cache.babel
56+
/packages/babel-register/test/.index.babel
5657
/packages/babel-cli/test/tmp
5758
/packages/babel-node/test/tmp
5859
/packages/*/lib
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function verifyAndAssertMessages(
3131
"../../../babel-eslint-shared-fixtures/config/babel.config.js",
3232
),
3333
},
34-
...overrideConfig?.parserOptions,
34+
...(overrideConfig && overrideConfig.parserOptions),
3535
},
3636
});
3737

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const supportsESM = parseInt(process.versions.node) >= 12;
22
const isPublishBundle = process.env.IS_PUBLISH;
33

44
module.exports = {
5+
runner: supportsESM ? "./test/jest-light-runner" : "jest-runner",
6+
57
collectCoverageFrom: [
68
"packages/*/src/**/*.{js,mjs,ts}",
79
"codemods/*/src/**/*.{js,mjs,ts}",
@@ -47,7 +49,6 @@ module.exports = {
4749
"/test/__data__/",
4850
"<rootDir>/build/",
4951
],
50-
resolver: supportsESM ? "./test/jestExportsMapResolver.cjs" : null,
5152
// We don't need module name mappers here as depedencies of workspace
5253
// package should be declared explicitly in the package.json
5354
// Yarn will generate correct file links so that Jest can resolve correctly

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"eslint-plugin-flowtype": "^8.0.3",
5454
"eslint-plugin-import": "^2.25.2",
5555
"eslint-plugin-jest": "^25.2.2",
56+
"eslint-plugin-node": "^11.1.0",
5657
"eslint-plugin-prettier": "^3.4.0",
5758
"fancy-log": "^1.3.3",
5859
"flow-bin": "^0.123.0",
@@ -61,8 +62,8 @@
6162
"gulp-filter": "^7.0.0",
6263
"gulp-plumber": "^1.2.1",
6364
"husky": "^7.0.4",
64-
"jest": "^27.2.0",
65-
"jest-worker": "^27.2.0",
65+
"jest": "^27.4.0",
66+
"jest-worker": "^27.4.0",
6667
"lint-staged": "^9.2.0",
6768
"lodash": "^4.17.21",
6869
"mergeiterator": "^1.2.5",
@@ -80,6 +81,7 @@
8081
"eslint/*",
8182
"packages/*",
8283
"test/esm",
84+
"test/jest-light-runner",
8385
"test/runtime-integration/*",
8486
"benchmark"
8587
],

packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with ignored file/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"args": ["./src/index.js"],
33
"env": {
4+
"BABEL_ENV": "test",
45
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
56
},
67
"os": ["darwin", "linux"]

packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with only file/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"args": ["./src/index.js"],
33
"env": {
4+
"BABEL_ENV": "test",
45
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
56
},
67
"os": ["darwin", "linux"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

packages/babel-cli/test/index.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ const readDir = function (loc, filter) {
4545

4646
const saveInFiles = function (files) {
4747
// Place an empty .babelrc in each test so tests won't unexpectedly get to repo-level config.
48-
if (!fs.existsSync(".babelrc")) {
49-
outputFileSync(".babelrc", "{}");
48+
if (!fs.existsSync(path.join(tmpLoc, ".babelrc"))) {
49+
outputFileSync(path.join(tmpLoc, ".babelrc"), "{}");
5050
}
5151

5252
Object.keys(files).forEach(function (filename) {
5353
const content = files[filename];
54-
outputFileSync(filename, content);
54+
outputFileSync(path.join(tmpLoc, filename), content);
5555
});
5656
};
5757

@@ -123,7 +123,7 @@ const assertTest = function (stdout, stderr, opts, cwd) {
123123
const expected = opts.outFiles[filename];
124124
const actual = actualFiles[filename];
125125

126-
expect(actual).toBe(expected ?? "");
126+
expect(actual).toBe(expected || "");
127127
}
128128
} catch (e) {
129129
e.message += "\n at " + filename;
@@ -152,7 +152,7 @@ const buildTest = function (binName, testName, opts) {
152152
args = args.concat(opts.args);
153153
const env = { ...process.env, ...opts.env };
154154

155-
const spawn = child.spawn(process.execPath, args, { env });
155+
const spawn = child.spawn(process.execPath, args, { env, cwd: tmpLoc });
156156

157157
let stderr = "";
158158
let stdout = "";
@@ -190,28 +190,18 @@ const buildTest = function (binName, testName, opts) {
190190
};
191191

192192
fs.readdirSync(fixtureLoc).forEach(function (binName) {
193-
if (binName.startsWith(".")) return;
193+
if (binName.startsWith(".") || binName === "package.json") return;
194194

195195
const suiteLoc = path.join(fixtureLoc, binName);
196196
describe("bin/" + binName, function () {
197-
let cwd;
198-
199197
beforeEach(() => {
200-
cwd = process.cwd();
201-
202198
if (fs.existsSync(tmpLoc)) {
203199
for (const child of fs.readdirSync(tmpLoc)) {
204200
rimraf.sync(path.join(tmpLoc, child));
205201
}
206202
} else {
207203
fs.mkdirSync(tmpLoc);
208204
}
209-
210-
process.chdir(tmpLoc);
211-
});
212-
213-
afterEach(() => {
214-
process.chdir(cwd);
215205
});
216206

217207
fs.readdirSync(suiteLoc).forEach(function (testName) {

packages/babel-cli/test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

packages/babel-core/test/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ describe("parser and generator options", function () {
138138
describe("api", function () {
139139
it("exposes the resolvePlugin method", function () {
140140
expect(() => babel.resolvePlugin("nonexistent-plugin")).toThrow(
141-
/Cannot resolve module 'babel-plugin-nonexistent-plugin'/,
141+
/Cannot (?:find|resolve) module 'babel-plugin-nonexistent-plugin'/,
142142
);
143143
});
144144

145145
it("exposes the resolvePreset method", function () {
146146
expect(() => babel.resolvePreset("nonexistent-preset")).toThrow(
147-
/Cannot resolve module 'babel-preset-nonexistent-preset'/,
147+
/Cannot (?:find|resolve) module 'babel-preset-nonexistent-preset'/,
148148
);
149149
});
150150

packages/babel-core/test/config-chain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { isMJS, loadOptionsAsync, skipUnsupportedESM } from "./helpers/esm.js";
1414
// TODO: In Babel 8, we can directly uses fs.promises which is supported by
1515
// node 8+
1616
const pfs =
17-
fs.promises ??
17+
fs.promises ||
1818
new Proxy(fs, {
1919
get(target, name) {
2020
if (name === "copyFile") {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

packages/babel-core/test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

packages/babel-core/test/resolution.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ describe("addon resolution", function () {
355355
// `require.resolve` is provided.
356356
// see https://github.com/babel/babel/pull/12439/files#r535996000
357357
process.versions.node.startsWith("8.")
358-
? /Cannot resolve module 'babel-preset-foo'/
359-
: /Cannot resolve module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/,
358+
? /Cannot (?:find|resolve) module 'babel-preset-foo'/
359+
: /Cannot (?:find|resolve) module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/s,
360360
);
361361
});
362362

@@ -371,8 +371,8 @@ describe("addon resolution", function () {
371371
});
372372
}).toThrow(
373373
process.versions.node.startsWith("8.")
374-
? /Cannot resolve module 'babel-plugin-foo'/
375-
: /Cannot resolve module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/,
374+
? /Cannot (?:find|resolve) module 'babel-plugin-foo'/
375+
: /Cannot (?:find|resolve) module 'babel-plugin-foo'.*\n- If you want to resolve "foo", use "module:foo"/s,
376376
);
377377
});
378378

@@ -387,8 +387,8 @@ describe("addon resolution", function () {
387387
});
388388
}).toThrow(
389389
process.versions.node.startsWith("8.")
390-
? /Cannot resolve module 'babel-preset-foo'/
391-
: /Cannot resolve module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/,
390+
? /Cannot (?:find|resolve) module 'babel-preset-foo'/
391+
: /Cannot (?:find|resolve) module 'babel-preset-foo'.*\n- Did you mean "@babel\/foo"\?/s,
392392
);
393393
});
394394

@@ -403,8 +403,8 @@ describe("addon resolution", function () {
403403
});
404404
}).toThrow(
405405
process.versions.node.startsWith("8.")
406-
? /Cannot resolve module 'babel-plugin-foo'/
407-
: /Cannot resolve module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/,
406+
? /Cannot (?:find|resolve) module 'babel-plugin-foo'/
407+
: /Cannot (?:find|resolve) module 'babel-plugin-foo'.*\n- Did you mean "@babel\/foo"\?/s,
408408
);
409409
});
410410

@@ -419,8 +419,8 @@ describe("addon resolution", function () {
419419
});
420420
}).toThrow(
421421
process.versions.node.startsWith("8.")
422-
? /Cannot resolve module 'babel-preset-testplugin'/
423-
: /Cannot resolve module 'babel-preset-testplugin'.*\n- Did you accidentally pass a plugin as a preset\?/,
422+
? /Cannot (?:find|resolve) module 'babel-preset-testplugin'/
423+
: /Cannot (?:find|resolve) module 'babel-preset-testplugin'.*\n- Did you accidentally pass a plugin as a preset\?/s,
424424
);
425425
});
426426

@@ -435,8 +435,8 @@ describe("addon resolution", function () {
435435
});
436436
}).toThrow(
437437
process.versions.node.startsWith("8.")
438-
? /Cannot resolve module 'babel-plugin-testpreset'/
439-
: /Cannot resolve module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a preset as a plugin\?/,
438+
? /Cannot (?:find|resolve) module 'babel-plugin-testpreset'/
439+
: /Cannot (?:find|resolve) module 'babel-plugin-testpreset'.*\n- Did you accidentally pass a preset as a plugin\?/s,
440440
);
441441
});
442442

@@ -449,7 +449,7 @@ describe("addon resolution", function () {
449449
babelrc: false,
450450
presets: ["foo"],
451451
});
452-
}).toThrow(/Cannot resolve module 'babel-preset-foo'/);
452+
}).toThrow(/Cannot (?:find|resolve) module 'babel-preset-foo'/);
453453
});
454454

455455
it("should throw about missing plugins", function () {
@@ -461,6 +461,6 @@ describe("addon resolution", function () {
461461
babelrc: false,
462462
plugins: ["foo"],
463463
});
464-
}).toThrow(/Cannot resolve module 'babel-plugin-foo'/);
464+
}).toThrow(/Cannot (?:find|resolve) module 'babel-plugin-foo'/);
465465
});
466466
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"browserslist": "chrome 4"
2+
"browserslist": "chrome 4",
3+
"type": "module"
34
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

packages/babel-helper-fixtures/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ function shouldIgnore(name, ignore?: Array<string>) {
6060
const base = path.basename(name, ext);
6161

6262
return (
63-
name[0] === "." || ext === ".md" || base === "LICENSE" || base === "options"
63+
name[0] === "." ||
64+
ext === ".md" ||
65+
base === "LICENSE" ||
66+
base === "options" ||
67+
name === "package.json"
6468
);
6569
}
6670

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

0 commit comments

Comments
 (0)