diff --git a/components/_util/__tests__/warning.test.js b/components/_util/__tests__/warning.test.js new file mode 100644 index 0000000000..2eeeb0f1bf --- /dev/null +++ b/components/_util/__tests__/warning.test.js @@ -0,0 +1,65 @@ +describe('Test warning', () => { + let spy: jest.SpyInstance; + + beforeAll(() => { + spy = jest.spyOn(console, 'error'); + }); + + afterAll(() => { + spy.mockRestore(); + }); + + beforeEach(() => { + jest.resetModules(); + }); + + afterEach(() => { + spy.mockReset(); + }); + + it('Test noop', async () => { + const { noop } = await import('../warning'); + const value = noop(); + + expect(value).toBe(undefined); + expect(spy).not.toHaveBeenCalled(); + expect(() => { + noop(); + }).not.toThrow(); + }); + + describe('process.env.NODE_ENV !== "production"', () => { + it('If `false`, exec `console.error`', async () => { + const warning = (await import('../warning')).default; + warning(false, 'error'); + + expect(spy).toHaveBeenCalled(); + }); + + it('If `true`, do not exec `console.error`', async () => { + const warning = (await import('../warning')).default; + warning(true, 'error message'); + + expect(spy).not.toHaveBeenCalled(); + }); + }); + + describe('process.env.NODE_ENV === "production"', () => { + it('Whether `true` or `false`, do not exec `console.error`', async () => { + const prevEnv = process.env.NODE_ENV; + process.env.NODE_ENV = 'production'; + + const { default: warning, noop } = await import('../warning'); + + expect(warning).toEqual(noop); + + warning(false, 'error message'); + expect(spy).not.toHaveBeenCalled(); + + warning(true, 'error message'); + expect(spy).not.toHaveBeenCalled(); + + process.env.NODE_ENV = prevEnv; + }); + }); +}); diff --git a/components/_util/devWarning.ts b/components/_util/devWarning.ts deleted file mode 100644 index 34090ca306..0000000000 --- a/components/_util/devWarning.ts +++ /dev/null @@ -1,12 +0,0 @@ -import devWarning, { resetWarned } from 'rc-util/lib/warning'; - -export { resetWarned }; - -export default (valid: boolean, component: string, message: string): void => { - devWarning(valid, `[antd: ${component}] ${message}`); - - // StrictMode will inject console which will not throw warning in React 17. - if (process.env.NODE_ENV === 'test') { - resetWarned(); - } -}; diff --git a/components/_util/warning.ts b/components/_util/warning.ts new file mode 100644 index 0000000000..ff7357f4f9 --- /dev/null +++ b/components/_util/warning.ts @@ -0,0 +1,21 @@ +import rcWarning, { resetWarned } from 'rc-util/lib/warning'; + +export { resetWarned }; +export function noop() {} + +type Warning = (valid: boolean, component: string, message: string) => void; + +// eslint-disable-next-line import/no-mutable-exports +let warning: Warning = noop; +if (process.env.NODE_ENV !== 'production') { + warning = (valid, component, message) => { + rcWarning(valid, `[antd: ${component}] ${message}`); + + // StrictMode will inject console which will not throw warning in React 17. + if (process.env.NODE_ENV === 'test') { + resetWarned(); + } + }; +} + +export default warning; diff --git a/components/auto-complete/__tests__/index.test.js b/components/auto-complete/__tests__/index.test.js index 4c925f0e94..702e885278 100644 --- a/components/auto-complete/__tests__/index.test.js +++ b/components/auto-complete/__tests__/index.test.js @@ -42,16 +42,15 @@ describe('AutoComplete', () => { }); it('AutoComplete throws error when contains invalid dataSource', () => { - jest.spyOn(console, 'error').mockImplementation(() => undefined); - expect(() => { - mount( - {}]}> -