Skip to content

Commit

Permalink
chore(e2e): Lint for restricted globals in e2e tests (#6600)
Browse files Browse the repository at this point in the history
* Simplified tsconfigs

* Add no-restricted-globals rule

* Fix lint warnings
  • Loading branch information
kraenhansen authored Jan 13, 2025
1 parent 1efea78 commit f1c2ba1
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 27 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion packages/compass-e2e-tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -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],
},
},
],
Expand Down
20 changes: 14 additions & 6 deletions packages/compass-e2e-tests/helpers/commands/codemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ItemConfig = {
browser: CompassBrowser,
selector: string
) => Promise<boolean>;
// eslint-disable-next-line no-restricted-globals
getScrollContainer: (parent: Element | null) => ChildNode | null | undefined;
};

Expand All @@ -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;
},
Expand All @@ -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;
},
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/compass-e2e-tests/helpers/commands/select-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function selectFile(
// HACK: the <input type="file"> 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');
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
);
Expand Down
16 changes: 11 additions & 5 deletions packages/compass-e2e-tests/helpers/compass-web-sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from 'node:assert/strict';

import crossSpawn from 'cross-spawn';
import { remote } from 'webdriverio';
import Debug from 'debug';
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions packages/compass-e2e-tests/helpers/compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ export class Compass {

async stopBrowser(): Promise<void> {
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');
Expand Down Expand Up @@ -1048,17 +1049,18 @@ 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
debug(`available width=${width}, height=${height}`);
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(() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/compass-e2e-tests/helpers/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function startFakeTelemetry(): Promise<Telemetry> {
},
pollForEvents: async (browser: CompassBrowser): Promise<void> => {
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;
Expand Down
3 changes: 2 additions & 1 deletion packages/compass-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-e2e-tests/tests/auto-connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion packages/compass-e2e-tests/tests/intercom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
},
{
Expand Down
5 changes: 0 additions & 5 deletions packages/compass-e2e-tests/tsconfig-lint.json

This file was deleted.

5 changes: 4 additions & 1 deletion packages/compass-e2e-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "@mongodb-js/tsconfig-compass/tsconfig.common.json",
"compilerOptions": {}
"compilerOptions": {
"noEmit": true
},
"include": ["helpers", "installers", "tests", "*.ts"]
}

0 comments on commit f1c2ba1

Please sign in to comment.