import * as glob from 'glob' ; import * as Mocha from 'mocha' ; import * as path from 'path' ; export async function run ( ) { const options = { ui : 'tdd' , color : true , reporter : 'mocha-multi-reporters' , reporterOptions : { reporterEnabled : 'spec, mocha-junit-reporter' , mochaJunitReporterReporterOptions : { mochaFile : path . join ( __dirname , '..' , '..' , 'test-results.xml' ) } } } ; addEnvVarsToMochaOptions ( options ) ; console . log ( `Mocha options: ${ JSON . stringify ( options , undefined , 2 ) }` ) ; const mocha = new Mocha ( options ) ; const files = await new Promise ( ( resolve , reject ) => { glob ( '**/**.test.js' , { cwd : __dirname } , ( err , result ) => { err ? reject ( err ) : resolve ( result ) ; } ) ; } ) ; files . forEach ( f => mocha . addFile ( path . resolve ( __dirname , f ) ) ) ; const failures = await new Promise < number > ( resolve => mocha . run ( resolve ) ) ; if ( failures > 0 ) { throw new Error ( `${ failures } tests failed.` ) ; } } function addEnvVarsToMochaOptions ( options ) { for ( const envVar of Object . keys ( process . env ) ) { const match = envVar . match ( /^mocha_(.+)/i ) ; if ( match ) { const [ , option ] = match ; let value = process . env [ envVar ] || '' ; if ( typeof value === 'string' && ! isNaN ( parseInt ( value ) ) ) { value = parseInt ( value ) ; } ( < any > options ) [ option ] = value ; } } }