import { BrowserMap } from "../browser-info" ; import SauceLabsAPI , { Job } from 'saucelabs' ; const REGION_MAPPING = { 'us' : '' , 'eu' : 'eu-central-1.' , } ; param region function getSauceEndpoint ( region ) { const shortRegion = REGION_MAPPING [ region ] ? region : 'us' ; return `https://app.${ REGION_MAPPING [ shortRegion ] }saucelabs.com/tests/` ; } /**
 * Karma browser reported that updates corresponding Saucelabs jobs whenever a given
 * browser finishes.
 */ export function SaucelabsReporter ( logger , browserMap ) { const log = logger . create ( 'reporter.sauce' ) ; let pendingUpdates = [ ] ; this . adapters = [ ] ; this . onSpecComplete = function ( browser , result ) { const browserId = browser . id ; const browserData = browserMap . get ( browserId ) ; if ( ! browserData ) { return ; } const status = result . success ? '✅' : result . skipped ? '➖' : '❌' ; browserData . results . push ( { status : 'info' , message : `${ status } ${ result . fullName || result . description }` , screenshot : null } ) ; if ( ! result . success && result . log . length > 0 ) { browserData . results . push ( { status : 'info' , message : `${ result . log [ 0 ] }` , screenshot : null } ) ; } } ; this . onBrowserComplete = function ( browser ) { const result = browser . lastResult ; const browserId = browser . id ; if ( result . disconnected ) { log . error ( '✖ Browser disconnected' ) ; } if ( result . error ) { log . error ( '✖ Tests errored' ) ; } const browserData = browserMap . get ( browserId ) ; if ( ! browserData ) { return ; } const { sessionId } = browserData ; const api = new SauceLabsAPI ( { user : browserData . username , key : browserData . accessKey , region : browserData . region , headless : browserData . headless } ) ; const hasPassed = ! result . failed && ! result . error && ! result . disconnected ; pendingUpdates . push ( api . updateJob ( browserData . username , sessionId , { id : sessionId , passed : hasPassed , 'custom-data' : result } ) ) ; log . info ( `Check out job at ${ getSauceEndpoint ( browserData . region ) }${ sessionId }` ) ; } ; this . onExit = ( doneFn ) => Promise . all ( pendingUpdates ) . then ( doneFn , ( error ) => { log . error ( 'Could not report results to Saucelabs: %s' , error ) ; doneFn ( ) ; } ) ; }