import { Runner , Test } from "mocha" ; interface MochaTestResults { total ? : number ; tests ? : StrippedTestResults [ ] ; } interface StrippedTestResults { title : string ; duration : number ; path : string [ ] ; err ? : string ; status : "failed" | "passed" | "pending" ; } declare global { interface Window { __mocha_test_results__ : MochaTestResults ; } } export function MochaReporter ( runner ) { const tests = [ ] ; runner . on ( "start" , ( ) => { window . __mocha_test_results__ = { } ; window . __mocha_test_results__ . total = runner . total ; } ) ; runner . on ( "end" , ( ) => { const strip = ( test ) => { return { title : test . title , path : test . titlePath ( ) , duration : test . duration , err : test . err ?. stack || test . err ?. message , status : test . state , } ; } ; window . __mocha_test_results__ . tests = tests . map ( strip ) ; const event = new Event ( "testsComplete" , { bubbles : true } ) ; window . dispatchEvent ( event ) ; } ) ; runner . on ( "pending" , ( test ) => tests . push ( test ) ) ; runner . on ( "fail" , ( test , error ) => { test . err = error ; tests . push ( test ) ; } ) ; runner . on ( "pass" , ( test ) => tests . push ( test ) ) ; }