jest: fail on error messages

Use the library https://github.com/ValentinH/jest-fail-on-console to
spy on the errors to make test failing if any occur.

Transfer the configuration from the previous FilteredConsole to this new
library.

Allow a few targeted errors to occur to give some time to refactor while
keeping the benefits to have failing on new error messages.
This commit is contained in:
Thomas Lavocat 2023-08-08 17:16:26 +02:00 committed by Thomas Lavocat
parent 697d0af4b5
commit e8380c12b9
3 changed files with 126 additions and 52 deletions

80
package-lock.json generated
View file

@ -62,6 +62,7 @@
"jest": "^29.6.2",
"jest-canvas-mock": "2.5.2",
"jest-environment-jsdom": "29.6.2",
"jest-fail-on-console": "^3.1.1",
"msw": "^1.2.3",
"npm-run-all": "4.1.5",
"postcss-scss": "4.0.6",
@ -12752,6 +12753,85 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
},
"node_modules/jest-fail-on-console": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/jest-fail-on-console/-/jest-fail-on-console-3.1.1.tgz",
"integrity": "sha512-g9HGhKcWOz8lHeZhLCXGg+RD/7upngiKkkBrHimsO/tGB0qi++QZffOgybjeI2bDW1qgdFiJJEG6t/WeTlfmOw==",
"dev": true,
"dependencies": {
"chalk": "^4.1.0"
}
},
"node_modules/jest-fail-on-console/node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/jest-fail-on-console/node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/jest-fail-on-console/node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/jest-fail-on-console/node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/jest-fail-on-console/node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"dev": true,
"engines": {
"node": ">=8"
}
},
"node_modules/jest-fail-on-console/node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/jest-get-type": {
"version": "29.4.3",
"dev": true,

View file

@ -87,6 +87,7 @@
"jest": "^29.6.2",
"jest-canvas-mock": "2.5.2",
"jest-environment-jsdom": "29.6.2",
"jest-fail-on-console": "^3.1.1",
"msw": "^1.2.3",
"npm-run-all": "4.1.5",
"postcss-scss": "4.0.6",

View file

@ -1,6 +1,51 @@
import 'whatwg-fetch';
import failOnConsole from 'jest-fail-on-console';
import { server } from './mocks/server';
// or with options:
failOnConsole({
shouldFailOnWarn: false,
silenceMessage: (errorMessage) => {
if (
// Upgrading @patternfly/react-core caused propTypes error in Pf4FormTemplate
// https://github.com/data-driven-forms/react-forms/issues/1352
errorMessage.includes(
'Failed prop type: Invalid prop `FormWrapper` supplied to `FormTemplate`, expected one of type [function].'
)
) {
// eslint-disable-next-line no-console
console.log(
'Suppressed error',
'Failed prop type: Invalid prop `FormWrapper` supplied to `FormTemplate`, expected one of type [function]',
'see [https://github.com/data-driven-forms/react-forms/issues/1352]'
);
return true;
}
// TODO [2023-08] CreateImageWizard warnings to be fixed later.
if (
errorMessage.includes('CreateImageWizard') &&
(errorMessage.includes(
'Cannot update a component (`ReactFinalForm`) while rendering a different component (`TargetEnvironment`)'
) ||
errorMessage.includes(
'Cannot update a component (`TextField`) while rendering a different component (`TextField`). To locate the bad setState() call inside `TextField`'
) ||
errorMessage.includes(
'Cannot update a component (`TextField`) while rendering a different component (`FormSpy`)'
) ||
errorMessage.includes(
"Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application."
))
) {
// eslint-disable-next-line no-console
console.warn(errorMessage);
return true;
}
return false;
},
});
jest.mock('@unleash/proxy-client-react', () => ({
useUnleashContext: () => jest.fn(),
useFlag: jest.fn(() => true),
@ -9,55 +54,3 @@ jest.mock('@unleash/proxy-client-react', () => ({
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);