From ccafb07a97c804eb79df16d961622ef320c7c649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Cie=C5=9Blak?= Date: Tue, 7 May 2024 11:11:22 +0200 Subject: [PATCH 1/2] Fail when JS test uses console.error or console.warn --- web/packages/build/package.json | 1 + web/packages/shared/setupTests.tsx | 39 ++++++++++++++++++++++++++++++ yarn.lock | 5 ++++ 3 files changed, 45 insertions(+) diff --git a/web/packages/build/package.json b/web/packages/build/package.json index 11dd7afc760af..7f1c56dfb1270 100644 --- a/web/packages/build/package.json +++ b/web/packages/build/package.json @@ -76,6 +76,7 @@ "html-webpack-plugin": "^5.5.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", + "jest-fail-on-console": "^3.2.0", "jest-styled-components": "^7.2.0", "jsdom": "^21.1.0", "jsdom-testing-mocks": "^1.9.0", diff --git a/web/packages/shared/setupTests.tsx b/web/packages/shared/setupTests.tsx index ac3236a9080a8..ee467b1855213 100644 --- a/web/packages/shared/setupTests.tsx +++ b/web/packages/shared/setupTests.tsx @@ -17,6 +17,20 @@ */ const crypt = require('crypto'); +const path = require('path'); + +const failOnConsole = require('jest-fail-on-console'); + +let entFailOnConsoleIgnoreList = []; +try { + entFailOnConsoleIgnoreList = require('../../../e/web/testsWithIgnoredConsole'); +} catch (err) { + // Ignore errors related to teleport.e not being present. This allows OSS users and OSS CI to run + // tests without teleport.e. + if (err['code'] !== 'MODULE_NOT_FOUND') { + throw err; + } +} Object.defineProperty(globalThis, 'crypto', { value: { @@ -29,3 +43,28 @@ global.ResizeObserver = jest.fn().mockImplementation(() => ({ unobserve: jest.fn(), disconnect: jest.fn(), })); + +const rootDir = path.join(__dirname, '..', '..', '..'); +// Do not add new paths to this list, instead fix the underlying problem which causes console.error +// or console.warn to be used. +// +// If the test is expected to use either of those console functions, follow the advice from the +// error message. +const failOnConsoleIgnoreList = new Set([ + 'web/packages/design/src/utils/match/matchers.test.ts', + 'web/packages/shared/components/TextEditor/TextEditor.test.tsx', + 'web/packages/teleport/src/components/BannerList/useAlerts.test.tsx', + 'web/packages/teleport/src/Navigation/NavigationItem.test.tsx', + 'web/packages/teleterm/src/ui/TabHost/TabHost.test.tsx', + // As of the parent commit (2189119fbd1), these two below are flakes. + // https://github.com/gravitational/teleport/pull/41252#discussion_r1595036569 + 'web/packages/teleport/src/Console/DocumentNodes/DocumentNodes.story.test.tsx', + 'web/packages/teleport/src/Recordings/Recordings.story.test.tsx', + ...entFailOnConsoleIgnoreList, +]); +failOnConsole({ + skipTest: ({ testPath }) => { + const relativeTestPath = path.relative(rootDir, testPath); + return failOnConsoleIgnoreList.has(relativeTestPath); + }, +}); diff --git a/yarn.lock b/yarn.lock index 365a3cb1ceb24..cdaadc304a0a6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10234,6 +10234,11 @@ jest-environment-node@^29.7.0: jest-mock "^29.7.0" jest-util "^29.7.0" +jest-fail-on-console@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/jest-fail-on-console/-/jest-fail-on-console-3.2.0.tgz#05fbf7d6084d04af7955aa8eb5a80fbc7cf46fa1" + integrity sha512-GSqvjURdT/U+yu9JEr3EUTEB4kZi9feXMWS/jXGXB/lsnXXHcdU9Xsm2tEupSdqSEtrhTjOCqEJseFMrQ1ryvg== + jest-get-type@^29.6.3: version "29.6.3" resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-29.6.3.tgz#36f499fdcea197c1045a127319c0481723908fd1" From e5e8f4b3904f72384f676fbb36984a8b7a71b039 Mon Sep 17 00:00:00 2001 From: Lisa Kim Date: Thu, 9 May 2024 13:06:05 -0700 Subject: [PATCH 2/2] Web: Add unto flakey test and throws console.error (#41390) --- web/packages/shared/setupTests.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/packages/shared/setupTests.tsx b/web/packages/shared/setupTests.tsx index ee467b1855213..3081eec5d2b5d 100644 --- a/web/packages/shared/setupTests.tsx +++ b/web/packages/shared/setupTests.tsx @@ -56,10 +56,11 @@ const failOnConsoleIgnoreList = new Set([ 'web/packages/teleport/src/components/BannerList/useAlerts.test.tsx', 'web/packages/teleport/src/Navigation/NavigationItem.test.tsx', 'web/packages/teleterm/src/ui/TabHost/TabHost.test.tsx', - // As of the parent commit (2189119fbd1), these two below are flakes. + // As of the parent commit (708dac8e0d0), the tests below are flakes. // https://github.com/gravitational/teleport/pull/41252#discussion_r1595036569 'web/packages/teleport/src/Console/DocumentNodes/DocumentNodes.story.test.tsx', 'web/packages/teleport/src/Recordings/Recordings.story.test.tsx', + 'web/packages/teleport/src/Audit/Audit.story.test.tsx', ...entFailOnConsoleIgnoreList, ]); failOnConsole({