Skip to content

Commit

Permalink
src/test/jest.setup.js: add log supressor
Browse files Browse the repository at this point in the history
Also filters warnings due to
data-driven-forms/react-forms#1352 from the
test logs.
  • Loading branch information
croissanne committed May 24, 2023
1 parent 1697ae8 commit 414e819
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"jest-canvas-mock"
],
"setupFilesAfterEnv": [
"./jest.setup.js"
"./src/test/jest.setup.js"
]
},
"devDependencies": {
Expand Down
63 changes: 63 additions & 0 deletions src/test/jest.setup.js
Original file line number Diff line number Diff line change
@@ -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 (let 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);

0 comments on commit 414e819

Please sign in to comment.