Skip to content

Commit f1c2ba1

Browse files
authored
chore(e2e): Lint for restricted globals in e2e tests (#6600)
* Simplified tsconfigs * Add no-restricted-globals rule * Fix lint warnings
1 parent 1efea78 commit f1c2ba1

15 files changed

+91
-27
lines changed

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-e2e-tests/.eslintrc.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
'use strict';
2+
3+
const globals = require('globals');
4+
5+
const browserGlobals = Object.keys(globals.browser);
6+
const nodeGlobals = Object.keys(globals.node);
7+
const browserOnlyGlobals = browserGlobals.filter(
8+
(key) => !nodeGlobals.includes(key)
9+
);
10+
211
module.exports = {
312
root: true,
413
extends: ['@mongodb-js/eslint-config-compass'],
514
parserOptions: {
615
tsconfigRootDir: __dirname,
7-
project: ['./tsconfig-lint.json'],
16+
project: ['./tsconfig.json'],
817
},
918
overrides: [
1019
{
1120
files: ['**/*.ts'],
1221
rules: {
1322
'no-console': 0,
23+
'no-restricted-globals': ['error', ...browserOnlyGlobals],
24+
},
25+
},
26+
{
27+
// We need to access these in `browser.execute` calls
28+
files: ['tests/**/*.ts', 'helpers/**/*.ts'],
29+
rules: {
30+
'no-restricted-globals': ['warn', ...browserOnlyGlobals],
1431
},
1532
},
1633
],

packages/compass-e2e-tests/helpers/commands/codemirror.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ export async function getCodemirrorEditorText(
99
// we have to find an instance of the editor and get the text directly from
1010
// its state
1111
const editorContents = await browser.execute(function (selector) {
12-
const node =
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
13+
const node: any =
14+
// eslint-disable-next-line no-restricted-globals
1315
document.querySelector(`${selector} [data-codemirror]`) ??
16+
// eslint-disable-next-line no-restricted-globals
1417
document.querySelector(`${selector}[data-codemirror]`);
15-
return (node as any)._cm.state.sliceDoc() as string;
18+
return node._cm.state.sliceDoc() as string;
1619
}, selector);
1720
return editorContents;
1821
}
@@ -26,10 +29,12 @@ export async function getCodemirrorEditorTextAll(
2629
// its state
2730
const editorContents = await browser.execute(function (selector) {
2831
const editors = Array.from(
32+
// eslint-disable-next-line no-restricted-globals
2933
document.querySelectorAll(`${selector} [data-codemirror]`)
3034
);
31-
return editors.map((node) => {
32-
return (node as any)._cm.state.sliceDoc() as string;
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
36+
return editors.map((node: any) => {
37+
return node._cm.state.sliceDoc() as string;
3338
});
3439
}, selector);
3540
return editorContents;
@@ -42,10 +47,13 @@ export async function setCodemirrorEditorValue(
4247
) {
4348
await browser.execute(
4449
function (selector, text) {
45-
const node =
50+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Accessing private Codemirror state
51+
const node: any =
52+
// eslint-disable-next-line no-restricted-globals
4653
document.querySelector(`${selector} [data-codemirror]`) ??
54+
// eslint-disable-next-line no-restricted-globals
4755
document.querySelector(`${selector}[data-codemirror]`);
48-
const editor = (node as any)._cm;
56+
const editor = node._cm;
4957

5058
editor.dispatch({
5159
changes: {

packages/compass-e2e-tests/helpers/commands/scroll-to-virtual-item.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type ItemConfig = {
77
browser: CompassBrowser,
88
selector: string
99
) => Promise<boolean>;
10+
// eslint-disable-next-line no-restricted-globals
1011
getScrollContainer: (parent: Element | null) => ChildNode | null | undefined;
1112
};
1213

@@ -23,6 +24,7 @@ const gridConfig: ItemConfig = {
2324
const length = await browser.$$(`${selector} [role="row"]`).length;
2425
return !!(rowCount && length);
2526
},
27+
// eslint-disable-next-line no-restricted-globals
2628
getScrollContainer: (parent: Element | null) => {
2729
return parent?.firstChild;
2830
},
@@ -37,6 +39,7 @@ const treeConfig: ItemConfig = {
3739
) => {
3840
return (await browser.$$(`${selector} [role="treeitem"]`).length) > 0;
3941
},
42+
// eslint-disable-next-line no-restricted-globals
4043
getScrollContainer: (parent: Element | null) => {
4144
return parent?.firstChild?.firstChild;
4245
},
@@ -63,6 +66,7 @@ export async function scrollToVirtualItem(
6366
// scroll content
6467
const [scrollHeight, totalHeight] = await browser.execute(
6568
(selector, getScrollContainerString) => {
69+
// eslint-disable-next-line no-restricted-globals
6670
const container = document.querySelector(selector);
6771
const scrollContainer = eval(getScrollContainerString)(container);
6872
const heightContainer = scrollContainer?.firstChild;
@@ -115,6 +119,7 @@ export async function scrollToVirtualItem(
115119
// scroll for another screen
116120
await browser.execute(
117121
(selector, nextScrollTop, getScrollContainerString) => {
122+
// eslint-disable-next-line no-restricted-globals
118123
const container = document.querySelector(selector);
119124
const scrollContainer = eval(getScrollContainerString)(container);
120125
if (!scrollContainer) {

packages/compass-e2e-tests/helpers/commands/select-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function selectFile(
88
// HACK: the <input type="file"> is not displayed so we can't interact
99
// with it until we change that.
1010
await browser.execute((selector) => {
11-
// eslint-disable-next-line no-undef
11+
// eslint-disable-next-line no-restricted-globals
1212
const f = document.querySelector(selector);
1313
if (f) {
1414
f.removeAttribute('style');
@@ -24,7 +24,7 @@ export async function selectFile(
2424

2525
// HACK: undo what we just did
2626
await browser.execute((selector) => {
27-
// eslint-disable-next-line no-undef
27+
// eslint-disable-next-line no-restricted-globals
2828
const f = document.querySelector(selector);
2929
if (f) {
3030
f.setAttribute('style', 'display: none');

packages/compass-e2e-tests/helpers/commands/set-export-filename.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export async function setExportFilename(
1313
await expect(fs.stat(filename)).to.be.rejected;
1414

1515
await browser.execute(function (f) {
16+
// eslint-disable-next-line no-restricted-globals
1617
document.dispatchEvent(
1718
new CustomEvent('selectExportFileName', { detail: f })
1819
);

packages/compass-e2e-tests/helpers/compass-web-sandbox.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import assert from 'node:assert/strict';
2+
13
import crossSpawn from 'cross-spawn';
24
import { remote } from 'webdriverio';
35
import Debug from 'debug';
@@ -171,12 +173,16 @@ export async function spawnCompassWebSandboxAndSignInToAtlas(
171173
}
172174

173175
const res = settledRes.value;
176+
assert(
177+
res.ok,
178+
`Failed to authenticate in Atlas Cloud: ${res.statusText} (${res.status})`
179+
);
174180

175-
if (res.ok === false || !(await res.json()).projectId) {
176-
throw new Error(
177-
`Failed to authenticate in Atlas Cloud: ${res.statusText} (${res.status})`
178-
);
179-
}
181+
const body = await res.json();
182+
assert(
183+
typeof body === 'object' && body !== null && 'projectId' in body,
184+
'Expected a project id'
185+
);
180186

181187
if (signal.aborted) {
182188
return electronProxyRemote;

packages/compass-e2e-tests/helpers/compass.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ export class Compass {
395395

396396
async stopBrowser(): Promise<void> {
397397
const logging: any[] = await this.browser.execute(function () {
398-
return (window as any).logging;
398+
// eslint-disable-next-line no-restricted-globals
399+
return 'logging' in window && (window.logging as any);
399400
});
400401
const lines = logging.map((log) => JSON.stringify(log));
401402
const text = lines.join('\n');
@@ -1048,17 +1049,18 @@ export async function init(
10481049
// larger window for more consistent results
10491050
const [width, height] = await browser.execute(() => {
10501051
// in case setWindowSize() below doesn't work
1052+
// eslint-disable-next-line no-restricted-globals
10511053
window.resizeTo(window.screen.availWidth, window.screen.availHeight);
1052-
1054+
// eslint-disable-next-line no-restricted-globals
10531055
return [window.screen.availWidth, window.screen.availHeight];
10541056
});
10551057
// getting available width=1512, height=944 in electron on mac which is arbitrary
10561058
debug(`available width=${width}, height=${height}`);
10571059
try {
10581060
// window.resizeTo() doesn't work on firefox
10591061
await browser.setWindowSize(width, height);
1060-
} catch (err: any) {
1061-
console.error(err?.stack);
1062+
} catch (err) {
1063+
console.error(err instanceof Error ? err.stack : err);
10621064
}
10631065
} else {
10641066
await browser.execute(() => {

packages/compass-e2e-tests/helpers/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function startFakeTelemetry(): Promise<Telemetry> {
3636
},
3737
pollForEvents: async (browser: CompassBrowser): Promise<void> => {
3838
tracking = await browser.execute(function () {
39-
return (window as any).tracking;
39+
// eslint-disable-next-line no-restricted-globals
40+
return 'tracking' in window && (window.tracking as any);
4041
});
4142

4243
neverFetched = false;

packages/compass-e2e-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "E2E test suite for Compass app that follows smoke tests / feature testing matrix",
66
"scripts": {
77
"clean": "node -e \"try { fs.rmdirSync('.mongodb', { recursive: true }); } catch (e) {}\" && node -e \"try { fs.rmdirSync('.log', { recursive: true }); } catch (e) {}\"",
8-
"typecheck": "tsc -p tsconfig-lint.json --noEmit",
8+
"typecheck": "tsc -p tsconfig.json",
99
"eslint": "eslint",
1010
"prettier": "prettier",
1111
"lint": "npm run typecheck && npm run eslint . && npm run prettier -- --check .",
@@ -55,6 +55,7 @@
5555
"electron-to-chromium": "^1.5.76",
5656
"eslint": "^7.25.0",
5757
"glob": "^10.2.5",
58+
"globals": "^15.14.0",
5859
"hadron-build": "^25.6.1",
5960
"lodash": "^4.17.21",
6061
"mocha": "^10.2.0",

0 commit comments

Comments
 (0)