chore(build): replace replaceable jest-mock-console with native Jest spies (#38643)

Signed-off-by: hainenber <dotronghai96@gmail.com>
This commit is contained in:
Đỗ Trọng Hải
2026-05-12 21:32:08 +07:00
committed by GitHub
parent b0c5b061c5
commit 4a79896bb2
7 changed files with 12 additions and 55 deletions

View File

@@ -77,6 +77,10 @@ const restrictedImportsRules = {
name: 'query-string',
message: 'Please use the URLSearchParams API instead of query-string.',
},
'no-jest-mock-console': {
name: 'jest-mock-console',
message: 'Please use native Jest spies, i.e. jest.spyOn(console, "warn")',
}
};
module.exports = {

View File

@@ -30223,16 +30223,6 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-mock-console": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/jest-mock-console/-/jest-mock-console-2.0.0.tgz",
"integrity": "sha512-7zrKtXVut+6doalosFxw/2O9spLepQJ9VukODtyLIub2fFkWKe1TyQrxr/GyQogTQcdkHfhvFJdx1OEzLqf/mw==",
"dev": true,
"license": "MIT",
"peerDependencies": {
"jest": ">= 22.4.2"
}
},
"node_modules/jest-pnp-resolver": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz",
@@ -50216,7 +50206,6 @@
"@types/rison": "0.1.0",
"@types/seedrandom": "^3.0.8",
"fetch-mock": "^12.6.0",
"jest-mock-console": "^2.0.0",
"resize-observer-polyfill": "1.5.1",
"timezone-mock": "^1.4.2"
},

View File

@@ -83,7 +83,6 @@
"@types/rison": "0.1.0",
"@types/seedrandom": "^3.0.8",
"fetch-mock": "^12.6.0",
"jest-mock-console": "^2.0.0",
"resize-observer-polyfill": "1.5.1",
"timezone-mock": "^1.4.2"
},

View File

