@@ -108,12 +108,10 @@ export const serverSatisfies = (
108108let i = 0 ;
109109// For the screenshots
110110let j = 0 ;
111- // For the coverage
112- let k = 0 ;
113111
114112interface Coverage {
115- main : string ;
116- renderer : string ;
113+ main ? : string ;
114+ renderer ? : string ;
117115}
118116
119117interface RenderLogEntry {
@@ -124,6 +122,7 @@ interface RenderLogEntry {
124122}
125123
126124export class Compass {
125+ name : string ;
127126 browser : CompassBrowser ;
128127 testPackagedApp : boolean ;
129128 needsCloseWelcomeModal : boolean ;
@@ -134,9 +133,11 @@ export class Compass {
134133 appName ?: string ;
135134
136135 constructor (
136+ name : string ,
137137 browser : CompassBrowser ,
138138 { testPackagedApp = false , needsCloseWelcomeModal = false } = { }
139139 ) {
140+ this . name = name ;
140141 this . browser = browser ;
141142 this . testPackagedApp = testPackagedApp ;
142143 this . needsCloseWelcomeModal = needsCloseWelcomeModal ;
@@ -319,30 +320,21 @@ export class Compass {
319320 }
320321 }
321322
322- async stop ( test ?: Mocha . Hook | Mocha . Test , step ?: string ) : Promise < void > {
323+ async stop ( ) : Promise < void > {
323324 // TODO: we don't have main logs to write :(
324325 /*
325326 const mainLogs = [];
326327 const mainLogPath = path.join(
327328 LOG_PATH,
328- `electron-main.${nowFormatted }.log`
329+ `electron-main.${name }.log`
329330 );
330331 debug(`Writing application main process log to ${mainLogPath}`);
331332 await fs.writeFile(mainLogPath, mainLogs.join('\n'));
332333 */
333334
334- const nowFormatted = formattedDate ( ) ;
335-
336- // name the log files after the closest test if possible to make it easier to find
337- let name = test ? pathName ( test . fullTitle ( ) ) : nowFormatted ;
338-
339- if ( step ) {
340- name = `${ name } -${ step } ` ;
341- }
342-
343335 const renderLogPath = path . join (
344336 LOG_PATH ,
345- `electron-render.${ nowFormatted } .json`
337+ `electron-render.${ this . name } .json`
346338 ) ;
347339 debug ( `Writing application render process log to ${ renderLogPath } ` ) ;
348340 await fs . writeFile ( renderLogPath , JSON . stringify ( this . renderLogs , null , 2 ) ) ;
@@ -362,22 +354,25 @@ export class Compass {
362354 } ) ;
363355 } ) ( ) ;
364356 } ) ;
365- const stopIndex = ++ k ;
366- await fs . writeFile (
367- path . join ( COVERAGE_PATH , `main.${ stopIndex } .log` ) ,
368- coverage . main
369- ) ;
370- await fs . writeFile (
371- path . join ( COVERAGE_PATH , `renderer.${ stopIndex } .log` ) ,
372- coverage . renderer
373- ) ;
357+ if ( coverage . main ) {
358+ await fs . writeFile (
359+ path . join ( COVERAGE_PATH , `main.${ this . name } .log` ) ,
360+ coverage . main
361+ ) ;
362+ }
363+ if ( coverage . renderer ) {
364+ await fs . writeFile (
365+ path . join ( COVERAGE_PATH , `renderer.${ this . name } .log` ) ,
366+ coverage . renderer
367+ ) ;
368+ }
374369 }
375370
376371 debug ( 'Stopping Compass application' ) ;
377372 await this . browser . deleteSession ( ) ;
378373
379374 const compassLog = await getCompassLog ( this . logPath ?? '' ) ;
380- const compassLogPath = path . join ( LOG_PATH , `compass-log.${ name } .log` ) ;
375+ const compassLogPath = path . join ( LOG_PATH , `compass-log.${ this . name } .log` ) ;
381376 debug ( `Writing Compass application log to ${ compassLogPath } ` ) ;
382377 await fs . writeFile ( compassLogPath , compassLog . raw ) ;
383378 this . logs = compassLog . structured ;
@@ -491,7 +486,10 @@ export async function runCompassOnce(args: string[], timeout = 30_000) {
491486 return { stdout, stderr } ;
492487}
493488
494- async function startCompass ( opts : StartCompassOptions = { } ) : Promise < Compass > {
489+ async function startCompass (
490+ name : string ,
491+ opts : StartCompassOptions = { }
492+ ) : Promise < Compass > {
495493 const { testPackagedApp, binary } = await getCompassExecutionParameters ( ) ;
496494 const nowFormatted = formattedDate ( ) ;
497495 let needsCloseWelcomeModal : boolean ;
@@ -710,7 +708,7 @@ async function startCompass(opts: StartCompassOptions = {}): Promise<Compass> {
710708 throw err ;
711709 }
712710
713- const compass = new Compass ( browser , {
711+ const compass = new Compass ( name , browser , {
714712 testPackagedApp,
715713 needsCloseWelcomeModal,
716714 } ) ;
@@ -906,10 +904,15 @@ function augmentError(error: Error, stack: string) {
906904 error . stack = `${ error . stack ?? '' } \nvia ${ strippedLines . join ( '\n' ) } ` ;
907905}
908906
909- export async function beforeTests (
907+ export async function init (
908+ name ?: string ,
910909 opts : StartCompassOptions = { }
911910) : Promise < Compass > {
912- const compass = await startCompass ( opts ) ;
911+ // Unfortunately mocha's type is that this.test inside a test or hook is
912+ // optional even though it always exists. So we have a lot of
913+ // this.test?.fullTitle() and therefore we hopefully won't end up with a lot
914+ // of dates in filenames in reality.
915+ const compass = await startCompass ( pathName ( name ?? formattedDate ( ) ) , opts ) ;
913916
914917 const { browser } = compass ;
915918
@@ -923,21 +926,11 @@ export async function beforeTests(
923926 return compass ;
924927}
925928
926- export async function afterTests (
927- compass ?: Compass ,
928- test ?: Mocha . Hook | Mocha . Test ,
929- step ?: string
930- ) : Promise < void > {
929+ export async function cleanup ( compass ?: Compass ) : Promise < void > {
931930 if ( ! compass ) {
932931 return ;
933932 }
934933
935- if ( test && test . state === undefined ) {
936- // if there's no state, then it is probably because the before() hook failed
937- const filename = screenshotPathName ( `${ test . fullTitle ( ) } -hook` ) ;
938- await compass . capturePage ( filename ) ;
939- }
940-
941934 let timeoutId ;
942935 const timeoutPromise = new Promise < void > ( ( resolve ) => {
943936 timeoutId = setTimeout ( ( ) => {
@@ -948,7 +941,7 @@ export async function afterTests(
948941
949942 const closePromise = ( async function close ( ) : Promise < void > {
950943 try {
951- await compass . stop ( test , step ) ;
944+ await compass . stop ( ) ;
952945 } catch ( err ) {
953946 debug ( 'An error occurred while stopping compass:' ) ;
954947 debug ( err ) ;
@@ -983,12 +976,21 @@ export function outputFilename(filename: string): string {
983976 return path . join ( OUTPUT_PATH , filename ) ;
984977}
985978
986- export async function afterTest (
979+ export async function screenshotIfFailed (
987980 compass : Compass ,
988981 test ?: Mocha . Hook | Mocha . Test
989982) : Promise < void > {
990- if ( test && test . state === 'failed' ) {
991- await compass . capturePage ( screenshotPathName ( test . fullTitle ( ) ) ) ;
983+ // NOTE: you cannot use this inside a test because the test wouldn't be marked
984+ // as failed yet. It is made for use inside an afterEach() to go with the
985+ // pattern where we init() compass in a before() hook and cleanup() in an
986+ // after() hook.
987+ if ( test ) {
988+ if ( test . state === undefined ) {
989+ // if there's no state, then it is probably because the before() hook failed
990+ await compass . capturePage ( screenshotPathName ( `${ test . fullTitle ( ) } -hook` ) ) ;
991+ } else if ( test . state === 'failed' ) {
992+ await compass . capturePage ( screenshotPathName ( test . fullTitle ( ) ) ) ;
993+ }
992994 }
993995}
994996
@@ -1020,3 +1022,14 @@ function redact(value: string): string {
10201022
10211023 return value ;
10221024}
1025+
1026+ export function subtestTitle (
1027+ test : Mocha . Runnable | undefined ,
1028+ step : string
1029+ ) : string {
1030+ // Sometimes we start and stop compass multiple times in the same test. In
1031+ // that case it is handy to give them unique names. That's what this function
1032+ // is for.
1033+ const title = test ?. fullTitle ( ) ?? formattedDate ( ) ;
1034+ return `${ title } _${ step } ` ;
1035+ }
0 commit comments