diff options
Diffstat (limited to 'test/runner/lib')
-rw-r--r-- | test/runner/lib/buildTestUrl.js | 29 | ||||
-rw-r--r-- | test/runner/lib/generateHash.js | 50 | ||||
-rw-r--r-- | test/runner/lib/getBrowserString.js | 49 | ||||
-rw-r--r-- | test/runner/lib/prettyMs.js | 18 |
4 files changed, 146 insertions, 0 deletions
diff --git a/test/runner/lib/buildTestUrl.js b/test/runner/lib/buildTestUrl.js new file mode 100644 index 000000000..6e0f1a9b0 --- /dev/null +++ b/test/runner/lib/buildTestUrl.js @@ -0,0 +1,29 @@ +import { generateModuleId } from "./generateHash.js"; + +export function buildTestUrl( modules, { browserstack, esm, jsdom, port, reportId } ) { + if ( !port ) { + throw new Error( "No port specified." ); + } + + const query = new URLSearchParams(); + for ( const module of modules ) { + query.append( "moduleId", generateModuleId( module ) ); + } + + if ( esm ) { + query.append( "esmodules", "true" ); + } + + if ( jsdom ) { + query.append( "jsdom", "true" ); + } + + if ( reportId ) { + query.append( "reportId", reportId ); + } + + // BrowserStack supplies a custom domain for local testing, + // which is especially necessary for iOS testing. + const host = browserstack ? "bs-local.com" : "localhost"; + return `http://${ host }:${ port }/test/?${ query }`; +} diff --git a/test/runner/lib/generateHash.js b/test/runner/lib/generateHash.js new file mode 100644 index 000000000..dbbd4b476 --- /dev/null +++ b/test/runner/lib/generateHash.js @@ -0,0 +1,50 @@ +import crypto from "node:crypto"; + +export function generateHash( string ) { + const hash = crypto.createHash( "md5" ); + hash.update( string ); + + // QUnit hashes are 8 characters long + // We use 10 characters to be more visually distinct + return hash.digest( "hex" ).slice( 0, 10 ); +} + +/** + * A copy of the generate hash function from QUnit, + * used to generate a hash for the module name. + * + * QUnit errors on passing multiple modules to the + * module query parameter. We need to know + * the hash for each module before loading QUnit + * in order to pass multiple moduleId parameters instead. + */ +export function generateModuleId( module, browser ) { + + // QUnit normally hashes the test name, but + // we've repurposed this function to generate + // report IDs for module/browser combinations. + // We still use it without the browser parameter + // to get the same module IDs as QUnit to pass + // multiple ahead-of-time in the query string. + const str = module + "\x1C" + browser; + let hash = 0; + + for ( let i = 0; i < str.length; i++ ) { + hash = ( hash << 5 ) - hash + str.charCodeAt( i ); + hash |= 0; + } + + let hex = ( 0x100000000 + hash ).toString( 16 ); + if ( hex.length < 8 ) { + hex = "0000000" + hex; + } + + return hex.slice( -8 ); +} + +export function printModuleHashes( modules ) { + console.log( "Module hashes:" ); + modules.forEach( ( module ) => { + console.log( ` ${ module }: ${ generateModuleId( module ) }` ); + } ); +} diff --git a/test/runner/lib/getBrowserString.js b/test/runner/lib/getBrowserString.js new file mode 100644 index 000000000..413a60500 --- /dev/null +++ b/test/runner/lib/getBrowserString.js @@ -0,0 +1,49 @@ +const browserMap = { + chrome: "Chrome", + edge: "Edge", + firefox: "Firefox", + ie: "IE", + jsdom: "JSDOM", + opera: "Opera", + safari: "Safari" +}; + +export function browserSupportsHeadless( browser ) { + browser = browser.toLowerCase(); + return ( + browser === "chrome" || + browser === "firefox" || + browser === "edge" + ); +} + +export function getBrowserString( + { + browser, + browser_version: browserVersion, + device, + os, + os_version: osVersion + }, + headless +) { + browser = browser.toLowerCase(); + browser = browserMap[ browser ] || browser; + let str = browser; + if ( browserVersion ) { + str += ` ${ browserVersion }`; + } + if ( device ) { + str += ` for ${ device }`; + } + if ( os ) { + str += ` on ${ os }`; + } + if ( osVersion ) { + str += ` ${ osVersion }`; + } + if ( headless && browserSupportsHeadless( browser ) ) { + str += " (headless)"; + } + return str; +} diff --git a/test/runner/lib/prettyMs.js b/test/runner/lib/prettyMs.js new file mode 100644 index 000000000..99bae2b35 --- /dev/null +++ b/test/runner/lib/prettyMs.js @@ -0,0 +1,18 @@ +/** + * Pretty print a time in milliseconds. + */ +export function prettyMs( time ) { + const minutes = Math.floor( time / 60000 ); + const seconds = Math.floor( time / 1000 ); + const ms = Math.floor( time % 1000 ); + + let prettyTime = `${ ms }ms`; + if ( seconds > 0 ) { + prettyTime = `${ seconds }s ${ prettyTime }`; + } + if ( minutes > 0 ) { + prettyTime = `${ minutes }m ${ prettyTime }`; + } + + return prettyTime; +} |