import * as assert from "assert" ; import { Context , Test } from "mocha" ; import { writeToLog } from "./testLog" ; export interface ITestPreparation { pretest ( this ) : ITestPreparationResult ; } export interface ITestPreparationResult { postTestActions ? ( ) : void ; skipTest ? : string ; } export function testWithPrep ( expectation , preparations ? , callback ? ) { try { return test ( expectation , async function ( this ) : Promise < unknown > { const postTestActions = [ ] ; try { if ( ! callback ) { this . skip ( ) ; } for ( let prep of preparations ?? [ ] ) { const prepResult = prep . pretest . call ( this ) ; if ( prepResult . skipTest ) { writeToLog ( `Skipping test because: ${ prepResult . skipTest }` ) ; this . skip ( ) ; } if ( prepResult . postTestActions ) { postTestActions . push ( prepResult . postTestActions ) ; } } try { return await callback . call ( this ) ; } catch ( error ) { if ( ( < { message ? : string ; } > error ) . message === 'sync skip' ) { } else { throw error ; } } } finally { for ( let post of postTestActions ) { post ( ) ; } } } ) ; } catch ( err ) { assert . fail ( "testWithPrep shouldn't throw" ) ; } }