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

Skip deprecation warning tests when in a folder named @babel #15936

Merged
merged 1 commit into from
Sep 6, 2023
Merged
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
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,
]);
});
},
);
});
});