From 8a7301d370ced4932de376fed9bace2724f6b14d Mon Sep 17 00:00:00 2001 From: Robert Sese Date: Fri, 25 Feb 2022 10:49:27 -0600 Subject: [PATCH] Fail on console.error in tests with failOnConsole (#25600) * Install jest-fail-on-console * Setup and use failOnConsole * Add intentional 'unique key' error * Revert "Add intentional 'unique key' error" This reverts commit 5f75f069d80df36ce632421ae7ad26e98ac17ad3. * Comment about why use this package --- jest.config.js | 2 +- jest.setup.js | 12 ++++++++++++ package-lock.json | 47 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 jest.setup.js diff --git a/jest.config.js b/jest.config.js index 65cd8080c159..6b0e3458c1f1 100644 --- a/jest.config.js +++ b/jest.config.js @@ -29,7 +29,7 @@ module.exports = { }, reporters, modulePathIgnorePatterns: ['assets/'], - setupFilesAfterEnv: ['@alex_neo/jest-expect-message'], + setupFilesAfterEnv: ['./jest.setup.js', '@alex_neo/jest-expect-message'], ...(isBrowser ? {} : { testEnvironment: 'node' }), testPathIgnorePatterns: [ 'node_modules/', diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 000000000000..a7f994411e5d --- /dev/null +++ b/jest.setup.js @@ -0,0 +1,12 @@ +import failOnConsole from 'jest-fail-on-console' + +// Jest tests don't fail in some cases where we would see an error in DevTools +// Console when running locally and we would also see the error in the test +// output. This includes the React `Each child in a list should have a unique +// "key" prop` error example. +// +// To catch this and fail tests in cases like this, we use `jest-fail-on-console` +// to fail on calls to `console.error()`. +failOnConsole({ + shouldFailOnWarn: false, +}) diff --git a/package-lock.json b/package-lock.json index ce094e1af8d6..9fae9b4cdbfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -138,6 +138,7 @@ "japanese-characters": "^1.1.0", "javascript-stringify": "^2.1.0", "jest": "^27.4.7", + "jest-fail-on-console": "^2.2.3", "jest-github-actions-reporter": "^1.0.3", "jest-slow-test-reporter": "^1.0.0", "linkinator": "^3.0.3", @@ -12195,6 +12196,31 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/jest-fail-on-console": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/jest-fail-on-console/-/jest-fail-on-console-2.2.3.tgz", + "integrity": "sha512-fwSslH/Stq1NqN/z7PMYzUuHZxqimPCJOjj3D3RovpYCPXj/2WRiradhpVUm+tsSw4zW4MB9iJcXz6ggmw2nEg==", + "dev": true, + "dependencies": { + "chalk": "^4.1.0" + } + }, + "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-get-type": { "version": "27.4.0", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-27.4.0.tgz", @@ -32059,6 +32085,27 @@ } } }, + "jest-fail-on-console": { + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/jest-fail-on-console/-/jest-fail-on-console-2.2.3.tgz", + "integrity": "sha512-fwSslH/Stq1NqN/z7PMYzUuHZxqimPCJOjj3D3RovpYCPXj/2WRiradhpVUm+tsSw4zW4MB9iJcXz6ggmw2nEg==", + "dev": true, + "requires": { + "chalk": "^4.1.0" + }, + "dependencies": { + "chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + } + } + }, "jest-get-type": { "version": "27.4.0", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-27.4.0.tgz", diff --git a/package.json b/package.json index 661c33fe5f15..65bc6ae0ba24 100644 --- a/package.json +++ b/package.json @@ -140,6 +140,7 @@ "japanese-characters": "^1.1.0", "javascript-stringify": "^2.1.0", "jest": "^27.4.7", + "jest-fail-on-console": "^2.2.3", "jest-github-actions-reporter": "^1.0.3", "jest-slow-test-reporter": "^1.0.0", "linkinator": "^3.0.3",