From 659aaa7b20054fa1888fbff06d9c684667c8ca70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 9 Jan 2025 12:17:23 +0100 Subject: [PATCH 1/3] Simplified tsconfigs --- .../helpers/compass-web-sandbox.ts | 16 +++++++++++----- packages/compass-e2e-tests/package.json | 2 +- packages/compass-e2e-tests/tsconfig-lint.json | 5 ----- packages/compass-e2e-tests/tsconfig.json | 5 ++++- 4 files changed, 16 insertions(+), 12 deletions(-) delete mode 100644 packages/compass-e2e-tests/tsconfig-lint.json diff --git a/packages/compass-e2e-tests/helpers/compass-web-sandbox.ts b/packages/compass-e2e-tests/helpers/compass-web-sandbox.ts index ad3c0e554e8..0019ec04482 100644 --- a/packages/compass-e2e-tests/helpers/compass-web-sandbox.ts +++ b/packages/compass-e2e-tests/helpers/compass-web-sandbox.ts @@ -1,3 +1,5 @@ +import assert from 'node:assert/strict'; + import crossSpawn from 'cross-spawn'; import { remote } from 'webdriverio'; import Debug from 'debug'; @@ -171,12 +173,16 @@ export async function spawnCompassWebSandboxAndSignInToAtlas( } const res = settledRes.value; + assert( + res.ok, + `Failed to authenticate in Atlas Cloud: ${res.statusText} (${res.status})` + ); - if (res.ok === false || !(await res.json()).projectId) { - throw new Error( - `Failed to authenticate in Atlas Cloud: ${res.statusText} (${res.status})` - ); - } + const body = await res.json(); + assert( + typeof body === 'object' && body !== null && 'projectId' in body, + 'Expected a project id' + ); if (signal.aborted) { return electronProxyRemote; diff --git a/packages/compass-e2e-tests/package.json b/packages/compass-e2e-tests/package.json index f9a16e4b7f9..fe2d5cc5f29 100644 --- a/packages/compass-e2e-tests/package.json +++ b/packages/compass-e2e-tests/package.json @@ -5,7 +5,7 @@ "description": "E2E test suite for Compass app that follows smoke tests / feature testing matrix", "scripts": { "clean": "node -e \"try { fs.rmdirSync('.mongodb', { recursive: true }); } catch (e) {}\" && node -e \"try { fs.rmdirSync('.log', { recursive: true }); } catch (e) {}\"", - "typecheck": "tsc -p tsconfig-lint.json --noEmit", + "typecheck": "tsc -p tsconfig.json", "eslint": "eslint", "prettier": "prettier", "lint": "npm run typecheck && npm run eslint . && npm run prettier -- --check .", diff --git a/packages/compass-e2e-tests/tsconfig-lint.json b/packages/compass-e2e-tests/tsconfig-lint.json deleted file mode 100644 index eda2ec11aa5..00000000000 --- a/packages/compass-e2e-tests/tsconfig-lint.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "extends": "./tsconfig.json", - "include": ["**/*"], - "exclude": ["node_modules", "lib"] -} diff --git a/packages/compass-e2e-tests/tsconfig.json b/packages/compass-e2e-tests/tsconfig.json index 8534724e776..bda9148d805 100644 --- a/packages/compass-e2e-tests/tsconfig.json +++ b/packages/compass-e2e-tests/tsconfig.json @@ -1,4 +1,7 @@ { "extends": "@mongodb-js/tsconfig-compass/tsconfig.common.json", - "compilerOptions": {} + "compilerOptions": { + "noEmit": true + }, + "include": ["helpers", "installers", "tests", "*.ts"] } From 122e035a8f1785d2a0952ac47a6385ff1c065969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 9 Jan 2025 12:18:02 +0100 Subject: [PATCH 2/3] Add no-restricted-globals rule --- package-lock.json | 21 +++++++++++++++++++++ packages/compass-e2e-tests/.eslintrc.js | 19 ++++++++++++++++++- packages/compass-e2e-tests/package.json | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index ca65c29d6b7..3935b28e867 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43285,6 +43285,7 @@ "electron-to-chromium": "^1.5.76", "eslint": "^7.25.0", "glob": "^10.2.5", + "globals": "^15.14.0", "hadron-build": "^25.6.1", "lodash": "^4.17.21", "mocha": "^10.2.0", @@ -43960,6 +43961,19 @@ "url": "https://github.com/sponsors/isaacs" } }, + "packages/compass-e2e-tests/node_modules/globals": { + "version": "15.14.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.14.0.tgz", + "integrity": "sha512-OkToC372DtlQeje9/zHIo5CT8lRP/FUgEOKBEhU4e0abL7J7CD24fD9ohiLN5hagG/kWCYj4K5oaxxtj2Z0Dig==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "packages/compass-e2e-tests/node_modules/htmlparser2": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-9.1.0.tgz", @@ -66388,6 +66402,7 @@ "electron-to-chromium": "^1.5.76", "eslint": "^7.25.0", "glob": "^10.2.5", + "globals": "^15.14.0", "hadron-build": "^25.6.1", "lodash": "^4.17.21", "mocha": "^10.2.0", @@ -66855,6 +66870,12 @@ "path-scurry": "^1.7.0" } }, + "globals": { + "version": "15.14.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-15.14.0.tgz", + "integrity": "sha512-OkToC372DtlQeje9/zHIo5CT8lRP/FUgEOKBEhU4e0abL7J7CD24fD9ohiLN5hagG/kWCYj4K5oaxxtj2Z0Dig==", + "dev": true + }, "htmlparser2": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-9.1.0.tgz", diff --git a/packages/compass-e2e-tests/.eslintrc.js b/packages/compass-e2e-tests/.eslintrc.js index 3eb1f8557c6..010d278cd28 100644 --- a/packages/compass-e2e-tests/.eslintrc.js +++ b/packages/compass-e2e-tests/.eslintrc.js @@ -1,16 +1,33 @@ 'use strict'; + +const globals = require('globals'); + +const browserGlobals = Object.keys(globals.browser); +const nodeGlobals = Object.keys(globals.node); +const browserOnlyGlobals = browserGlobals.filter( + (key) => !nodeGlobals.includes(key) +); + module.exports = { root: true, extends: ['@mongodb-js/eslint-config-compass'], parserOptions: { tsconfigRootDir: __dirname, - project: ['./tsconfig-lint.json'], + project: ['./tsconfig.json'], }, overrides: [ { files: ['**/*.ts'], rules: { 'no-console': 0, + 'no-restricted-globals': ['error', ...browserOnlyGlobals], + }, + }, + { + // We need to access these in `browser.execute` calls + files: ['tests/**/*.ts', 'helpers/**/*.ts'], + rules: { + 'no-restricted-globals': ['warn', ...browserOnlyGlobals], }, }, ], diff --git a/packages/compass-e2e-tests/package.json b/packages/compass-e2e-tests/package.json index fe2d5cc5f29..f5dd8e289a5 100644 --- a/packages/compass-e2e-tests/package.json +++ b/packages/compass-e2e-tests/package.json @@ -55,6 +55,7 @@ "electron-to-chromium": "^1.5.76", "eslint": "^7.25.0", "glob": "^10.2.5", + "globals": "^15.14.0", "hadron-build": "^25.6.1", "lodash": "^4.17.21", "mocha": "^10.2.0", From 0825c79d49647c21e4af2a08c6d7d895ac2a2b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 9 Jan 2025 12:18:11 +0100 Subject: [PATCH 3/3] Fix lint warnings --- .../helpers/commands/codemirror.ts | 20 +++++++++++++------ .../commands/scroll-to-virtual-item.ts | 5 +++++ .../helpers/commands/select-file.ts | 4 ++-- .../helpers/commands/set-export-filename.ts | 1 + packages/compass-e2e-tests/helpers/compass.ts | 10 ++++++---- .../compass-e2e-tests/helpers/telemetry.ts | 3 ++- .../tests/auto-connect.test.ts | 2 ++ .../tests/collection-aggregations-tab.test.ts | 1 + .../compass-e2e-tests/tests/intercom.test.ts | 3 ++- 9 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/compass-e2e-tests/helpers/commands/codemirror.ts b/packages/compass-e2e-tests/helpers/commands/codemirror.ts index 80fc05e90d1..30dc8ed775d 100644 --- a/packages/compass-e2e-tests/helpers/commands/codemirror.ts +++ b/packages/compass-e2e-tests/helpers/commands/codemirror.ts @@ -9,10 +9,13 @@ export async function getCodemirrorEditorText( // we have to find an instance of the editor and get the text directly from // its state const editorContents = await browser.execute(function (selector) { - const node = + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state + const node: any = + // eslint-disable-next-line no-restricted-globals document.querySelector(`${selector} [data-codemirror]`) ?? + // eslint-disable-next-line no-restricted-globals document.querySelector(`${selector}[data-codemirror]`); - return (node as any)._cm.state.sliceDoc() as string; + return node._cm.state.sliceDoc() as string; }, selector); return editorContents; } @@ -26,10 +29,12 @@ export async function getCodemirrorEditorTextAll( // its state const editorContents = await browser.execute(function (selector) { const editors = Array.from( + // eslint-disable-next-line no-restricted-globals document.querySelectorAll(`${selector} [data-codemirror]`) ); - return editors.map((node) => { - return (node as any)._cm.state.sliceDoc() as string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state + return editors.map((node: any) => { + return node._cm.state.sliceDoc() as string; }); }, selector); return editorContents; @@ -42,10 +47,13 @@ export async function setCodemirrorEditorValue( ) { await browser.execute( function (selector, text) { - const node = + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state + const node: any = + // eslint-disable-next-line no-restricted-globals document.querySelector(`${selector} [data-codemirror]`) ?? + // eslint-disable-next-line no-restricted-globals document.querySelector(`${selector}[data-codemirror]`); - const editor = (node as any)._cm; + const editor = node._cm; editor.dispatch({ changes: { diff --git a/packages/compass-e2e-tests/helpers/commands/scroll-to-virtual-item.ts b/packages/compass-e2e-tests/helpers/commands/scroll-to-virtual-item.ts index 72d3cfb1fc9..2d0f1c746da 100644 --- a/packages/compass-e2e-tests/helpers/commands/scroll-to-virtual-item.ts +++ b/packages/compass-e2e-tests/helpers/commands/scroll-to-virtual-item.ts @@ -7,6 +7,7 @@ type ItemConfig = { browser: CompassBrowser, selector: string ) => Promise; + // eslint-disable-next-line no-restricted-globals getScrollContainer: (parent: Element | null) => ChildNode | null | undefined; }; @@ -23,6 +24,7 @@ const gridConfig: ItemConfig = { const length = await browser.$$(`${selector} [role="row"]`).length; return !!(rowCount && length); }, + // eslint-disable-next-line no-restricted-globals getScrollContainer: (parent: Element | null) => { return parent?.firstChild; }, @@ -37,6 +39,7 @@ const treeConfig: ItemConfig = { ) => { return (await browser.$$(`${selector} [role="treeitem"]`).length) > 0; }, + // eslint-disable-next-line no-restricted-globals getScrollContainer: (parent: Element | null) => { return parent?.firstChild?.firstChild; }, @@ -63,6 +66,7 @@ export async function scrollToVirtualItem( // scroll content const [scrollHeight, totalHeight] = await browser.execute( (selector, getScrollContainerString) => { + // eslint-disable-next-line no-restricted-globals const container = document.querySelector(selector); const scrollContainer = eval(getScrollContainerString)(container); const heightContainer = scrollContainer?.firstChild; @@ -115,6 +119,7 @@ export async function scrollToVirtualItem( // scroll for another screen await browser.execute( (selector, nextScrollTop, getScrollContainerString) => { + // eslint-disable-next-line no-restricted-globals const container = document.querySelector(selector); const scrollContainer = eval(getScrollContainerString)(container); if (!scrollContainer) { diff --git a/packages/compass-e2e-tests/helpers/commands/select-file.ts b/packages/compass-e2e-tests/helpers/commands/select-file.ts index d0800c864a0..b67f830a709 100644 --- a/packages/compass-e2e-tests/helpers/commands/select-file.ts +++ b/packages/compass-e2e-tests/helpers/commands/select-file.ts @@ -8,7 +8,7 @@ export async function selectFile( // HACK: the is not displayed so we can't interact // with it until we change that. await browser.execute((selector) => { - // eslint-disable-next-line no-undef + // eslint-disable-next-line no-restricted-globals const f = document.querySelector(selector); if (f) { f.removeAttribute('style'); @@ -24,7 +24,7 @@ export async function selectFile( // HACK: undo what we just did await browser.execute((selector) => { - // eslint-disable-next-line no-undef + // eslint-disable-next-line no-restricted-globals const f = document.querySelector(selector); if (f) { f.setAttribute('style', 'display: none'); diff --git a/packages/compass-e2e-tests/helpers/commands/set-export-filename.ts b/packages/compass-e2e-tests/helpers/commands/set-export-filename.ts index 4f08e644a6b..eb3d95c1ba7 100644 --- a/packages/compass-e2e-tests/helpers/commands/set-export-filename.ts +++ b/packages/compass-e2e-tests/helpers/commands/set-export-filename.ts @@ -13,6 +13,7 @@ export async function setExportFilename( await expect(fs.stat(filename)).to.be.rejected; await browser.execute(function (f) { + // eslint-disable-next-line no-restricted-globals document.dispatchEvent( new CustomEvent('selectExportFileName', { detail: f }) ); diff --git a/packages/compass-e2e-tests/helpers/compass.ts b/packages/compass-e2e-tests/helpers/compass.ts index 0d414dea8ba..923fdbd0382 100644 --- a/packages/compass-e2e-tests/helpers/compass.ts +++ b/packages/compass-e2e-tests/helpers/compass.ts @@ -395,7 +395,8 @@ export class Compass { async stopBrowser(): Promise { const logging: any[] = await this.browser.execute(function () { - return (window as any).logging; + // eslint-disable-next-line no-restricted-globals + return 'logging' in window && (window.logging as any); }); const lines = logging.map((log) => JSON.stringify(log)); const text = lines.join('\n'); @@ -1048,8 +1049,9 @@ export async function init( // larger window for more consistent results const [width, height] = await browser.execute(() => { // in case setWindowSize() below doesn't work + // eslint-disable-next-line no-restricted-globals window.resizeTo(window.screen.availWidth, window.screen.availHeight); - + // eslint-disable-next-line no-restricted-globals return [window.screen.availWidth, window.screen.availHeight]; }); // getting available width=1512, height=944 in electron on mac which is arbitrary @@ -1057,8 +1059,8 @@ export async function init( try { // window.resizeTo() doesn't work on firefox await browser.setWindowSize(width, height); - } catch (err: any) { - console.error(err?.stack); + } catch (err) { + console.error(err instanceof Error ? err.stack : err); } } else { await browser.execute(() => { diff --git a/packages/compass-e2e-tests/helpers/telemetry.ts b/packages/compass-e2e-tests/helpers/telemetry.ts index 1eaa126d2c2..b6b9198fdad 100644 --- a/packages/compass-e2e-tests/helpers/telemetry.ts +++ b/packages/compass-e2e-tests/helpers/telemetry.ts @@ -36,7 +36,8 @@ function startFakeTelemetry(): Promise { }, pollForEvents: async (browser: CompassBrowser): Promise => { tracking = await browser.execute(function () { - return (window as any).tracking; + // eslint-disable-next-line no-restricted-globals + return 'tracking' in window && (window.tracking as any); }); neverFetched = false; diff --git a/packages/compass-e2e-tests/tests/auto-connect.test.ts b/packages/compass-e2e-tests/tests/auto-connect.test.ts index d38ed9b41df..b599ac819a0 100644 --- a/packages/compass-e2e-tests/tests/auto-connect.test.ts +++ b/packages/compass-e2e-tests/tests/auto-connect.test.ts @@ -231,6 +231,7 @@ describe('Automatically connecting from the command line', function () { } ); await browser.execute(() => { + // eslint-disable-next-line no-restricted-globals location.reload(); }); await browser.waitForConnectionResult( @@ -241,6 +242,7 @@ describe('Automatically connecting from the command line', function () { ); await browser.disconnectAll(); await browser.execute(() => { + // eslint-disable-next-line no-restricted-globals location.reload(); }); } catch (err: any) { diff --git a/packages/compass-e2e-tests/tests/collection-aggregations-tab.test.ts b/packages/compass-e2e-tests/tests/collection-aggregations-tab.test.ts index 46ad52f3575..fd325a63cdc 100644 --- a/packages/compass-e2e-tests/tests/collection-aggregations-tab.test.ts +++ b/packages/compass-e2e-tests/tests/collection-aggregations-tab.test.ts @@ -124,6 +124,7 @@ describe('Collection aggregations tab', function () { await browser.connectToDefaults(); // set guide cue to not show up await browser.execute((key) => { + // eslint-disable-next-line no-restricted-globals localStorage.setItem(key, 'true'); }, STAGE_WIZARD_GUIDE_CUE_STORAGE_KEY); diff --git a/packages/compass-e2e-tests/tests/intercom.test.ts b/packages/compass-e2e-tests/tests/intercom.test.ts index 3ff9a4b7178..e125fcc2e12 100644 --- a/packages/compass-e2e-tests/tests/intercom.test.ts +++ b/packages/compass-e2e-tests/tests/intercom.test.ts @@ -44,7 +44,8 @@ describe('Intercom integration', function () { await compass.browser.waitUntil( () => { return compass.browser.execute(() => { - return typeof (window as any).Intercom === 'function'; + // eslint-disable-next-line no-restricted-globals + return 'Intercom' in window && typeof window.Intercom === 'function'; }); }, {