From 87f26d5a172dae8f6d81a86c9d831e60c99220fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 22 Jun 2022 21:33:22 +0200 Subject: [PATCH] Don't load plugins synchronously in tests (#14679) * Don't load plugins synchronously in tests * Full paths --- packages/babel-core/test/api.js | 22 ++++++------ .../presets/es2015_default_function.js | 13 +++---- .../presets/es2015_default_object.js | 15 ++++---- .../option-manager/presets/es2015_named.js | 9 +++-- .../option-manager/presets/es5_function.js | 7 ++-- .../option-manager/presets/es5_object.js | 9 ++--- packages/babel-core/test/option-manager.js | 24 +++++++------ .../helper-function-name/options.json | 3 +- .../{plugin.js => plugin.mjs} | 5 +-- .../test/plugin-ordering.test.js | 6 ++-- .../test/plugin-ordering.js | 6 ++-- .../test/fixtures/exec/options.json | 3 +- .../test/fixtures/exec/scope-bindings.js | 35 +++++++++---------- .../test/fixtures/general/issue-10339/exec.js | 18 +++++----- .../fixtures/general/issue-10339/options.json | 3 +- .../test/fixtures/source-map/exec.js | 14 ++++---- .../test/fixtures/source-map/options.json | 3 +- .../react-source/basic-sample/exec.js | 8 +++-- .../react-source/basic-sample/options.json | 3 +- .../fixtures/react-source/with-source/exec.js | 8 +++-- .../react-source/with-source/options.json | 3 +- .../scripts/build-dist.js | 4 ++- 22 files changed, 122 insertions(+), 99 deletions(-) rename packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/{plugin.js => plugin.mjs} (52%) diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index eedf91b88031..d810caf65127 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -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)); @@ -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, }, @@ -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 () { diff --git a/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_function.js b/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_function.js index a58f2d700df9..d0755324db58 100644 --- a/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_function.js +++ b/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_function.js @@ -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 }, ], - ] + ], }; }; diff --git a/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_object.js b/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_object.js index cd1208bf358b..cb27145ce49b 100644 --- a/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_object.js +++ b/packages/babel-core/test/fixtures/option-manager/presets/es2015_default_object.js @@ -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 }, ], - ] + ], }; }; diff --git a/packages/babel-core/test/fixtures/option-manager/presets/es2015_named.js b/packages/babel-core/test/fixtures/option-manager/presets/es2015_named.js index dd3b12968d64..f12116e26c45 100644 --- a/packages/babel-core/test/fixtures/option-manager/presets/es2015_named.js +++ b/packages/babel-core/test/fixtures/option-manager/presets/es2015_named.js @@ -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; diff --git a/packages/babel-core/test/fixtures/option-manager/presets/es5_function.js b/packages/babel-core/test/fixtures/option-manager/presets/es5_function.js index 539c58e1646a..dda0ce55675c 100644 --- a/packages/babel-core/test/fixtures/option-manager/presets/es5_function.js +++ b/packages/babel-core/test/fixtures/option-manager/presets/es5_function.js @@ -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 }, ], - ] + ], }; }; diff --git a/packages/babel-core/test/fixtures/option-manager/presets/es5_object.js b/packages/babel-core/test/fixtures/option-manager/presets/es5_object.js index 30c4d017c4eb..dda0ce55675c 100644 --- a/packages/babel-core/test/fixtures/option-manager/presets/es5_object.js +++ b/packages/babel-core/test/fixtures/option-manager/presets/es5_object.js @@ -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 }, ], - ] + ], }; }; diff --git a/packages/babel-core/test/option-manager.js b/packages/babel-core/test/option-manager.js index 50b1d8bdea84..e072534984dd 100644 --- a/packages/babel-core/test/option-manager.js +++ b/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", () => { @@ -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"]] }, @@ -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)], }); @@ -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); }); }); }); diff --git a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json index da9603fd6474..4597692aea15 100644 --- a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json +++ b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/options.json @@ -1,6 +1,7 @@ { + "minNodeVersion": "12.11.0", "plugins": [ - "./plugin.js", + "./plugin.mjs", "bugfix-safari-id-destructuring-collision-in-function-expression" ] } diff --git a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.js b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.mjs similarity index 52% rename from packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.js rename to packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.mjs index 7514cb9cbb6f..02a7cb7117c7 100644 --- a/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.js +++ b/packages/babel-plugin-bugfix-safari-id-destructuring-collision-in-function-expression/test/fixtures/integration/helper-function-name/plugin.mjs @@ -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); diff --git a/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js b/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js index 72505b9d639d..be48de60ada1 100644 --- a/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js +++ b/packages/babel-plugin-proposal-class-static-block/test/plugin-ordering.test.js @@ -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", () => { @@ -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; } diff --git a/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js index 9879497c8ee9..4390a7069bfd 100644 --- a/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js +++ b/packages/babel-plugin-proposal-destructuring-private/test/plugin-ordering.js @@ -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", () => { @@ -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, diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/options.json b/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/options.json index cca3c7921585..1154882222ea 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/options.json +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/options.json @@ -4,5 +4,6 @@ "transform-arrow-functions", "transform-destructuring", "transform-block-scoping" - ] + ], + "parserOpts": { "allowReturnOutsideFunction": true } } diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/scope-bindings.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/scope-bindings.js index 3014558a0261..cf7b2b3d327c 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/scope-bindings.js +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/exec/scope-bindings.js @@ -3,16 +3,21 @@ 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; @@ -20,19 +25,13 @@ var res = transform(code, { } 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); diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/exec.js b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/exec.js index f8b3f6b3699f..1815a28ade50 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/exec.js +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/exec.js @@ -12,7 +12,7 @@ let programPath; let forOfPath; let functionPath; -transform(code, { +return transformAsync(code, { configFile: false, plugins: [ __dirname + "/../../../../lib", @@ -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"]); +}); diff --git a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/options.json b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/options.json index c71915a51cc7..5fb69a1b6590 100644 --- a/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/options.json +++ b/packages/babel-plugin-transform-block-scoping/test/fixtures/general/issue-10339/options.json @@ -2,5 +2,6 @@ "plugins": [ "transform-block-scoping", ["proposal-object-rest-spread", { "loose": true }] - ] + ], + "parserOpts": { "allowReturnOutsideFunction": true } } diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/exec.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/exec.js index c3b5044c0b2b..b7999270c1fe 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/exec.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/exec.js @@ -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(''); + }) +)); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/options.json b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/options.json index 81e5ce12436d..138bbd250d9a 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/options.json +++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/source-map/options.json @@ -1,3 +1,4 @@ { - "plugins": ["transform-modules-commonjs"] + "plugins": ["transform-modules-commonjs"], + "parserOpts": { "allowReturnOutsideFunction": true } } diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js index 21bf1293f132..bae12f93ce4e 100644 --- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js +++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/exec.js @@ -1,7 +1,7 @@ -var actual = transform( +var actualP = transformAsync( 'var x = ', Object.assign({}, opts, { filename: '/fake/path/mock.js' }) -).code; +) var expected = ` var _jsxFileName = "/fake/path/mock.js"; @@ -12,4 +12,6 @@ var x = ; `.trim(); -expect(actual).toBe(expected); +return actualP.then(actual => { + expect(actual.code).toBe(expected); +}); diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/options.json b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/options.json index d6180568ae8d..a2614c2f1f89 100644 --- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/options.json +++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/basic-sample/options.json @@ -1,3 +1,4 @@ { - "os": ["linux", "darwin"] + "os": ["linux", "darwin"], + "parserOpts": { "allowReturnOutsideFunction": true } } diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/exec.js b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/exec.js index d6849ef01674..9c63d9b34139 100644 --- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/exec.js +++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/exec.js @@ -1,8 +1,10 @@ -var actual = transform( +var actualP = transformAsync( 'var x = ;', Object.assign({}, opts, { filename: '/fake/path/mock.js' }) -).code; +) var expected = 'var x = ;'; -expect(actual).toBe(expected); +return actualP.then(actual => { + expect(actual.code).toBe(expected); +}); diff --git a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/options.json b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/options.json index d6180568ae8d..a2614c2f1f89 100644 --- a/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/options.json +++ b/packages/babel-plugin-transform-react-jsx-source/test/fixtures/react-source/with-source/options.json @@ -1,3 +1,4 @@ { - "os": ["linux", "darwin"] + "os": ["linux", "darwin"], + "parserOpts": { "allowReturnOutsideFunction": true } } diff --git a/packages/babel-plugin-transform-runtime/scripts/build-dist.js b/packages/babel-plugin-transform-runtime/scripts/build-dist.js index 29c77ba3ae42..18b4d728689f 100644 --- a/packages/babel-plugin-transform-runtime/scripts/build-dist.js +++ b/packages/babel-plugin-transform-runtime/scripts/build-dist.js @@ -11,6 +11,8 @@ import transformRuntime from "../lib/index.js"; import corejs2Definitions from "./runtime-corejs2-definitions.js"; import corejs3Definitions from "./runtime-corejs3-definitions.js"; +import presetEnv from "@babel/preset-env"; + const require = createRequire(import.meta.url); const runtimeVersion = require("@babel/runtime/package.json").version; @@ -237,7 +239,7 @@ function buildHelper( return transformFromAstSync(tree, null, { filename: helperFilename, - presets: [["@babel/preset-env", { modules: false }]], + presets: [[presetEnv, { modules: false }]], plugins: [ [transformRuntime, { corejs, version: runtimeVersion }], buildRuntimeRewritePlugin(runtimeName, helperName),