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
159 changed files with 1283 additions and 611 deletions

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));
}
} else {
fs.mkdirSync(tmpLoc);
rimraf.sync(tmpLoc);
}
process.chdir(tmpLoc);
});
afterEach(() => {
process.chdir(cwd);
fs.mkdirSync(tmpLoc);
});
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" }

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

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