From 96dcfd5516bad92e95da85e6f4eafbcbcd890b1b Mon Sep 17 00:00:00 2001 From: Alec M Date: Thu, 6 Jun 2024 12:34:53 -0400 Subject: [PATCH 1/2] fix: Jest not failing for warnings --- src/setupTests.tsx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/setupTests.tsx b/src/setupTests.tsx index 4fc5edea..d9f186a1 100644 --- a/src/setupTests.tsx +++ b/src/setupTests.tsx @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ // eslint-disable-next-line max-classes-per-file import "@testing-library/jest-dom"; import "jest-axe/extend-expect"; @@ -58,3 +59,14 @@ jest.mock("recharts", () => ({ ...jest.requireActual("recharts"), ResponsiveContainer: MockResponsiveContainer, })); + +/** + * Prevents the console.error and console.warn from silently failing + * in tests by throwing an error when called + */ +console.error = (message, ...rest) => { + throw message instanceof Error ? message : new Error(message, ...rest); +}; +console.warn = (message, ...rest) => { + throw message instanceof Error ? message : new Error(message, ...rest); +}; From 68c14e9363d72319d15b30d3cb4221f2d9611bff Mon Sep 17 00:00:00 2001 From: Alec M Date: Thu, 6 Jun 2024 12:50:08 -0400 Subject: [PATCH 2/2] use `jest-fail-on-console` package --- package-lock.json | 6 ++++++ package.json | 1 + src/setupTests.tsx | 12 +++++------- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index c384606d..40115009 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "graphql": "^16.7.1", "graphql-tag": "^2.12.6", "jest-axe": "^8.0.0", + "jest-fail-on-console": "^3.3.0", "lodash": "^4.17.21", "notistack": "^3.0.1", "papaparse": "^5.4.1", @@ -11243,6 +11244,11 @@ "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" } }, + "node_modules/jest-fail-on-console": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/jest-fail-on-console/-/jest-fail-on-console-3.3.0.tgz", + "integrity": "sha512-J9rnFQvQwkcGJw01zCEKe2Uag+E926lFgIyaQGep2LqhQH7OCRHyD+tm/jnNoKlSRnOBO60DmzMjeQAVI3f5cw==" + }, "node_modules/jest-get-type": { "version": "27.5.1", "license": "MIT", diff --git a/package.json b/package.json index 99624b0f..f2b47e20 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "graphql": "^16.7.1", "graphql-tag": "^2.12.6", "jest-axe": "^8.0.0", + "jest-fail-on-console": "^3.3.0", "lodash": "^4.17.21", "notistack": "^3.0.1", "papaparse": "^5.4.1", diff --git a/src/setupTests.tsx b/src/setupTests.tsx index d9f186a1..6b56d475 100644 --- a/src/setupTests.tsx +++ b/src/setupTests.tsx @@ -1,7 +1,7 @@ -/* eslint-disable no-console */ // eslint-disable-next-line max-classes-per-file import "@testing-library/jest-dom"; import "jest-axe/extend-expect"; +import failOnConsole from "jest-fail-on-console"; /** * Mocks the enqueueSnackbar function from notistack for testing @@ -64,9 +64,7 @@ jest.mock("recharts", () => ({ * Prevents the console.error and console.warn from silently failing * in tests by throwing an error when called */ -console.error = (message, ...rest) => { - throw message instanceof Error ? message : new Error(message, ...rest); -}; -console.warn = (message, ...rest) => { - throw message instanceof Error ? message : new Error(message, ...rest); -}; +failOnConsole({ + shouldFailOnWarn: true, + shouldFailOnError: true, +});