Skip to content

Commit

Permalink
Skip deprecation warning tests when in a folder named @babel (#15936)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 6, 2023
1 parent 9b700f4 commit 21a6a2c
Showing 1 changed file with 64 additions and 54 deletions.
118 changes: 64 additions & 54 deletions packages/babel-traverse/test/traverse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse } from "@babel/parser";
import * as t from "@babel/types";
import { commonJS } from "$repo-utils";

import _traverse from "../lib/index.js";
const traverse = _traverse.default || _traverse;
Expand Down Expand Up @@ -375,59 +376,68 @@ describe("traverse", function () {
});
});
describe("traverse.explode", () => {
describe("deprecated types and aliases", () => {
beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
console.warn.mockClear();
});
afterAll(() => {
console.warn.mockRestore();
});
it("should warn for deprecated node types", function testFn1() {
const visitNumericLiteral = () => {};
const visitor = {
NumberLiteral: visitNumericLiteral,
};
traverse.explode(visitor);
expect(console.warn).toHaveBeenCalledWith(
expect.stringMatching(
/Visitor `NumberLiteral` has been deprecated, please migrate to `NumericLiteral`[^]+at.+testFn1/,
),
);
expect(visitor).toHaveProperty("NumericLiteral.enter", [
visitNumericLiteral,
]);
});

it("should warn for deprecated aliases", function testFn2() {
const visitImportOrExportDeclaration = () => {};
const visitor = {
ModuleDeclaration: visitImportOrExportDeclaration,
};
traverse.explode(visitor);
expect(console.warn).toHaveBeenCalledWith(
expect.stringMatching(
/Visitor `ModuleDeclaration` has been deprecated, please migrate to `ImportOrExportDeclaration`[^]+at.+testFn2/,
),
);
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
visitImportOrExportDeclaration,
]);
});

it("should not warn deprecations if usage comes from a @babel/* package", () => {
const visitImportOrExportDeclaration = () => {};
const visitor = {
ModuleDeclaration: visitImportOrExportDeclaration,
};
callFromAtBabelPackage(traverse.explode, visitor);
expect(console.warn).not.toHaveBeenCalled();
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
visitImportOrExportDeclaration,
]);
});
});
const { __dirname } = commonJS(import.meta.url);
// These tests fail if the monorepo is in a folder named `@babel`, due to
// https://github.com/babel/babel/blob/24106296d4e80ff2c7cb7f956bab66aa819b2c35/packages/babel-types/src/utils/deprecationWarning.ts#L40
// This happens, for example, in nodejs/citgm.
const shuoldSkip = /[\\/]@babel[\\/]/.test(__dirname);

(shuoldSkip ? describe.skip : describe)(
"deprecated types and aliases",
() => {
beforeAll(() => {
jest.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
console.warn.mockClear();
});
afterAll(() => {
console.warn.mockRestore();
});
it("should warn for deprecated node types", function testFn1() {
const visitNumericLiteral = () => {};
const visitor = {
NumberLiteral: visitNumericLiteral,
};
traverse.explode(visitor);
expect(console.warn).toHaveBeenCalledWith(
expect.stringMatching(
/Visitor `NumberLiteral` has been deprecated, please migrate to `NumericLiteral`[^]+at.+testFn1/,
),
);
expect(visitor).toHaveProperty("NumericLiteral.enter", [
visitNumericLiteral,
]);
});

it("should warn for deprecated aliases", function testFn2() {
const visitImportOrExportDeclaration = () => {};
const visitor = {
ModuleDeclaration: visitImportOrExportDeclaration,
};
traverse.explode(visitor);
expect(console.warn).toHaveBeenCalledWith(
expect.stringMatching(
/Visitor `ModuleDeclaration` has been deprecated, please migrate to `ImportOrExportDeclaration`[^]+at.+testFn2/,
),
);
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
visitImportOrExportDeclaration,
]);
});

it("should not warn deprecations if usage comes from a @babel/* package", () => {
const visitImportOrExportDeclaration = () => {};
const visitor = {
ModuleDeclaration: visitImportOrExportDeclaration,
};
callFromAtBabelPackage(traverse.explode, visitor);
expect(console.warn).not.toHaveBeenCalled();
expect(visitor).toHaveProperty("ImportDeclaration.enter", [
visitImportOrExportDeclaration,
]);
});
},
);
});
});

0 comments on commit 21a6a2c

Please sign in to comment.