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

extract more test helpers to repo-utils #15902

Merged
merged 8 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,10 @@ function pluginImportMetaUrl({ types: t, template }) {
// We must be sure to run this before the istanbul plugins, because its
// instrumentation breaks our detection.
programPath.traverse({
// fileURLToPath(import.meta.url)
CallExpression(path) {
const { node } = path;

// fileURLToPath(import.meta.url)
if (
(function () {
if (
Expand Down Expand Up @@ -723,6 +723,7 @@ function pluginImportMetaUrl({ types: t, template }) {
return;
}

// const { __dirname: cwd } = commonJS(import.meta.url)
if (
!t.isIdentifier(node.callee, { name: "commonJS" }) ||
node.arguments.length !== 1
Expand Down
12 changes: 5 additions & 7 deletions eslint/babel-eslint-parser/test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from "path";
import escope from "eslint-scope";
import unpad from "dedent";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import { parseForESLint as parseForESLintOriginal } from "../lib/index.cjs";
import { ESLint } from "eslint";
import { itDummy, commonJS } from "$repo-utils";

function parseForESLint(code, options) {
return parseForESLintOriginal(code, {
Expand All @@ -19,11 +18,12 @@ function parseForESLint(code, options) {

const ESLINT_VERSION = ESLint.version;
const isESLint7 = ESLINT_VERSION.startsWith("7.");
const dirname = path.dirname(fileURLToPath(import.meta.url));
const { __dirname: dirname, require } = commonJS(import.meta.url);

// @babel/eslint-parser 8 will drop ESLint 7 support
const itESLint7 = isESLint7 && !process.env.BABEL_8_BREAKING ? it : it.skip;
const itESLint8 = isESLint7 ? it.skip : it;

const itESLint7 = isESLint7 && !process.env.BABEL_8_BREAKING ? it : itDummy;
const itESLint8 = isESLint7 ? itDummy : it;

const BABEL_OPTIONS = {
configFile: path.resolve(
Expand Down Expand Up @@ -122,8 +122,6 @@ describe("Babel and Espree", () => {
}

beforeAll(() => {
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", {
Expand Down
6 changes: 2 additions & 4 deletions eslint/babel-eslint-tests/test/integration/config-files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ESLint } from "eslint";
import path from "path";
import { fileURLToPath } from "url";
import { USE_ESM } from "$repo-utils";
import { itESM, itGteNoESM } from "$repo-utils";

describe("Babel config files", () => {
const itESM = USE_ESM ? it : it.skip;
const nodeGte12NoESM =
USE_ESM || parseInt(process.versions.node) < 12 ? it.skip : it;
const nodeGte12NoESM = itGteNoESM("12.0.0");

itESM("works with babel.config.mjs", async () => {
const engine = new ESLint({ ignore: false });
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-core/test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as babel from "../lib/index.js";
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
import path from "path";
import generator from "@babel/generator";
import { fileURLToPath } from "url";

import _Plugin from "../lib/config/plugin.js";
const Plugin = _Plugin.default || _Plugin;
Expand All @@ -11,10 +10,10 @@ 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";
import { itBabel8, commonJS } from "$repo-utils";

const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;

const cwd = path.dirname(fileURLToPath(import.meta.url));
const { __dirname } = commonJS(import.meta.url);
const cwd = __dirname;
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would supporting const cwd = commonJS(import.meta.url).__dirname make the plugin significantly more complex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current babelImportMetaUrl plugin removes const { __dirname } = commonJS(import.meta.url) and let __dirname references the global variable, so it breaks when we rename the variable in destructuring. At first I replaced commonJS(import.meta.url) by { __dirname, __filename, require } but the test throws with _dirname (one underscore) not defined, I guess it is a glitch in the replacer as it renames __dirname to _dirname (incorrectly). I will investigate this issue later.

Since it is very likely that we will get rid of the custom babelImportMetaUrl in Babel 8, I workaround this issue and just keep this PR mergeable.


function assertIgnored(result) {
expect(result).toBeNull();
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-core/test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
spawnTransformAsync,
spawnTransformSync,
supportsESM,
itESM,
} from "./helpers/esm.js";

import { itGte } from "$repo-utils";
import { itGte, itESM } from "$repo-utils";

// "minNodeVersion": "8.0.0" <-- For Ctrl+F when dropping node 6
const nodeGte8 = itGte("8.0.0");
Expand Down
12 changes: 4 additions & 8 deletions packages/babel-core/test/config-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ import {
createConfigItemSync,
} from "../lib/index.js";
import path from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import { itNoWin32 } from "$repo-utils";
import { itNoWin32, itBabel8, commonJS } from "$repo-utils";

const require = createRequire(import.meta.url);

const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
const { require, __dirname } = commonJS(import.meta.url);

describe("@babel/core config loading", () => {
const FILEPATH = path.join(
path.dirname(fileURLToPath(import.meta.url)),
__dirname,
"fixtures",
"config-loading",
"folder",
Expand Down Expand Up @@ -152,7 +148,7 @@ describe("@babel/core config loading", () => {

it("should always set 'rootMode' to 'root'", async () => {
const cwd = path.join(
path.dirname(fileURLToPath(import.meta.url)),
__dirname,
"fixtures",
"config-loading",
"root",
Expand Down
8 changes: 6 additions & 2 deletions packages/babel-core/test/esm-cjs-integration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execFile } from "child_process";
import { createRequire } from "module";
import { outputType } from "./helpers/esm.js";
import { describeESM } from "$repo-utils";

const require = createRequire(import.meta.url);

Expand All @@ -18,7 +18,11 @@ async function run(name) {
});
}

(outputType === "module" ? describe : describe.skip)("usage from cjs", () => {
describe("dummy", () => {
it("dummy", () => {});
});

describeESM("usage from cjs", () => {
it("lazy plugin required", async () => {
expect(await run("lazy-plugin-required.cjs")).toMatchInlineSnapshot(`
Object {
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-core/test/helpers/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export const outputType = USE_ESM ? "module" : "script";

export const isMJS = file => path.extname(file) === ".mjs";

export const itESM = supportsESM ? it : it.skip;

export function skipUnsupportedESM(name) {
if (!supportsESM) {
console.warn(
Expand Down
5 changes: 1 addition & 4 deletions packages/babel-core/test/option-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as babel from "../lib/index.js";
import path from "path";
import { fileURLToPath } from "url";
import { USE_ESM } from "$repo-utils";
import { itBabel7, itBabel7NoESM } from "$repo-utils";

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

Expand All @@ -13,9 +13,6 @@ function loadOptionsAsync(opts) {
return babel.loadOptionsAsync({ cwd, ...opts });
}

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
const itBabel7NoESM = process.env.BABEL_8_BREAKING || USE_ESM ? it.skip : it;

describe("option-manager", () => {
itBabel7NoESM("throws for babel 5 plugin", () => {
return expect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { fileURLToPath } from "url";

import _getTargets from "../lib/index.js";
const getTargets = _getTargets.default || _getTargets;

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
import { itBabel8, itBabel7 } from "$repo-utils";

describe("getTargets", () => {
it("parses", () => {
Expand Down
15 changes: 7 additions & 8 deletions packages/babel-helper-simple-access/test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as babel from "@babel/core";
import simplifyAccess from "../lib/index.js";
import { itBabel7 } from "$repo-utils";

const plugin = (_api, options) => {
// TODO(Babel 8): Remove includeUpdateExpression
Expand All @@ -18,8 +19,6 @@ const plugin = (_api, options) => {
};
};

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;

itBabel7("simplifyAccess with default config", function () {
const code = `
let a = foo++;
Expand All @@ -30,7 +29,7 @@ itBabel7("simplifyAccess with default config", function () {
a++;
foo = a++;
foo = ++a;
let b = bar--;
b = --bar;
bar--;
Expand All @@ -39,11 +38,11 @@ itBabel7("simplifyAccess with default config", function () {
b--;
bar = b--;
bar = --b;
let c = baz += 1;
baz += 1;
c += 1;
function f() {
let foo = 1;
let a = foo++;
Expand Down Expand Up @@ -108,7 +107,7 @@ it("simplifyAccess with includeUpdateExpression=false", function () {
a++;
foo = a++;
foo = ++a;
let b = bar--;
b = --bar;
bar--;
Expand All @@ -117,11 +116,11 @@ it("simplifyAccess with includeUpdateExpression=false", function () {
b--;
bar = b--;
bar = --b;
let c = baz += 1;
baz += 1;
c += 1;
function f() {
let foo = 1;
let a = foo++;
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-parser/test/plugin-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { parse } from "../lib/index.js";

const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
import { itBabel8 } from "$repo-utils";

function getParser(code, plugins) {
return () => parse(code, { plugins, sourceType: "module" });
Expand Down
8 changes: 3 additions & 5 deletions packages/babel-plugin-syntax-decorators/test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseSync } from "@babel/core";
import syntaxDecorators from "../lib/index.js";
import { itBabel8, describeBabel7 } from "$repo-utils";

function makeParser(code, options) {
return () =>
Expand All @@ -10,10 +11,7 @@ function makeParser(code, options) {
});
}

const itBabel8 = process.env.BABEL_8_BREAKING ? test : test.skip;
const babel7describe = process.env.BABEL_8_BREAKING ? describe.skip : describe;

babel7describe("'legacy' option", function () {
describeBabel7("'legacy' option", function () {
test("legacy must be boolean", function () {
expect(makeParser("", { legacy: "legacy" })).toThrow();
});
Expand All @@ -31,7 +29,7 @@ babel7describe("'legacy' option", function () {
});
});

babel7describe("'decoratorsBeforeExport' option", function () {
describeBabel7("'decoratorsBeforeExport' option", function () {
test("decoratorsBeforeExport must be boolean", function () {
expect(
makeParser("", { version: "2021-12", decoratorsBeforeExport: "before" }),
Expand Down
4 changes: 1 addition & 3 deletions packages/babel-preset-env/test/import-meta.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import env from "../lib/index.js";
import * as babel from "@babel/core";
import { itNoESM } from "$repo-utils";

const itBabel7NoESM = process.env.BABEL_8_BREAKING ? it.skip : itNoESM;
import { itBabel7NoESM } from "$repo-utils";

describe("preset-env", () => {
function extractParserOptions(api, { ref }) {
Expand Down
6 changes: 1 addition & 5 deletions packages/babel-preset-env/test/index.skip-bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ if (/* commonjs */ _transformations.default) {
pluginCoreJS3 = _pluginCoreJS3_esm;
pluginRegenerator = _pluginRegenerator_esm;
}

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;

const describeBabel7 = process.env.BABEL_8_BREAKING ? describe.skip : describe;
import { itBabel7, itBabel8, describeBabel7 } from "$repo-utils";

describe("babel-preset-env", () => {
describe("transformIncludesAndExcludes", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import _normalizeOptions, {
normalizePluginName,
} from "../lib/normalize-options.js";
const normalizeOptions = _normalizeOptions.default || _normalizeOptions;

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
import { itBabel7 } from "$repo-utils";

describe("normalize-options", () => {
describe("normalizeOptions", () => {
Expand Down
11 changes: 2 additions & 9 deletions packages/babel-preset-env/test/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ import * as babel from "@babel/core";
import env from "../lib/index.js";
import path from "path";
import { fileURLToPath } from "url";
import { USE_ESM, commonJS } from "$repo-utils";
import { commonJS, itBabel7, itBabel7GteNoESM } from "$repo-utils";

const { require } = commonJS(import.meta.url);

const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
const itBabel7NodeGte14NoESM =
process.env.BABEL_8_BREAKING ||
parseInt(process.versions.node) < 14 ||
USE_ESM
? it.skip
: it;

describe("regressions", () => {
it("empty", () => {
// TODO(Babel 8): Delete this file
Expand Down Expand Up @@ -54,6 +46,7 @@ describe("regressions", () => {
},
);

const itBabel7NodeGte14NoESM = itBabel7GteNoESM("14.0.0");
// create-reat-app missing dependency fallback
// jest fake timers only work in the Jest version we are using for Node.js 14+
itBabel7NodeGte14NoESM(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _normalizeOptions from "../lib/normalize-options.js";
const normalizeOptions = _normalizeOptions.default || _normalizeOptions;
import { describeBabel8, describeBabel7 } from "$repo-utils";

describe("normalize options", () => {
(process.env.BABEL_8_BREAKING ? describe : describe.skip)("Babel 8", () => {
describeBabel8("Babel 8", () => {
it("should throw on unknown options", () => {
expect(() => normalizeOptions({ al: true }))
.toThrowErrorMatchingInlineSnapshot(`
Expand Down Expand Up @@ -34,7 +35,7 @@ describe("normalize options", () => {
`);
});
});
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => {
describeBabel7("Babel 7", () => {
it("should not throw on unknown options", () => {
expect(() => normalizeOptions({ allDeclareField: true })).not.toThrow();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-preset-react/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _reactPreset from "../lib/index.js";
const reactPreset = _reactPreset.default || _reactPreset;

const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
import { itBabel8 } from "$repo-utils";

describe("react preset", () => {
it("does throw clear error when no options passed for Babel 6", () => {
Expand Down