jquery/test/runner/jsdom.js
Timmy Willison 95a4c94b81
Tests: reuse browser workers in BrowserStack tests (#5428)
- reuse BrowserStack workers.
- add support for "latest" and "latest-1" in browser version filters
- add support for specifying non-final browser versions, such as beta versions
- more accurate eslint for files in test/runner
- switched `--no-isolate` command flag to `--isolate`. Now that browser instances are shared, it made more sense to me to default to no isolation unless specified. This turned out to be cleaner because the only place we isolate is in browserstack.yml.
- fixed an issue with retries where it wasn't always waiting for the retried test run
- enable strict mode in test yargs command
2024-03-05 14:44:01 -05:00

56 lines
1.2 KiB
JavaScript

import jsdom from "jsdom";
const { JSDOM } = jsdom;
const windows = Object.create( null );
export async function runJSDOM( url, { reportId, verbose } ) {
const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo( console );
virtualConsole.removeAllListeners( "clear" );
const { window } = await JSDOM.fromURL( url, {
resources: "usable",
runScripts: "dangerously",
virtualConsole
} );
if ( verbose ) {
console.log( "JSDOM window opened.", reportId );
}
windows[ reportId ] = window;
return new Promise( ( resolve ) => {
window.finish = resolve;
} );
}
export function cleanupJSDOM( reportId, { verbose } ) {
const window = windows[ reportId ];
if ( window ) {
if ( window.finish ) {
window.finish();
}
window.close();
delete windows[ reportId ];
if ( verbose ) {
console.log( "Closed JSDOM window.", reportId );
}
}
}
export function cleanupAllJSDOM( { verbose } ) {
const windowsRemaining = Object.keys( windows ).length;
if ( windowsRemaining ) {
if ( verbose ) {
console.log(
`Cleaning up ${ windowsRemaining } JSDOM window${
windowsRemaining > 1 ? "s" : ""
}...`
);
}
for ( const id in windows ) {
cleanupJSDOM( id, { verbose } );
}
}
}