11import * as path from "path" ;
22import * as Mocha from "mocha" ;
3- import * as glob from "glob" ;
3+ import { glob } from "glob" ;
44
55import { TIMEOUT_MS } from "./setup" ;
66
7- export function run ( ) : Promise < void > {
7+ export async function run ( ) : Promise < void > {
88 const mocha = new Mocha ( {
99 asyncOnly : true ,
1010 color : true ,
@@ -16,33 +16,32 @@ export function run(): Promise<void> {
1616
1717 const testsRoot = path . resolve ( __dirname , ".." ) ;
1818
19- return new Promise ( ( c , e ) => {
20- glob ( "**/**.test.js" , { cwd : testsRoot } , ( err , files ) => {
21- if ( err ) {
22- return e ( err ) ;
23- }
19+ try {
20+ // Find all test files
21+ const files = await glob ( "**/**.test.js" , { cwd : testsRoot } ) ;
2422
25- // Add files to the test suite
26- files . forEach ( f => mocha . addFile ( path . resolve ( testsRoot , f ) ) ) ;
23+ // Add files to the test suite
24+ files . forEach ( f => mocha . addFile ( path . resolve ( testsRoot , f ) ) ) ;
2725
28- try {
29- // Run the mocha test
30- mocha . run ( failures => {
31- if ( failures > 0 ) {
32- // Let the cameras roll for a bit & make sure we capture the error
33- if ( process . env . CI ) {
34- setTimeout ( ( ) => e ( new Error ( `${ failures } tests failed; pausing for dramatic effect.` ) ) , 3000 ) ;
35- } else {
36- e ( new Error ( `${ failures } tests failed.` ) ) ;
37- }
26+ // Run the mocha test and wrap in a promise since mocha.run uses callbacks
27+ await new Promise < void > ( ( resolve , reject ) => {
28+ mocha . run ( failures => {
29+ if ( failures > 0 ) {
30+ const error = new Error ( `${ failures } tests failed${ process . env . CI ? '; pausing for dramatic effect.' : '.' } ` ) ;
31+
32+ // Let the cameras roll for a bit & make sure we capture the error
33+ if ( process . env . CI ) {
34+ setTimeout ( ( ) => reject ( error ) , 3000 ) ;
3835 } else {
39- c ( ) ;
36+ reject ( error ) ;
4037 }
41- } ) ;
42- } catch ( err ) {
43- console . error ( err ) ;
44- e ( err ) ;
45- }
38+ } else {
39+ resolve ( ) ;
40+ }
41+ } ) ;
4642 } ) ;
47- } ) ;
43+ } catch ( err ) {
44+ console . error ( err ) ;
45+ throw err ;
46+ }
4847}
0 commit comments