Skip to content

Commit b2bbe96

Browse files
authored
fix: Prevent mocha tests failures when window does not have focus. (#9332)
* chore: Add puppeteer-core as a dev dependency. * fix: Make mocha tests run in a fake-focused window. * fix: Make `test:mocha:interactive` use the same gulp codepath as `test`.
1 parent 3d28ca8 commit b2bbe96

File tree

5 files changed

+111
-18
lines changed

5 files changed

+111
-18
lines changed

gulpfile.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ import {
4545
publishBeta,
4646
recompile,
4747
} from './scripts/gulpfiles/release_tasks.mjs';
48-
import {generators, test} from './scripts/gulpfiles/test_tasks.mjs';
48+
import {
49+
generators,
50+
interactiveMocha,
51+
test,
52+
} from './scripts/gulpfiles/test_tasks.mjs';
4953

5054
const clean = parallel(cleanBuildDir, cleanReleaseDir);
5155

@@ -80,6 +84,7 @@ export {
8084
clean,
8185
test,
8286
generators as testGenerators,
87+
interactiveMocha,
8388
buildAdvancedCompilationTest,
8489
createRC as gitCreateRC,
8590
docs,

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"test": "gulp test",
4646
"test:browser": "cd tests/browser && npx mocha",
4747
"test:generators": "gulp testGenerators",
48-
"test:mocha:interactive": "npm run build && concurrently -n tsc,server \"tsc --watch --preserveWatchOutput --outDir \"build/src\" --declarationDir \"build/declarations\"\" \"http-server ./ -o /tests/mocha/index.html -c-1\"",
48+
"test:mocha:interactive": "npm run build && concurrently -n tsc,server \"tsc --watch --preserveWatchOutput --outDir \"build/src\" --declarationDir \"build/declarations\"\" \"gulp interactiveMocha\"",
4949
"test:compile:advanced": "gulp buildAdvancedCompilationTest --debug",
5050
"updateGithubPages": "npm ci && gulp gitUpdateGithubPages"
5151
},
@@ -138,6 +138,7 @@
138138
"patch-package": "^8.0.0",
139139
"prettier": "^3.3.3",
140140
"prettier-plugin-organize-imports": "^4.0.0",
141+
"puppeteer-core": "^24.17.0",
141142
"readline-sync": "^1.4.10",
142143
"rimraf": "^5.0.0",
143144
"typescript": "^5.3.3",

scripts/gulpfiles/test_tasks.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ async function metadata() {
257257
* Run Mocha tests inside a browser.
258258
* @return {Promise} Asynchronous result.
259259
*/
260-
async function mocha() {
260+
async function mocha(exitOnCompletion = true) {
261261
return runTestTask('mocha', async () => {
262-
const result = await runMochaTestsInBrowser().catch(e => {
262+
const result = await runMochaTestsInBrowser(exitOnCompletion).catch(e => {
263263
throw e;
264264
});
265265
if (result) {
@@ -269,6 +269,14 @@ async function mocha() {
269269
});
270270
}
271271

272+
/**
273+
* Run Mocha tests inside a browser and keep the browser open upon completion.
274+
* @return {Promise} Asynchronous result.
275+
*/
276+
export async function interactiveMocha() {
277+
return mocha(false);
278+
}
279+
272280
/**
273281
* Helper method for comparison file.
274282
* @param {string} file1 First target file.

tests/mocha/webdriver.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ const {posixPath} = require('../../scripts/helpers');
1515
* Runs the Mocha tests in this directory in Chrome. It uses webdriverio to
1616
* launch Chrome and load index.html. Outputs a summary of the test results
1717
* to the console.
18+
*
19+
* @param {boolean} exitOnCompletetion True if the browser should automatically
20+
* quit after tests have finished running.
1821
* @return {number} 0 on success, 1 on failure.
1922
*/
20-
async function runMochaTestsInBrowser() {
23+
async function runMochaTestsInBrowser(exitOnCompletion = true) {
2124
const options = {
2225
capabilities: {
2326
browserName: 'chrome',
@@ -45,6 +48,17 @@ async function runMochaTestsInBrowser() {
4548
console.log('Loading URL: ' + url);
4649
await browser.url(url);
4750

51+
// Toggle the devtools setting to emulate focus, so that the window will
52+
// always act as if it has focus regardless of the state of the window manager
53+
// or operating system. This improves the reliability of FocusManager-related
54+
// tests.
55+
const puppeteer = await browser.getPuppeteer();
56+
await browser.call(async () => {
57+
const page = (await puppeteer.pages())[0];
58+
const session = await page.createCDPSession();
59+
await session.send('Emulation.setFocusEmulationEnabled', { enabled: true });
60+
});
61+
4862
await browser.waitUntil(async() => {
4963
const elem = await browser.$('#failureCount');
5064
const text = await elem.getAttribute('tests_failed');
@@ -74,7 +88,7 @@ async function runMochaTestsInBrowser() {
7488
if (parseInt(numOfFailure) !== 0) {
7589
return 1;
7690
}
77-
await browser.deleteSession();
91+
if (exitOnCompletion) await browser.deleteSession();
7892
return 0;
7993
}
8094

0 commit comments

Comments
 (0)