import { expect } from "chai" ; import { benchmark } from "../Runner" ; import { BenchmarkType , isParentProcess } from "../Configuration" ; describe ( "`benchmark` function" , ( ) => { describe ( "uses `before` and `after`" , ( ) => { let beforeHasBeenCalled = false ; let afterHasBeenCalled = false ; benchmark ( { title : "test" , before : async ( ) => delay ( 1 ) . then ( ( ) => { beforeHasBeenCalled = true ; } ) , benchmarkFn : ( ) => { expect ( beforeHasBeenCalled ) . to . equal ( true , "before should be called before test body" ) ; expect ( afterHasBeenCalled ) . to . equal ( false , "after should not be called during test execution" ) ; } , after : async ( ) => delay ( 1 ) . then ( ( ) => { afterHasBeenCalled = true ; } ) , type : BenchmarkType . OwnCorrectness , } ) ; afterEach ( ( ) => { if ( ! isParentProcess ) { expect ( afterHasBeenCalled ) . to . equal ( true , "after should be called after test execution" ) ; } } ) ; } ) ; function doLoop ( upperLimit ) { let i = 0 ; while ( i < upperLimit ) { i += 1 ; } } for ( const loopSize of [ 1e6 ] ) { benchmark ( { title : `while loop with ${ loopSize } iterations` , benchmarkFn : ( ) => doLoop ( loopSize ) , type : BenchmarkType . OwnCorrectness , } ) ; benchmark ( { title : `async-initialized while loop with ${ loopSize } iterations` , benchmarkFnAsync : async ( ) => nextTick ( ( ) => doLoop ( loopSize ) ) , type : BenchmarkType . OwnCorrectness , } ) ; } } ) ; const dummyPromise = Promise . resolve ( ) ; param callback returns const nextTick = async ( callback ) : Promise < void > => dummyPromise . then ( callback ) ; /**
 * Waits for the provided duration in milliseconds. See
 *  https .
  const delay = async ( milliseconds ) : Promise < void > => new Promise ( ( resolve ) => setTimeout ( resolve , milliseconds ) ) ;