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`.
This commit is contained in:
Aaron Dodson
2025-08-27 12:28:06 -07:00
committed by GitHub
parent 3d28ca8448
commit b2bbe965ba
5 changed files with 111 additions and 18 deletions

View File

@@ -15,9 +15,12 @@ const {posixPath} = require('../../scripts/helpers');
* Runs the Mocha tests in this directory in Chrome. It uses webdriverio to
* launch Chrome and load index.html. Outputs a summary of the test results
* to the console.
*
* @param {boolean} exitOnCompletetion True if the browser should automatically
* quit after tests have finished running.
* @return {number} 0 on success, 1 on failure.
*/
async function runMochaTestsInBrowser() {
async function runMochaTestsInBrowser(exitOnCompletion = true) {
const options = {
capabilities: {
browserName: 'chrome',
@@ -45,6 +48,17 @@ async function runMochaTestsInBrowser() {
console.log('Loading URL: ' + url);
await browser.url(url);
// Toggle the devtools setting to emulate focus, so that the window will
// always act as if it has focus regardless of the state of the window manager
// or operating system. This improves the reliability of FocusManager-related
// tests.
const puppeteer = await browser.getPuppeteer();
await browser.call(async () => {
const page = (await puppeteer.pages())[0];
const session = await page.createCDPSession();
await session.send('Emulation.setFocusEmulationEnabled', { enabled: true });
});
await browser.waitUntil(async() => {
const elem = await browser.$('#failureCount');
const text = await elem.getAttribute('tests_failed');
@@ -74,7 +88,7 @@ async function runMochaTestsInBrowser() {
if (parseInt(numOfFailure) !== 0) {
return 1;
}
await browser.deleteSession();
if (exitOnCompletion) await browser.deleteSession();
return 0;
}