Run tests in a native Node.js ESM environment (#13966)

This commit is contained in:
Nicolò Ribaudo 2021-12-03 15:32:58 +01:00 committed by GitHub
parent 578ab22fb7
commit 2d989a983d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
159 changed files with 1283 additions and 611 deletions

View File

@ -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",
@ -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",
@ -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",
],
@ -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}",

3
.gitignore vendored
View File

@ -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

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -31,7 +31,7 @@ export default function verifyAndAssertMessages(
"../../../babel-eslint-shared-fixtures/config/babel.config.js",
),
},
...overrideConfig?.parserOptions,
...(overrideConfig && overrideConfig.parserOptions),
},
});

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -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}",
@ -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

View File

@ -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",
@ -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",
@ -80,6 +81,7 @@
"eslint/*",
"packages/*",
"test/esm",
"test/jest-light-runner",
"test/runtime-integration/*",
"benchmark"
],

View File

@ -1,6 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_ENV": "test",
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]

View File

@ -1,6 +1,7 @@
{
"args": ["./src/index.js"],
"env": {
"BABEL_ENV": "test",
"BABEL_SHOW_CONFIG_FOR": "./src/index.js"
},
"os": ["darwin", "linux"]

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -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);
});
};
@ -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;
@ -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 = "";
@ -190,15 +190,11 @@ 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));
@ -206,12 +202,6 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) {
} else {
fs.mkdirSync(tmpLoc);
}
process.chdir(tmpLoc);
});
afterEach(() => {
process.chdir(cwd);
});
fs.readdirSync(suiteLoc).forEach(function (testName) {

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -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'/,
);
});

View File

@ -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") {

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -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,
);
});
@ -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,
);
});
@ -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,
);
});
@ -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,
);
});
@ -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,
);
});
@ -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,
);
});
@ -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 () {
@ -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'/);
});
});

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -1,3 +1,4 @@
{
"browserslist": "chrome 4"
"browserslist": "chrome 4",
"type": "module"
}

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -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"
);
}

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1,11 @@
import _cjs from "./lib/index.js";
const adapter = _cjs.default.bind();
// For backward compatibility with the CJS-only version, this makes
// import _x from "@babel/helper-plugin-test-runner"
// const x = _x.default
// still work.
adapter.default = _cjs.default;
export default adapter;

View File

@ -13,6 +13,20 @@
"access": "public"
},
"main": "./lib/index.js",
"exports": {
".": [
{
"import": "./esm.mjs",
"default": "./lib/index.js"
},
"./lib/index.js"
],
"./lib": "./lib/index.js",
"./lib/index": "./lib/index.js",
"./lib/index.js": "./lib/index.js",
"./package": "./package.json",
"./package.json": "./package.json"
},
"dependencies": {
"@babel/helper-transform-fixture-test-runner": "workspace:^"
},

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -34,12 +34,9 @@ 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.
outputFileSync(".babelrc", "{}");
Object.keys(files).forEach(function (filename) {
const content = files[filename];
outputFileSync(filename, content);
outputFileSync(path.join(tmpLoc, filename), content);
});
};
@ -76,14 +73,17 @@ const assertTest = function (stdout, stderr, ipcMessage, opts) {
}
if (opts.outFiles) {
const actualFiles = readDir(path.join(tmpLoc));
// For some reaons, on GH actions always appears a file called "null" ¯\_(ツ)_/¯
const actualFiles = readDir(tmpLoc, name => name !== "null");
Object.keys(actualFiles).forEach(function (filename) {
if (!Object.prototype.hasOwnProperty.call(opts.inFiles, filename)) {
const expected = opts.outFiles[filename];
const actual = actualFiles[filename];
expect(expected).not.toBeUndefined();
if (expected == null) {
throw new Error("Unexpected generated file: " + filename);
}
if (expected) {
expect(actual).toBe(expected);
@ -100,11 +100,9 @@ const assertTest = function (stdout, stderr, ipcMessage, opts) {
const buildTest = function (testName, opts) {
return function (callback) {
saveInFiles(opts.inFiles);
let args = [binLoc];
args.push("--config-file", "../config.json");
args = args.concat(opts.args);
const args = [binLoc].concat(opts.args);
const spawnOpts = {};
const spawnOpts = { cwd: tmpLoc, env: { BABEL_DISABLE_CACHE: true } };
if (opts.ipc) {
spawnOpts.stdio = ["pipe", "pipe", "pipe", "ipc"];
}
@ -136,9 +134,6 @@ const buildTest = function (testName, opts) {
assertTest(stdout, stderr, ipcMessage, opts);
} catch (e) {
err = e;
}
if (err) {
err.message =
args.map(arg => `"${arg}"`).join(" ") + ": " + err.message;
}
@ -154,24 +149,11 @@ const buildTest = function (testName, opts) {
};
describe("bin/babel-node", function () {
let cwd;
beforeEach(() => {
cwd = process.cwd();
if (fs.existsSync(tmpLoc)) {
for (const child of fs.readdirSync(tmpLoc)) {
rimraf.sync(path.join(tmpLoc, child));
rimraf.sync(tmpLoc);
}
} else {
fs.mkdirSync(tmpLoc);
}
process.chdir(tmpLoc);
});
afterEach(() => {
process.chdir(cwd);
});
fs.readdirSync(fixtureLoc).forEach(function (testName) {
@ -202,6 +184,12 @@ describe("bin/babel-node", function () {
if (fs.existsSync(babelrcLoc)) {
// copy .babelrc file to tmp directory
opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc);
} else {
// Place an empty .babelrc in each test so tests won't unexpectedly get to repo-level config.
opts.inFiles[".babelrc"] = "{}";
}
if (!opts.inFiles["package.json"]) {
opts.inFiles["package.json"] = `{ "type": "commonjs" }`;
}
// eslint-disable-next-line jest/valid-title

View File

@ -0,0 +1,6 @@
{
"presets": [
"@babel/preset-env",
["@babel/preset-react", { "runtime": "classic" }]
]
}

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -6,8 +6,7 @@ import { fileURLToPath } from "url";
runFixtureTestsWithoutExactASTMatch(
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"),
(input, options = {}) => {
options.plugins = options.plugins || [];
options.plugins.push("estree");
return parse(input, options);
const plugins = options.plugins || [];
return parse(input, { ...options, plugins: plugins.concat("estree") });
},
);

View File

@ -218,7 +218,7 @@ function runTest(test, parseFunction, compareErrorsOnly = false) {
return save(test, ast);
}
if (ast.errors?.length) {
if (ast.errors && ast.errors.length) {
throw new Error(
`Expected non-recoverable error message: ${
opts.throws

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "module" }

View File

@ -0,0 +1 @@
{ "type": "commonjs" }

View File

@ -0,0 +1 @@
{ "type": "module" }

Some files were not shown because too many files have changed in this diff Show More