@@ -19,7 +19,6 @@
import '@testing-library/jest-dom';
import { render, screen } from '@superset-ui/core/spec';
import mockConsole, { RestoreConsole } from 'jest-mock-console';
import { triggerResizeObserver } from 'resize-observer-polyfill';
import { ErrorBoundary } from 'react-error-boundary';
@@ -66,8 +65,6 @@ function getDimensionText(container: HTMLElement) {
describe('SuperChart', () => {
jest.setTimeout(5000);
let restoreConsole: RestoreConsole;
const plugins = [
new DiligentChartPlugin().configure({ key: ChartKeys.DILIGENT }),
new BuggyChartPlugin().configure({ key: ChartKeys.BUGGY }),
@@ -80,14 +77,9 @@ describe('SuperChart', () => {
});
beforeEach(() => {
restoreConsole = mockConsole();
triggerResizeObserver([]); // Reset any pending resize observers
});
afterEach(() => {
restoreConsole();
});
describe('includes ErrorBoundary', () => {
let expectedErrors = 0;
let actualErrors = 0;

View File

@@ -18,7 +18,6 @@
*/
import '@testing-library/jest-dom';
import mockConsole, { RestoreConsole } from 'jest-mock-console';
import { ChartProps } from '@superset-ui/core';
import { supersetTheme } from '@apache-superset/core/theme';
import { render, screen, waitFor } from '@superset-ui/core/spec';
@@ -38,8 +37,6 @@ describe('SuperChartCore', () => {
new SlowChartPlugin().configure({ key: ChartKeys.SLOW }),
];
let restoreConsole: RestoreConsole;
beforeAll(() => {
jest.setTimeout(30000);
plugins.forEach(p => {
@@ -53,14 +50,6 @@ describe('SuperChartCore', () => {
});
});
beforeEach(() => {
restoreConsole = mockConsole();
});
afterEach(() => {
restoreConsole();
});
describe('registered charts', () => {
test('renders registered chart', async () => {
const { container } = render(

View File

@@ -19,7 +19,6 @@
import '@testing-library/jest-dom';
import { ComponentType } from 'react';
import mockConsole, { RestoreConsole } from 'jest-mock-console';
import { render as renderTestComponent, screen } from '@testing-library/react';
import createLoadableRenderer, {
LoadableRenderer as LoadableRendererType,
@@ -33,10 +32,8 @@ describe('createLoadableRenderer', () => {
let render: (loaded: { Chart: ComponentType }) => JSX.Element;
let loading: () => JSX.Element;
let LoadableRenderer: LoadableRendererType<{}>;
let restoreConsole: RestoreConsole;
beforeEach(() => {
restoreConsole = mockConsole();
loadChartSuccess = jest.fn(() => Promise.resolve(TestComponent));
render = jest.fn(loaded => {
const { Chart } = loaded;
@@ -54,10 +51,6 @@ describe('createLoadableRenderer', () => {
});
});
afterEach(() => {
restoreConsole();
});
describe('returns a LoadableRenderer class', () => {
test('LoadableRenderer.preload() preloads the lazy-load components', () => {
expect(LoadableRenderer.preload).toBeInstanceOf(Function);

View File

@@ -18,11 +18,18 @@
*/
/* eslint no-console: 0 */
import mockConsole from 'jest-mock-console';
import { Registry, OverwritePolicy } from '@superset-ui/core';
const loader = () => 'testValue';
const consoleWarnSpy = jest.spyOn(console, 'warn');
const consoleErrorSpy = jest.spyOn(console, 'error');
beforeEach(() => {
consoleErrorSpy.mockClear();
consoleWarnSpy.mockClear();
});
describe('Registry', () => {
test('exists', () => {
expect(Registry !== undefined).toBe(true);
@@ -308,18 +315,15 @@ describe('Registry', () => {
describe('=ALLOW', () => {
describe('.registerValue(key, value)', () => {
test('registers normally', () => {
const restoreConsole = mockConsole();
const registry = new Registry();
registry.registerValue('a', 'testValue');
expect(() => registry.registerValue('a', 'testValue2')).not.toThrow();
expect(registry.get('a')).toEqual('testValue2');
expect(console.warn).not.toHaveBeenCalled();
restoreConsole();
});
});
describe('.registerLoader(key, loader)', () => {
test('registers normally', () => {
const restoreConsole = mockConsole();
const registry = new Registry();
registry.registerLoader('a', () => 'testValue');
expect(() =>
@@ -327,14 +331,12 @@ describe('Registry', () => {
).not.toThrow();
expect(registry.get('a')).toEqual('testValue2');
expect(console.warn).not.toHaveBeenCalled();
restoreConsole();
});
});
});
describe('=WARN', () => {
describe('.registerValue(key, value)', () => {
test('warns when overwrite', () => {
const restoreConsole = mockConsole();
const registry = new Registry({
overwritePolicy: OverwritePolicy.Warn,
});
@@ -342,12 +344,10 @@ describe('Registry', () => {
expect(() => registry.registerValue('a', 'testValue2')).not.toThrow();
expect(registry.get('a')).toEqual('testValue2');
expect(console.warn).toHaveBeenCalled();
restoreConsole();
});
});
describe('.registerLoader(key, loader)', () => {
test('warns when overwrite', () => {
const restoreConsole = mockConsole();
const registry = new Registry({
overwritePolicy: OverwritePolicy.Warn,
});
@@ -357,7 +357,6 @@ describe('Registry', () => {
).not.toThrow();
expect(registry.get('a')).toEqual('testValue2');
expect(console.warn).toHaveBeenCalled();
restoreConsole();
});
});
});
@@ -438,14 +437,6 @@ describe('Registry', () => {
});
describe('with a broken listener', () => {
let restoreConsole: any;
beforeEach(() => {
restoreConsole = mockConsole();
});
afterEach(() => {
restoreConsole();
});
test('keeps working', () => {
const errorListener = jest.fn().mockImplementation(() => {
throw new Error('test error');