Skip to content

Commit

Permalink
Don't load plugins synchronously in tests (#14679)
Browse files Browse the repository at this point in the history
* Don't load plugins synchronously in tests

* Full paths
  • Loading branch information
nicolo-ribaudo committed Jun 22, 2022
1 parent 3636faa commit 87f26d5
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 99 deletions.
22 changes: 12 additions & 10 deletions packages/babel-core/test/api.js
Expand Up @@ -9,6 +9,7 @@ const Plugin = _Plugin.default || _Plugin;

import presetEnv from "@babel/preset-env";
import pluginSyntaxFlow from "@babel/plugin-syntax-flow";
import pluginSyntaxJSX from "@babel/plugin-syntax-jsx";
import pluginFlowStripTypes from "@babel/plugin-transform-flow-strip-types";

const cwd = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -115,7 +116,7 @@ describe("parser and generator options", function () {
function newTransformWithPlugins(string) {
return transformSync(string, {
ast: true,
plugins: [cwd + "/../../babel-plugin-syntax-flow"],
plugins: [pluginSyntaxFlow],
parserOpts: {
parser: recast.parse,
},
Expand Down Expand Up @@ -351,20 +352,21 @@ describe("api", function () {
it("options throw on falsy true", function () {
return expect(function () {
transformSync("", {
plugins: [cwd + "/../../babel-plugin-syntax-jsx", false],
plugins: [pluginSyntaxJSX, false],
});
}).toThrow(/.plugins\[1\] must be a string, object, function/);
});

it("options merge backwards", function () {
return transformAsync("", {
presets: [cwd + "/../../babel-preset-env"],
plugins: [cwd + "/../../babel-plugin-syntax-jsx"],
}).then(function (result) {
expect(result.options.plugins[0].manipulateOptions.toString()).toEqual(
expect.stringContaining("jsx"),
);
it("options merge backwards", async function () {
const result = await transformAsync("", {
cwd,
presets: ["@babel/preset-env"],
plugins: ["@babel/plugin-syntax-jsx"],
});

expect(result.options.plugins[0].manipulateOptions.toString()).toEqual(
expect.stringContaining("jsx"),
);
});

it("option wrapPluginVisitorMethod", function () {
Expand Down
@@ -1,22 +1,23 @@
// from code:
// export default function() {
// return {
// plugins: [require('../../../../../babel-plugin-syntax-decorators'),]
// plugins: ["__dirname + "/../../../../../babel-plugin-syntax-decorators/lib/index.js"]
// };
// }
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
value: true,
});

exports.default = function () {
return {
plugins: [
[
require('../../../../../babel-plugin-syntax-decorators'),
{ legacy: true }
__dirname +
"/../../../../../babel-plugin-syntax-decorators/lib/index.js",
{ legacy: true },
],
]
],
};
};
@@ -1,19 +1,18 @@
// from code:
// export default {
// plugins: [
// require('../../../../../babel-plugin-syntax-decorators'),
// ]
// plugins: [__dirname + "/../../../../../babel-plugin-syntax-decorators/lib/index.js"]
// };
'use strict';
"use strict";

exports.__esModule = true;
module.exports = function() {
module.exports = function () {
return {
plugins: [
[
require('../../../../../babel-plugin-syntax-decorators'),
{ legacy: true }
__dirname +
"/../../../../../babel-plugin-syntax-decorators/lib/index.js",
{ legacy: true },
],
]
],
};
};
@@ -1,8 +1,11 @@
// from code:
// export const plugins = [
// require('../../../../../babel-plugin-syntax-decorators'),
// __dirname + "/../../../../../babel-plugin-syntax-decorators/lib/index.js"
// ];
'use strict';
"use strict";

exports.__esModule = true;
var plugins = exports.plugins = [require('../../../../../babel-plugin-syntax-decorators')];
var plugins = [
__dirname + "/../../../../../babel-plugin-syntax-decorators/lib/index.js",
];
exports.plugins = plugins;
Expand Up @@ -2,9 +2,10 @@ module.exports = function () {
return {
plugins: [
[
require('../../../../../babel-plugin-syntax-decorators'),
{ legacy: true }
__dirname +
"/../../../../../babel-plugin-syntax-decorators/lib/index.js",
{ legacy: true },
],
]
],
};
};
@@ -1,10 +1,11 @@
module.exports = function() {
module.exports = function () {
return {
plugins: [
[
require('../../../../../babel-plugin-syntax-decorators'),
{ legacy: true }
__dirname +
"/../../../../../babel-plugin-syntax-decorators/lib/index.js",
{ legacy: true },
],
]
],
};
};
24 changes: 14 additions & 10 deletions packages/babel-core/test/option-manager.js
@@ -1,11 +1,15 @@
import { loadOptions as loadOptionsOrig } from "../lib/index.js";
import * as babel from "../lib/index.js";
import path from "path";
import { fileURLToPath } from "url";

const cwd = path.dirname(fileURLToPath(import.meta.url));

function loadOptions(opts) {
return loadOptionsOrig({ cwd, ...opts });
return babel.loadOptionsSync({ cwd, ...opts });
}

function loadOptionsAsync(opts) {
return babel.loadOptionsAsync({ cwd, ...opts });
}

describe("option-manager", () => {
Expand Down Expand Up @@ -61,10 +65,10 @@ describe("option-manager", () => {
}).toThrowErrorMatchingSnapshot();
});

it("should not throw when a preset string followed by valid preset object", () => {
it("should not throw when a preset string followed by valid preset object", async () => {
const { plugin } = makePlugin();
expect(
loadOptions({
await loadOptionsAsync({
presets: [
"@babel/env",
{ plugins: [[plugin, undefined, "my-plugin"]] },
Expand Down Expand Up @@ -234,8 +238,8 @@ describe("option-manager", () => {
"es5_object",
"es2015_default_function",
"es2015_default_object",
])("%p should work", name => {
const options = loadOptions({
])("%p should work", async name => {
const options = await loadOptionsAsync({
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
});

Expand All @@ -248,12 +252,12 @@ describe("option-manager", () => {
["es2015_named", /Must export a default export when using ES6 modules/],
["es2015_invalid", /Unsupported format: string/],
["es5_invalid", /Unsupported format: string/],
])("%p should throw %p", (name, msg) => {
expect(() =>
loadOptions({
])("%p should throw %p", async (name, msg) => {
await expect(
loadOptionsAsync({
presets: [path.join(cwd, "fixtures/option-manager/presets", name)],
}),
).toThrow(msg);
).rejects.toThrow(msg);
});
});
});
@@ -1,6 +1,7 @@
{
"minNodeVersion": "12.11.0",
"plugins": [
"./plugin.js",
"./plugin.mjs",
"bugfix-safari-id-destructuring-collision-in-function-expression"
]
}
@@ -1,6 +1,7 @@
const nameFunction = require("@babel/helper-function-name").default;
import _nameFunction from "@babel/helper-function-name";
const nameFunction = _nameFunction.default || _nameFunction;

module.exports = api => ({
export default api => ({
visitor: {
FunctionExpression(path) {
const replacement = nameFunction(path);
Expand Down
@@ -1,5 +1,6 @@
import * as babel from "@babel/core";
import proposalClassStaticBlock from "../lib/index.js";
import proposalClassProperties from "@babel/plugin-proposal-class-properties";

describe("plugin ordering", () => {
it("should work when @babel/plugin-proposal-class-static-block is after class features plugin", () => {
Expand All @@ -16,10 +17,7 @@ describe("plugin ordering", () => {
highlightCode: false,
configFile: false,
babelrc: false,
plugins: [
"@babel/plugin-proposal-class-properties",
proposalClassStaticBlock,
],
plugins: [proposalClassProperties, proposalClassStaticBlock],
}).code,
).toMatchInlineSnapshot(`
"function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Expand Down
@@ -1,5 +1,7 @@
import { transformSync } from "@babel/core";
import proposalDestructuringPrivate from "../lib/index.js";
import proposalClassProperties from "@babel/plugin-proposal-class-properties";
import proposalClassStaticBlock from "@babel/plugin-proposal-class-static-block";

describe("plugin ordering", () => {
it("should work when @babel/plugin-proposal-destructuring-private is after class features plugin", () => {
Expand All @@ -18,8 +20,8 @@ describe("plugin ordering", () => {
configFile: false,
babelrc: false,
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-class-static-block",
proposalClassProperties,
proposalClassStaticBlock,
proposalDestructuringPrivate,
],
}).code,
Expand Down
Expand Up @@ -4,5 +4,6 @@
"transform-arrow-functions",
"transform-destructuring",
"transform-block-scoping"
]
],
"parserOpts": { "allowReturnOutsideFunction": true }
}
Expand Up @@ -3,36 +3,35 @@ if (x) {
const bar = 1;
}`;

var expected = `var foo = 1;
if (x) {
var bar = 1;
}`;

var innerScope = true;
var res = transform(code, {
return transformAsync(code, {
configFile: false,
plugins: opts.plugins.concat([
function (b) {
var t = b.types;
function ({ types: t }) {
return {
visitor: {
Scope: {
exit: function(path) {
exit: function (path) {
if (innerScope) {
expect(Object.keys(path.scope.bindings)).toHaveLength(0);
innerScope = false;
return;
}

expect(Object.keys(path.scope.bindings)).toHaveLength(2);
}
}
}
}
}
},
},
},
};
},
]),
}).then(res => {
expect(res.code).toBe(expected);
expect(innerScope).toBe(false);
});

var expected = `var foo = 1;
if (x) {
var bar = 1;
}`;

expect(res.code).toBe(expected);
expect(innerScope).toBe(false);
Expand Up @@ -12,7 +12,7 @@ let programPath;
let forOfPath;
let functionPath;

transform(code, {
return transformAsync(code, {
configFile: false,
plugins: [
__dirname + "/../../../../lib",
Expand All @@ -26,13 +26,13 @@ transform(code, {
}
}
]
});

expect(Object.keys(programPath.scope.bindings)).toEqual(["foo", "bar"]);
}).then(() => {
expect(Object.keys(programPath.scope.bindings)).toEqual(["foo", "bar"]);

// for declarations should be transformed to for bindings
expect(forOfPath.scope.bindings).toEqual({});
// The body should be wrapped into closure
expect(forOfPath.get("body").scope.bindings).toEqual({});
// for declarations should be transformed to for bindings
expect(forOfPath.scope.bindings).toEqual({});
// The body should be wrapped into closure
expect(forOfPath.get("body").scope.bindings).toEqual({});

expect(Object.keys(functionPath.scope.bindings)).toEqual(["foo", "bar", "qux", "quux"]);
expect(Object.keys(functionPath.scope.bindings)).toEqual(["foo", "bar", "qux", "quux"]);
});
Expand Up @@ -2,5 +2,6 @@
"plugins": [
"transform-block-scoping",
["proposal-object-rest-spread", { "loose": true }]
]
],
"parserOpts": { "allowReturnOutsideFunction": true }
}
Expand Up @@ -21,13 +21,13 @@ var tests = [
'export {default as foo} from "foo";',
];

tests.forEach(function (code) {
var res = transform(code, {
return Promise.all(tests.map(code =>
transformAsync(code, {
configFile: false,
sourceMap: true,
plugins: opts.plugins
});

// Should create mapping
expect(res.map.mappings).not.toBe('');
});
}).then(res => {
// Should create mapping
expect(res.map.mappings).not.toBe('');
})
));
@@ -1,3 +1,4 @@
{
"plugins": ["transform-modules-commonjs"]
"plugins": ["transform-modules-commonjs"],
"parserOpts": { "allowReturnOutsideFunction": true }
}

0 comments on commit 87f26d5

Please sign in to comment.