From 80e17a05690c0fc5a46762f100399670b807b845 Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Tue, 23 May 2023 15:53:06 +0200 Subject: [PATCH] src/test/jest.setup.js: add log supressor Also filters warnings due to https://github.com/data-driven-forms/react-forms/issues/1352 from the test logs. --- package.json | 2 +- src/test/jest.setup.js | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/test/jest.setup.js diff --git a/package.json b/package.json index 74e7f1958..f645586dd 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "jest-canvas-mock" ], "setupFilesAfterEnv": [ - "./jest.setup.js" + "./src/test/jest.setup.js" ] }, "devDependencies": { diff --git a/src/test/jest.setup.js b/src/test/jest.setup.js new file mode 100644 index 000000000..c6d2d8092 --- /dev/null +++ b/src/test/jest.setup.js @@ -0,0 +1,63 @@ +import 'whatwg-fetch'; +import { server } from './mocks/server'; + +jest.mock('@unleash/proxy-client-react', () => ({ + useUnleashContext: () => jest.fn(), + useFlag: jest.fn(() => true), +})); + +beforeAll(() => server.listen()); +afterEach(() => server.resetHandlers()); +afterAll(() => server.close()); + +// Upgrading @patternfly/react-core caused propTypes error in Pf4FormTemplate +// https://github.com/data-driven-forms/react-forms/issues/1352 +const filter1 = (args) => { + if ( + args[2] === + 'Invalid prop `FormWrapper` supplied to `FormTemplate`, expected one of type [function].' + ) { + return [true, args[2]]; + } + return [false, null]; +}; + +class FilteredConsole { + constructor(console) { + this.console = console; + this.filters = [filter1]; + } + + logSuppressedError(err) { + this.console.info('Suppressed error: ', err); + } + + filter(...args) { + for (const fn of this.filters) { + const [f, msg] = fn(args); + if (f) { + this.logSuppressedError(msg); + return true; + } + } + return false; + } + + log(...args) { + this.console.log(...args); + } + + info(...args) { + this.console.info(...args); + } + + warn(...args) { + if (!this.filter(...args)) this.console.warn(...args); + } + + error(...args) { + if (!this.filter(...args)) this.console.error(...args); + } +} + +window.console = new FilteredConsole(window.console);