Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v15] Fail when JS test uses console.error or console.warn #41367

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions web/packages/shared/setupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -29,3 +43,29 @@ 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 (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({
skipTest: ({ testPath }) => {
const relativeTestPath = path.relative(rootDir, testPath);
return failOnConsoleIgnoreList.has(relativeTestPath);
},
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading