Skip to content

Commit f64b04e

Browse files
JLHwungnicolo-ribaudoliuxingbaoyu
authored
extract more test helpers to repo-utils (#15902)
Co-authored-by: Nicolò Ribaudo <[email protected]> Co-authored-by: liuxingbaoyu <[email protected]>
1 parent f9d1adf commit f64b04e

File tree

29 files changed

+125
-137
lines changed

29 files changed

+125
-137
lines changed

babel.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,10 @@ function pluginImportMetaUrl({ types: t, template }) {
696696
// We must be sure to run this before the istanbul plugins, because its
697697
// instrumentation breaks our detection.
698698
programPath.traverse({
699-
// fileURLToPath(import.meta.url)
700699
CallExpression(path) {
701700
const { node } = path;
702701

702+
// fileURLToPath(import.meta.url)
703703
if (
704704
(function () {
705705
if (
@@ -731,6 +731,7 @@ function pluginImportMetaUrl({ types: t, template }) {
731731
return;
732732
}
733733

734+
// const { __dirname: cwd } = commonJS(import.meta.url)
734735
if (
735736
!t.isIdentifier(node.callee, { name: "commonJS" }) ||
736737
node.arguments.length !== 1

eslint/babel-eslint-parser/test/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import path from "path";
22
import escope from "eslint-scope";
33
import unpad from "dedent";
4-
import { fileURLToPath } from "url";
5-
import { createRequire } from "module";
64
import { parseForESLint as parseForESLintOriginal } from "../lib/index.cjs";
75
import { ESLint } from "eslint";
6+
import { itDummy, commonJS } from "$repo-utils";
87

98
function parseForESLint(code, options) {
109
return parseForESLintOriginal(code, {
@@ -19,11 +18,12 @@ function parseForESLint(code, options) {
1918

2019
const ESLINT_VERSION = ESLint.version;
2120
const isESLint7 = ESLINT_VERSION.startsWith("7.");
22-
const dirname = path.dirname(fileURLToPath(import.meta.url));
21+
const { __dirname: dirname, require } = commonJS(import.meta.url);
2322

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

2828
const BABEL_OPTIONS = {
2929
configFile: path.resolve(
@@ -122,8 +122,6 @@ describe("Babel and Espree", () => {
122122
}
123123

124124
beforeAll(() => {
125-
const require = createRequire(import.meta.url);
126-
127125
// Use the version of Espree that is a dependency of
128126
// the version of ESLint we are testing against.
129127
const espreePath = require.resolve("espree", {

eslint/babel-eslint-tests/test/integration/config-files.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { ESLint } from "eslint";
22
import path from "path";
33
import { fileURLToPath } from "url";
4-
import { USE_ESM } from "$repo-utils";
4+
import { itESM, itGteNoESM } from "$repo-utils";
55

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

119
itESM("works with babel.config.mjs", async () => {
1210
const engine = new ESLint({ ignore: false });

packages/babel-core/test/api.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as babel from "../lib/index.js";
22
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping";
33
import path from "path";
44
import generator from "@babel/generator";
5-
import { fileURLToPath } from "url";
65

76
import _Plugin from "../lib/config/plugin.js";
87
const Plugin = _Plugin.default || _Plugin;
@@ -11,10 +10,10 @@ import presetEnv from "@babel/preset-env";
1110
import pluginSyntaxFlow from "@babel/plugin-syntax-flow";
1211
import pluginSyntaxJSX from "@babel/plugin-syntax-jsx";
1312
import pluginFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
13+
import { itBabel8, commonJS } from "$repo-utils";
1414

15-
const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
16-
17-
const cwd = path.dirname(fileURLToPath(import.meta.url));
15+
const { __dirname } = commonJS(import.meta.url);
16+
const cwd = __dirname;
1817

1918
function assertIgnored(result) {
2019
expect(result).toBeNull();

packages/babel-core/test/async.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import {
66
spawnTransformAsync,
77
spawnTransformSync,
88
supportsESM,
9-
itESM,
109
} from "./helpers/esm.js";
1110

12-
import { itGte } from "$repo-utils";
11+
import { itGte, itESM } from "$repo-utils";
1312

1413
// "minNodeVersion": "8.0.0" <-- For Ctrl+F when dropping node 6
1514
const nodeGte8 = itGte("8.0.0");

packages/babel-core/test/config-loading.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@ import {
88
createConfigItemSync,
99
} from "../lib/index.js";
1010
import path from "path";
11-
import { fileURLToPath } from "url";
12-
import { createRequire } from "module";
13-
import { itNoWin32 } from "$repo-utils";
11+
import { itNoWin32, itBabel8, commonJS } from "$repo-utils";
1412

15-
const require = createRequire(import.meta.url);
16-
17-
const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
13+
const { require, __dirname } = commonJS(import.meta.url);
1814

1915
describe("@babel/core config loading", () => {
2016
const FILEPATH = path.join(
21-
path.dirname(fileURLToPath(import.meta.url)),
17+
__dirname,
2218
"fixtures",
2319
"config-loading",
2420
"folder",
@@ -152,7 +148,7 @@ describe("@babel/core config loading", () => {
152148

153149
it("should always set 'rootMode' to 'root'", async () => {
154150
const cwd = path.join(
155-
path.dirname(fileURLToPath(import.meta.url)),
151+
__dirname,
156152
"fixtures",
157153
"config-loading",
158154
"root",

packages/babel-core/test/esm-cjs-integration.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { execFile } from "child_process";
22
import { createRequire } from "module";
3-
import { outputType } from "./helpers/esm.js";
3+
import { describeESM } from "$repo-utils";
44

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

@@ -18,7 +18,11 @@ async function run(name) {
1818
});
1919
}
2020

21-
(outputType === "module" ? describe : describe.skip)("usage from cjs", () => {
21+
describe("dummy", () => {
22+
it("dummy", () => {});
23+
});
24+
25+
describeESM("usage from cjs", () => {
2226
it("lazy plugin required", async () => {
2327
expect(await run("lazy-plugin-required.cjs")).toMatchInlineSnapshot(`
2428
Object {

packages/babel-core/test/helpers/esm.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export const outputType = USE_ESM ? "module" : "script";
1414

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

17-
export const itESM = supportsESM ? it : it.skip;
18-
1917
export function skipUnsupportedESM(name) {
2018
if (!supportsESM) {
2119
console.warn(

packages/babel-core/test/option-manager.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as babel from "../lib/index.js";
22
import path from "path";
33
import { fileURLToPath } from "url";
4-
import { USE_ESM } from "$repo-utils";
4+
import { itBabel7, itBabel7NoESM } from "$repo-utils";
55

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

@@ -13,9 +13,6 @@ function loadOptionsAsync(opts) {
1313
return babel.loadOptionsAsync({ cwd, ...opts });
1414
}
1515

16-
const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
17-
const itBabel7NoESM = process.env.BABEL_8_BREAKING || USE_ESM ? it.skip : it;
18-
1916
describe("option-manager", () => {
2017
itBabel7NoESM("throws for babel 5 plugin", () => {
2118
return expect(() => {

packages/babel-helper-compilation-targets/test/targets-parser.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { fileURLToPath } from "url";
44

55
import _getTargets from "../lib/index.js";
66
const getTargets = _getTargets.default || _getTargets;
7-
8-
const itBabel7 = process.env.BABEL_8_BREAKING ? it.skip : it;
9-
const itBabel8 = process.env.BABEL_8_BREAKING ? it : it.skip;
7+
import { itBabel8, itBabel7 } from "$repo-utils";
108

119
describe("getTargets", () => {
1210
it("parses", () => {

0 commit comments

Comments
 (0)