Skip to content

Commit

Permalink
Run tests in a native Node.js ESM environment (#13966)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored Dec 3, 2021
1 parent 578ab22 commit 2d989a9
Show file tree
Hide file tree
Showing 159 changed files with 1,283 additions and 611 deletions.
43 changes: 25 additions & 18 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ const path = require("path");

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

const testFiles = [
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
];
const sourceFiles = exts => [
`packages/*/src/**/*.{${exts}}`,
`codemods/*/src/**/*.{${exts}}`,
`eslint/*/src/**/*.{${exts}}`,
];

module.exports = {
root: true,
plugins: [
"import",
"node",
"jest",
"prettier",
"@babel/development",
Expand Down Expand Up @@ -36,11 +48,7 @@ module.exports = {
},
},
{
files: [
"packages/*/src/**/*.{js,ts,cjs}",
"codemods/*/src/**/*.{js,ts,cjs}",
"eslint/*/src/**/*.{js,ts,cjs}",
],
files: sourceFiles("js,ts,cjs,mjs"),
rules: {
"@babel/development/no-undefined-identifier": "error",
"@babel/development/no-deprecated-clone": "error",
Expand All @@ -51,9 +59,7 @@ module.exports = {
},
{
files: [
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
...testFiles,
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
Expand All @@ -68,20 +74,21 @@ module.exports = {
"jest/no-test-callback": "off",
"jest/valid-describe": "off",
"import/extensions": ["error", "always"],
"import/no-extraneous-dependencies": "off",
"no-restricted-imports": ["error", { patterns: ["**/src/**"] }],
},
},
{
files: [
"packages/*/src/**/*.{js,ts}",
"codemods/*/src/**/*.{js,ts}",
"eslint/*/src/**/*.{js,ts}",
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"eslint/*/test/**/*.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.{ts,js}",
"test/**/*.js",
],
files: testFiles,
rules: {
"node/no-unsupported-features": [
"error",
{ version: "12.17.0", ignores: ["modules"] },
],
},
},
{
files: [...sourceFiles("js,ts,mjs"), ...testFiles, "test/**/*.js"],
excludedFiles: [
// @babel/register is the require() hook, so it will always be CJS-based
"packages/babel-register/**/*.{js,ts}",
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ package-lock.json
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js

/packages/babel-register/test/.babel
/packages/babel-register/test/.cache.babel
/packages/babel-register/test/.index.babel
/packages/babel-cli/test/tmp
/packages/babel-node/test/tmp
/packages/*/lib
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions eslint/babel-eslint-parser/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions eslint/babel-eslint-plugin/test/rules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions eslint/babel-eslint-tests/test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function verifyAndAssertMessages(
"../../../babel-eslint-shared-fixtures/config/babel.config.js",
),
},
...overrideConfig?.parserOptions,
...(overrideConfig && overrideConfig.parserOptions),
},
});

Expand Down
1 change: 1 addition & 0 deletions eslint/babel-eslint-tests/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const supportsESM = parseInt(process.versions.node) >= 12;
const isPublishBundle = process.env.IS_PUBLISH;

module.exports = {
runner: supportsESM ? "./test/jest-light-runner" : "jest-runner",

collectCoverageFrom: [
"packages/*/src/**/*.{js,mjs,ts}",
"codemods/*/src/**/*.{js,mjs,ts}",
Expand Down Expand Up @@ -47,7 +49,6 @@ module.exports = {
"/test/__data__/",
"<rootDir>/build/",
],
resolver: supportsESM ? "./test/jestExportsMapResolver.cjs" : null,
// We don't need module name mappers here as depedencies of workspace
// package should be declared explicitly in the package.json
// Yarn will generate correct file links so that Jest can resolve correctly
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jest": "^25.2.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.4.0",
"fancy-log": "^1.3.3",
"flow-bin": "^0.123.0",
Expand All @@ -61,8 +62,8 @@
"gulp-filter": "^7.0.0",
"gulp-plumber": "^1.2.1",
"husky": "^7.0.4",
"jest": "^27.2.0",
"jest-worker": "^27.2.0",
"jest": "^27.4.0",
"jest-worker": "^27.4.0",
"lint-staged": "^9.2.0",
"lodash": "^4.17.21",
"mergeiterator": "^1.2.5",
Expand All @@ -80,6 +81,7 @@
"eslint/*",
"packages/*",
"test/esm",
"test/jest-light-runner",
"test/runtime-integration/*",
"benchmark"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_ENV": "test",
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_ENV": "test",
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
1 change: 1 addition & 0 deletions packages/babel-cli/test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
22 changes: 6 additions & 16 deletions packages/babel-cli/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ const readDir = function (loc, filter) {

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

Object.keys(files).forEach(function (filename) {
const content = files[filename];
outputFileSync(filename, content);
outputFileSync(path.join(tmpLoc, filename), content);
});
};

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

expect(actual).toBe(expected ?? "");
expect(actual).toBe(expected || "");
}
} catch (e) {
e.message += "\n at " + filename;
Expand Down Expand Up @@ -152,7 +152,7 @@ const buildTest = function (binName, testName, opts) {
args = args.concat(opts.args);
const env = { ...process.env, ...opts.env };

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

let stderr = "";
let stdout = "";
Expand Down Expand Up @@ -190,28 +190,18 @@ const buildTest = function (binName, testName, opts) {
};

fs.readdirSync(fixtureLoc).forEach(function (binName) {
if (binName.startsWith(".")) return;
if (binName.startsWith(".") || binName === "package.json") return;

const suiteLoc = path.join(fixtureLoc, binName);
describe("bin/" + binName, function () {
let cwd;

beforeEach(() => {
cwd = process.cwd();

if (fs.existsSync(tmpLoc)) {
for (const child of fs.readdirSync(tmpLoc)) {
rimraf.sync(path.join(tmpLoc, child));
}
} else {
fs.mkdirSync(tmpLoc);
}

process.chdir(tmpLoc);
});

afterEach(() => {
process.chdir(cwd);
});

fs.readdirSync(suiteLoc).forEach(function (testName) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-cli/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions packages/babel-code-frame/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
4 changes: 2 additions & 2 deletions packages/babel-core/test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ describe("parser and generator options", function () {
describe("api", function () {
it("exposes the resolvePlugin method", function () {
expect(() => babel.resolvePlugin("nonexistent-plugin")).toThrow(
/Cannot resolve module 'babel-plugin-nonexistent-plugin'/,
/Cannot (?:find|resolve) module 'babel-plugin-nonexistent-plugin'/,
);
});

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

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-core/test/config-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isMJS, loadOptionsAsync, skipUnsupportedESM } from "./helpers/esm.js";
// TODO: In Babel 8, we can directly uses fs.promises which is supported by
// node 8+
const pfs =
fs.promises ??
fs.promises ||
new Proxy(fs, {
get(target, name) {
if (name === "copyFile") {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/test/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "commonjs" }
1 change: 1 addition & 0 deletions packages/babel-core/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
28 changes: 14 additions & 14 deletions packages/babel-core/test/resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ describe("addon resolution", function () {
// `require.resolve` is provided.
// see https://github.com/babel/babel/pull/12439/files#r535996000
process.versions.node.startsWith("8.")
? /Cannot resolve module 'babel-preset-foo'/
: /Cannot resolve module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/,
? /Cannot (?:find|resolve) module 'babel-preset-foo'/
: /Cannot (?:find|resolve) module 'babel-preset-foo'.*\n- If you want to resolve "foo", use "module:foo"/s,
);
});

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

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

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

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

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

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

it("should throw about missing plugins", function () {
Expand All @@ -461,6 +461,6 @@ describe("addon resolution", function () {
babelrc: false,
plugins: ["foo"],
});
}).toThrow(/Cannot resolve module 'babel-plugin-foo'/);
}).toThrow(/Cannot (?:find|resolve) module 'babel-plugin-foo'/);
});
});
1 change: 1 addition & 0 deletions packages/babel-generator/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
1 change: 1 addition & 0 deletions packages/babel-helper-annotate-as-pure/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"browserslist": "chrome 4"
"browserslist": "chrome 4",
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
6 changes: 5 additions & 1 deletion packages/babel-helper-fixtures/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function shouldIgnore(name, ignore?: Array<string>) {
const base = path.basename(name, ext);

return (
name[0] === "." || ext === ".md" || base === "LICENSE" || base === "options"
name[0] === "." ||
ext === ".md" ||
base === "LICENSE" ||
base === "options" ||
name === "package.json"
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/babel-helper-module-imports/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
Loading

0 comments on commit 2d989a9

Please sign in to comment.