import * as path from 'path' ; import * as process from 'process' ; import * as Mocha from 'mocha' ; import * as glob from 'glob' ; function setupCoverage ( ) { const NYC = require ( 'nyc' ) ; const nyc = new NYC ( { cwd : path . join ( __dirname , '..' , '..' , '..' ) , exclude : [ '**/test/**' , '.vscode-test/**' ] , reporter : [ 'lcov' ] , all : true , instrument : true , hookRequire : true , hookRunInContext : true , hookRunInThisContext : true } ) ; nyc . reset ( ) ; nyc . wrap ( ) ; return nyc ; } export async function run ( ) { const nyc = process . env . COVERAGE ? setupCoverage ( ) : null ; const mocha = new Mocha ( { ui : 'tdd' , color : true , timeout : 10 * 1000 } ) ; const testsRoot = path . resolve ( __dirname , '..' ) ; const files = glob . sync ( '**/*.test.js' , { cwd : testsRoot } ) ; for ( const file of files ) { mocha . addFile ( path . resolve ( testsRoot , file ) ) ; } try { await new Promise ( ( resolve , reject ) => { mocha . run ( ( failures ) => { failures ? reject ( new Error ( `${ failures } tests failed` ) ) : resolve ( undefined ) ; } ) ; } ) ; } finally { if ( nyc ) { nyc . writeCoverageFile ( ) ; await nyc . report ( ) ; } } }