Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Babel asynchronously in fixtures #14659

Merged
merged 4 commits into from Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 37 additions & 17 deletions packages/babel-helper-transform-fixture-test-runner/src/index.ts
Expand Up @@ -20,6 +20,24 @@ const require = createRequire(import.meta.url);

import checkDuplicateNodes from "@babel/helper-check-duplicate-nodes";

if (!process.env.BABEL_8_BREAKING) {
// Introduced in Node.js 10
if (!assert.rejects) {
assert.rejects = async function (promise, validateError) {
try {
await promise;
return Promise.reject(new Error("Promise not rejected"));
} catch (error) {
if (!validateError(error)) {
return Promise.reject(
new Error("Promise rejected with invalid error"),
);
}
}
};
}
}

const EXTERNAL_HELPERS_VERSION = "7.100.0";

const cachedScripts = new QuickLRU<
Expand All @@ -40,12 +58,20 @@ function transformWithoutConfigFile(code, opts) {
...opts,
});
}
function transformAsyncWithoutConfigFile(code, opts) {
return babel.transformAsync(code, {
configFile: false,
babelrc: false,
...opts,
});
}

function createContext() {
const context = vm.createContext({
...helpers,
process: process,
transform: transformWithoutConfigFile,
transformAsync: transformAsyncWithoutConfigFile,
setTimeout: setTimeout,
setImmediate: setImmediate,
expect,
Expand Down Expand Up @@ -194,10 +220,10 @@ export function runCodeInTestContext(
}
}

function maybeMockConsole(validateLogs, run) {
async function maybeMockConsole(validateLogs, run) {
const actualLogs = { stdout: "", stderr: "" };

if (!validateLogs) return { result: run(), actualLogs };
if (!validateLogs) return { result: await run(), actualLogs };

const spy1 = jest.spyOn(console, "log").mockImplementation(msg => {
actualLogs.stdout += `${msg}\n`;
Expand All @@ -207,14 +233,14 @@ function maybeMockConsole(validateLogs, run) {
});

try {
return { result: run(), actualLogs };
return { result: await run(), actualLogs };
} finally {
spy1.mockRestore();
spy2.mockRestore();
}
}

function run(task) {
async function run(task) {
const {
actual,
expect: expected,
Expand Down Expand Up @@ -256,8 +282,8 @@ function run(task) {

// Ignore Babel logs of exec.js files.
// They will be validated in input/output files.
({ result } = maybeMockConsole(validateLogs, () =>
babel.transformSync(execCode, execOpts),
({ result } = await maybeMockConsole(validateLogs, () =>
babel.transformAsync(execCode, execOpts),
));

checkDuplicateNodes(result.ast);
Expand All @@ -278,8 +304,8 @@ function run(task) {
if (!execCode || inputCode) {
let actualLogs;

({ result, actualLogs } = maybeMockConsole(validateLogs, () =>
babel.transformSync(inputCode, getOpts(actual)),
({ result, actualLogs } = await maybeMockConsole(validateLogs, () =>
babel.transformAsync(inputCode, getOpts(actual)),
));

const outputCode = normalizeOutput(result.code);
Expand Down Expand Up @@ -470,11 +496,7 @@ export default function (
testFn(
task.title,

function () {
function runTask() {
run(task);
}

async function () {
if ("sourceMap" in task.options === false) {
task.options.sourceMap = !!(
task.sourceMappings || task.sourceMap
Expand All @@ -499,7 +521,7 @@ export default function (
// the options object with useless options
delete task.options.throws;

assert.throws(runTask, function (err: Error) {
await assert.rejects(run(task), function (err: Error) {
assert.ok(
throwMsg === true || err.message.includes(throwMsg),
`
Expand All @@ -509,13 +531,11 @@ Actual Error: ${err.message}`,
return true;
});
} else {
const result = await run(task);
if (task.exec.code) {
const result = run(task);
if (result && typeof result.then === "function") {
return result;
}
} else {
runTask();
}
}
},
Expand Down
@@ -1,3 +1,3 @@
{
"plugins": ["../../../res/checkScopeInfo.js"]
"plugins": ["../../helpers/checkScopeInfo.js"]
}
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"plugins": ["transform-destructuring", "../../helpers/checkScopeInfo.js"],
"assumptions": {
"arrayLikeIsIterable": true
}
Expand Down
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"plugins": ["transform-destructuring", "../../helpers/checkScopeInfo.js"],
"assumptions": {
"iterableIsArray": true
}
Expand Down
@@ -1,5 +1,5 @@
{
"plugins": ["transform-destructuring", "../../../res/checkScopeInfo.js"],
"plugins": ["transform-destructuring", "../../helpers/checkScopeInfo.js"],
"assumptions": {
"objectRestNoSymbols": true
}
Expand Down
Expand Up @@ -6,6 +6,6 @@
"transform-block-scoping",
"proposal-object-rest-spread",
"transform-regenerator",
"../../../res/checkScopeInfo.js"
"../../helpers/checkScopeInfo.js"
]
}
@@ -1,5 +1,4 @@
// checkScopeInfo.js
module.exports = () => {
export default () => {
return {
visitor: {
Program: {
Expand Down