From 3f7ea3af64fa09019a9c6d646de5386236bc20ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 12 Feb 2021 17:31:24 +0100 Subject: [PATCH] Avoid using `require` in `test` files --- eslint/babel-eslint-parser/test/index.js | 7 ++- .../test/helpers/verifyAndAssertMessages.js | 5 ++- .../test/integration/eslint/config.js | 5 ++- .../test/integration/eslint/verify.js | 5 ++- packages/babel-cli/test/index.js | 25 ++++++----- packages/babel-core/test/api.js | 17 ++++---- packages/babel-core/test/config-loading.js | 3 ++ packages/babel-core/test/helpers/esm.js | 3 ++ packages/babel-core/test/parse.js | 3 ++ .../test/targets-supported.js | 4 +- packages/babel-node/test/index.js | 22 +++++----- .../test/copied-nodes.js | 4 +- .../test/esmodule-flag.js | 6 ++- .../test/helper.spec.js | 3 ++ .../test/get-option-specific-excludes.spec.js | 5 +-- packages/babel-preset-env/test/index.spec.js | 24 +++++------ .../test/normalize-options.spec.js | 43 +++++++++---------- packages/babel-standalone/test/babel.js | 4 +- .../test/preset-stage-1.test.js | 4 +- 19 files changed, 109 insertions(+), 83 deletions(-) diff --git a/eslint/babel-eslint-parser/test/index.js b/eslint/babel-eslint-parser/test/index.js index 350e17ea2e58..e8a2d73b8544 100644 --- a/eslint/babel-eslint-parser/test/index.js +++ b/eslint/babel-eslint-parser/test/index.js @@ -1,10 +1,13 @@ import path from "path"; import escope from "eslint-scope"; import unpad from "dedent"; +import { fileURLToPath } from "url"; +import { createRequire } from "module"; import { parseForESLint } from "../src"; const BABEL_OPTIONS = { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../babel-eslint-shared-fixtures/config/babel.config.js", ), }; @@ -73,6 +76,8 @@ describe("Babel and Espree", () => { } beforeAll(async () => { + const require = createRequire(import.meta.url); + // Use the version of Espree that is a dependency of // the version of ESLint we are testing against. const espreePath = require.resolve("espree", { diff --git a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js index 609cb8f50b79..b8cec5e450d9 100644 --- a/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js +++ b/eslint/babel-eslint-tests/test/helpers/verifyAndAssertMessages.js @@ -1,5 +1,7 @@ import eslint from "eslint"; import unpad from "dedent"; +import path from "path"; +import { fileURLToPath } from "url"; import * as parser from "../../../babel-eslint-parser"; export default function verifyAndAssertMessages( @@ -24,7 +26,8 @@ export default function verifyAndAssertMessages( sourceType, requireConfigFile: false, babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../babel-eslint-shared-fixtures/config/babel.config.js", ), }, diff --git a/eslint/babel-eslint-tests/test/integration/eslint/config.js b/eslint/babel-eslint-tests/test/integration/eslint/config.js index e82465b86f9b..989e9a8287d0 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/config.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/config.js @@ -1,4 +1,6 @@ import eslint from "eslint"; +import path from "path"; +import { fileURLToPath } from "url"; import * as parser from "@babel/eslint-parser"; describe("ESLint config", () => { @@ -10,7 +12,8 @@ describe("ESLint config", () => { parser: "@babel/eslint-parser", parserOptions: { babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../../babel-eslint-shared-fixtures/config/babel.config.js", ), }, diff --git a/eslint/babel-eslint-tests/test/integration/eslint/verify.js b/eslint/babel-eslint-tests/test/integration/eslint/verify.js index 6f63452871e9..a025c55fdc58 100644 --- a/eslint/babel-eslint-tests/test/integration/eslint/verify.js +++ b/eslint/babel-eslint-tests/test/integration/eslint/verify.js @@ -1,4 +1,6 @@ import verifyAndAssertMessages from "../../helpers/verifyAndAssertMessages"; +import path from "path"; +import { fileURLToPath } from "url"; describe("verify", () => { it("arrow function support (issue #1)", () => { @@ -1080,7 +1082,8 @@ describe("verify", () => { parserOptions: { sourceType, babelOptions: { - configFile: require.resolve( + configFile: path.resolve( + path.dirname(fileURLToPath(import.meta.url)), "../../../../babel-eslint-shared-fixtures/config/babel.config.decorators-legacy.js", ), }, diff --git a/packages/babel-cli/test/index.js b/packages/babel-cli/test/index.js index 7a3aadb7ffe4..0837afd80b0d 100644 --- a/packages/babel-cli/test/index.js +++ b/packages/babel-cli/test/index.js @@ -1,15 +1,18 @@ -const readdir = require("fs-readdir-recursive"); -const helper = require("@babel/helper-fixtures"); -const rimraf = require("rimraf"); -const { sync: makeDirSync } = require("make-dir"); -const child = require("child_process"); -const escapeRegExp = require("lodash/escapeRegExp"); -const merge = require("lodash/merge"); -const path = require("path"); -const fs = require("fs"); -const { chmod } = require("../lib/babel/util"); - +import readdir from "fs-readdir-recursive"; +import * as helper from "@babel/helper-fixtures"; +import rimraf from "rimraf"; +import { sync as makeDirSync } from "make-dir"; +import child from "child_process"; +import escapeRegExp from "lodash/escapeRegExp"; +import merge from "lodash/merge"; +import path from "path"; +import fs from "fs"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +import { chmod } from "../lib/babel/util"; + +const require = createRequire(import.meta.url); const dirname = path.dirname(fileURLToPath(import.meta.url)); const fixtureLoc = path.join(dirname, "fixtures"); diff --git a/packages/babel-core/test/api.js b/packages/babel-core/test/api.js index 98441c19d6c3..ed58229050ac 100644 --- a/packages/babel-core/test/api.js +++ b/packages/babel-core/test/api.js @@ -5,6 +5,10 @@ import Plugin from "../lib/config/plugin"; import generator from "@babel/generator"; import { fileURLToPath } from "url"; +import presetEnv from "../../babel-preset-env"; +import pluginSyntaxFlow from "../../babel-plugin-syntax-flow"; +import pluginFlowStripTypes from "../../babel-plugin-transform-flow-strip-types"; + const cwd = path.dirname(fileURLToPath(import.meta.url)); function assertIgnored(result) { @@ -307,17 +311,12 @@ describe("api", function () { }, // env preset - require(cwd + "/../../babel-preset-env"), + presetEnv, // Third preset for Flow. - function () { - return { - plugins: [ - require(cwd + "/../../babel-plugin-syntax-flow"), - require(cwd + "/../../babel-plugin-transform-flow-strip-types"), - ], - }; - }, + () => ({ + plugins: [pluginSyntaxFlow, pluginFlowStripTypes], + }), ], }); } diff --git a/packages/babel-core/test/config-loading.js b/packages/babel-core/test/config-loading.js index 0cee54e74fbe..da84e590d420 100644 --- a/packages/babel-core/test/config-loading.js +++ b/packages/babel-core/test/config-loading.js @@ -4,6 +4,9 @@ import loadConfigRunner, { } from "../lib/config"; import path from "path"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); const loadConfig = loadConfigRunner.sync; diff --git a/packages/babel-core/test/helpers/esm.js b/packages/babel-core/test/helpers/esm.js index 1992cd7d9be6..c485f1fa8422 100644 --- a/packages/babel-core/test/helpers/esm.js +++ b/packages/babel-core/test/helpers/esm.js @@ -2,8 +2,11 @@ import cp from "child_process"; import util from "util"; import path from "path"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; + import * as babel from "../../lib"; +const require = createRequire(import.meta.url); const dirname = path.dirname(fileURLToPath(import.meta.url)); // "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10 diff --git a/packages/babel-core/test/parse.js b/packages/babel-core/test/parse.js index 6e3bfbbcc158..b22ff86b4a90 100644 --- a/packages/babel-core/test/parse.js +++ b/packages/babel-core/test/parse.js @@ -2,6 +2,9 @@ import fs from "fs"; import path from "path"; import { parse } from "../lib"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); function fixture(...args) { return path.join( diff --git a/packages/babel-helper-compilation-targets/test/targets-supported.js b/packages/babel-helper-compilation-targets/test/targets-supported.js index fd018172ef29..aa4a2085de80 100644 --- a/packages/babel-helper-compilation-targets/test/targets-supported.js +++ b/packages/babel-helper-compilation-targets/test/targets-supported.js @@ -1,6 +1,4 @@ -"use strict"; - -const { targetsSupported } = require("../lib/filter-items"); +import { targetsSupported } from "../lib/filter-items"; describe("targetsSupported", () => { const MAX_VERSION = `${Number.MAX_SAFE_INTEGER}.0.0`; diff --git a/packages/babel-node/test/index.js b/packages/babel-node/test/index.js index d6e6298de4a9..f9fcdbb6d88d 100644 --- a/packages/babel-node/test/index.js +++ b/packages/babel-node/test/index.js @@ -1,14 +1,16 @@ -const includes = require("lodash/includes"); -const readdir = require("fs-readdir-recursive"); -const helper = require("@babel/helper-fixtures"); -const rimraf = require("rimraf"); -const { sync: makeDirSync } = require("make-dir"); -const child = require("child_process"); -const merge = require("lodash/merge"); -const path = require("path"); -const fs = require("fs"); - +import includes from "lodash/includes"; +import readdir from "fs-readdir-recursive"; +import * as helper from "@babel/helper-fixtures"; +import rimraf from "rimraf"; +import { sync as makeDirSync } from "make-dir"; +import child from "child_process"; +import merge from "lodash/merge"; +import path from "path"; +import fs from "fs"; import { fileURLToPath } from "url"; +import { createRequire } from "module"; + +const require = createRequire(import.meta.url); const dirname = path.dirname(fileURLToPath(import.meta.url)); const fixtureLoc = path.join(dirname, "fixtures"); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js index 00d5d3b0da11..4fb76081850a 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/copied-nodes.js @@ -2,13 +2,15 @@ import * as babel from "@babel/core"; import { fileURLToPath } from "url"; import path from "path"; +import transformCommonJS from ".."; + test("Doesn't use the same object for two different nodes in the AST", function () { const code = 'import Foo from "bar"; Foo; Foo;'; const ast = babel.transform(code, { cwd: path.dirname(fileURLToPath(import.meta.url)), ast: true, - plugins: [[require("../"), { loose: true }]], + plugins: [[transformCommonJS, { loose: true }]], }).ast; expect(ast.program.body[0].declarations[0].id.type).toBe("Identifier"); diff --git a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js index 3ecc9451c94f..cdb44988f7a4 100644 --- a/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js +++ b/packages/babel-plugin-transform-modules-commonjs/test/esmodule-flag.js @@ -3,6 +3,8 @@ import vm from "vm"; import { fileURLToPath } from "url"; import path from "path"; +import transformCommonJS from ".."; + test("Re-export doesn't overwrite __esModule flag", function () { let code = 'export * from "./dep";'; const depStub = { @@ -15,14 +17,14 @@ test("Re-export doesn't overwrite __esModule flag", function () { }, require: function (id) { if (id === "./dep") return depStub; - return require(id); + throw new Error("Unexpected dependency: " + id); }, }; context.exports = context.module.exports; code = babel.transform(code, { cwd: path.dirname(fileURLToPath(import.meta.url)), - plugins: [[require("../"), { loose: true }]], + plugins: [[transformCommonJS, { loose: true }]], ast: false, }).code; diff --git a/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js index 8a11f4133f10..7d84993c92e3 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js +++ b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js @@ -1,8 +1,11 @@ import * as babel from "@babel/core"; import fs from "fs"; +import { createRequire } from "module"; import transformTypeofSymbol from ".."; +const require = createRequire(import.meta.url); + const readFile = path => new Promise((resolve, reject) => fs.readFile(path, "utf8", (err, contents) => { diff --git a/packages/babel-preset-env/test/get-option-specific-excludes.spec.js b/packages/babel-preset-env/test/get-option-specific-excludes.spec.js index ea0cd6950c7d..f3cdae4485fa 100644 --- a/packages/babel-preset-env/test/get-option-specific-excludes.spec.js +++ b/packages/babel-preset-env/test/get-option-specific-excludes.spec.js @@ -1,7 +1,4 @@ -"use strict"; - -const getOptionSpecificExcludesFor = require("../lib/get-option-specific-excludes") - .default; +import getOptionSpecificExcludesFor from "../lib/get-option-specific-excludes"; describe("defaults", () => { describe("getOptionSpecificExcludesFor", () => { diff --git a/packages/babel-preset-env/test/index.spec.js b/packages/babel-preset-env/test/index.spec.js index 16f265c86c02..15878bc898ba 100644 --- a/packages/babel-preset-env/test/index.spec.js +++ b/packages/babel-preset-env/test/index.spec.js @@ -1,17 +1,17 @@ -"use strict"; +import compatData from "@babel/compat-data/plugins"; -const babelPresetEnv = require("../lib/index"); -const removeRegeneratorEntryPlugin = require("../lib/polyfills/regenerator") - .default; -const pluginCoreJS2 = require("babel-plugin-polyfill-corejs2").default; -const pluginCoreJS3 = require("babel-plugin-polyfill-corejs3").default; -const pluginRegenerator = require("babel-plugin-polyfill-regenerator").default; -const pluginLegacyBabelPolyfill = require("../lib/polyfills/babel-polyfill") - .default; -const transformations = require("../lib/module-transformations").default; +import * as babelPresetEnv from "../lib/index"; +import removeRegeneratorEntryPlugin from "../lib/polyfills/regenerator"; +import pluginLegacyBabelPolyfill from "../lib/polyfills/babel-polyfill"; +import transformations from "../lib/module-transformations"; +import availablePlugins from "../lib/available-plugins"; -const compatData = require("@babel/compat-data/plugins"); -const availablePlugins = require("../lib/available-plugins").default; +import _pluginCoreJS2 from "babel-plugin-polyfill-corejs2"; +import _pluginCoreJS3 from "babel-plugin-polyfill-corejs3"; +import _pluginRegenerator from "babel-plugin-polyfill-regenerator"; +const pluginCoreJS2 = _pluginCoreJS2.default; +const pluginCoreJS3 = _pluginCoreJS3.default; +const pluginRegenerator = _pluginRegenerator.default; describe("babel-preset-env", () => { describe("transformIncludesAndExcludes", () => { diff --git a/packages/babel-preset-env/test/normalize-options.spec.js b/packages/babel-preset-env/test/normalize-options.spec.js index 948c107cee70..aaac0dc718e5 100644 --- a/packages/babel-preset-env/test/normalize-options.spec.js +++ b/packages/babel-preset-env/test/normalize-options.spec.js @@ -1,17 +1,14 @@ -"use strict"; - -const normalizeOptions = require("../lib/normalize-options.js"); - -const { +import normalizeOptions, { checkDuplicateIncludeExcludes, validateModulesOption, validateUseBuiltInsOption, normalizePluginName, -} = normalizeOptions; +} from "../lib/normalize-options"; + describe("normalize-options", () => { describe("normalizeOptions", () => { it("should return normalized `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ include: [ "babel-plugin-transform-spread", "transform-classes", @@ -59,9 +56,9 @@ describe("normalize-options", () => { `( "should throw if with includes $include and excludes $exclude", ({ include, exclude }) => { - expect(() => - normalizeOptions.default({ include, exclude }), - ).toThrowError(/were found in both/); + expect(() => normalizeOptions({ include, exclude })).toThrowError( + /were found in both/, + ); }, ); @@ -69,7 +66,7 @@ describe("normalize-options", () => { [2, 2.1, 3, 3.5].forEach(corejs => { ["entry", "usage"].forEach(useBuiltIns => { expect(() => - normalizeOptions.default({ useBuiltIns, corejs }), + normalizeOptions({ useBuiltIns, corejs }), ).not.toThrowError(); }); }); @@ -78,28 +75,28 @@ describe("normalize-options", () => { it("should throw if corejs version is invalid", () => { [1, 1.2, 4, 4.5].forEach(corejs => { ["entry", "usage"].forEach(useBuiltIns => { - expect(() => - normalizeOptions.default({ useBuiltIns, corejs }), - ).toThrowError(/The version passed to `corejs` is invalid./); + expect(() => normalizeOptions({ useBuiltIns, corejs })).toThrowError( + /The version passed to `corejs` is invalid./, + ); }); }); }); it("throws when including module plugins", () => { expect(() => - normalizeOptions.default({ include: ["proposal-dynamic-import"] }), + normalizeOptions({ include: ["proposal-dynamic-import"] }), ).toThrow(); expect(() => - normalizeOptions.default({ include: ["transform-modules-amd"] }), + normalizeOptions({ include: ["transform-modules-amd"] }), ).toThrow(); }); it("allows exclusion of module plugins ", () => { expect(() => - normalizeOptions.default({ exclude: ["proposal-dynamic-import"] }), + normalizeOptions({ exclude: ["proposal-dynamic-import"] }), ).not.toThrow(); expect(() => - normalizeOptions.default({ exclude: ["transform-modules-commonjs"] }), + normalizeOptions({ exclude: ["transform-modules-commonjs"] }), ).not.toThrow(); }); }); @@ -116,7 +113,7 @@ describe("normalize-options", () => { describe("RegExp include/exclude", () => { it("should not allow invalid plugins in `include` and `exclude`", () => { const normalizeWithNonExistingPlugin = () => { - normalizeOptions.default({ + normalizeOptions({ include: ["non-existing-plugin"], }); }; @@ -124,7 +121,7 @@ describe("normalize-options", () => { }); it("should expand regular expressions in `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ include: ["^[a-z]*-spread", "babel-plugin-transform-classes"], }); expect(normalized.include).toEqual([ @@ -134,7 +131,7 @@ describe("normalize-options", () => { }); it("should expand regular expressions in `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ useBuiltIns: "entry", corejs: 3, exclude: ["es.math.log.*"], @@ -148,7 +145,7 @@ describe("normalize-options", () => { it("should not allow the same modules in `include` and `exclude`", () => { const normalizeWithNonExistingPlugin = () => { - normalizeOptions.default({ + normalizeOptions({ useBuiltIns: "entry", corejs: 3, include: ["es.math.log2"], @@ -159,7 +156,7 @@ describe("normalize-options", () => { }); it("should not do partial match if not explicitly defined `include` and `exclude`", () => { - const normalized = normalizeOptions.default({ + const normalized = normalizeOptions({ useBuiltIns: "entry", corejs: 3, include: ["es.reflect.set-prototype-of"], diff --git a/packages/babel-standalone/test/babel.js b/packages/babel-standalone/test/babel.js index 3858571e3963..3211e8ccf880 100644 --- a/packages/babel-standalone/test/babel.js +++ b/packages/babel-standalone/test/babel.js @@ -1,9 +1,9 @@ +import * as Babel from "../babel"; + // Basic smoke tests for @babel/standalone (process.env.TEST_TYPE === "cov" ? describe.skip : describe)( "@babel/standalone", () => { - const Babel = require("../babel"); - it("handles the es2015-no-commonjs preset", () => { const output = Babel.transform('const getMessage = () => "Hello World"', { presets: ["es2015-no-commonjs"], diff --git a/packages/babel-standalone/test/preset-stage-1.test.js b/packages/babel-standalone/test/preset-stage-1.test.js index fbcb49b6ff0b..6e1ab10ad97b 100644 --- a/packages/babel-standalone/test/preset-stage-1.test.js +++ b/packages/babel-standalone/test/preset-stage-1.test.js @@ -1,8 +1,8 @@ +import * as Babel from "../babel"; + (process.env.TEST_TYPE === "cov" ? describe.skip : describe)( "stage-1 preset", () => { - const Babel = require("../babel"); - it("should parser decimal literal", () => { const output = Babel.transform("0.3m", { presets: [["stage-1", { decoratorsBeforeExport: true }]],