import * as SUT from '../index' ; import { stringifyRequest , RequestInfo , sleep } from '../../../../utils' ; import { Browser , chromium , Request } from 'playwright' ; import { FakeServer } from 'simple-fake-server' ; import * as path from 'path' ; describe ( 'record failed requests' , ( ) : void => { let browser = undefined ; let fakeServer = undefined ; beforeAll ( ( ) => { fakeServer = new FakeServer ( 1237 ) ; fakeServer . start ( ) ; } ) ; afterAll ( ( ) => { if ( fakeServer ) { fakeServer . stop ( ) ; } } ) ; beforeEach ( ( ) : void => { } ) ; afterEach ( async ( ) : Promise < void > => { if ( browser ) { await browser . close ( ) ; } } ) ; test ( 'should record HTP 500 requests' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const context = await browser . newContext ( { viewport : null } ) ; const page = await context . newPage ( ) ; fakeServer && fakeServer . http . get ( ) . to ( '/500' ) . willFail ( 500 ) ; const requests = [ ] ; const callback = ( request ) => requests . push ( request ) ; await SUT . recordFailedRequests ( page , callback ) ; await page . goto ( `file:${ path . join ( __dirname , 'record-failed-requests-500.test.html' ) }` ) ; await sleep ( 3000 ) ; expect ( requests . length ) . toBe ( 1 ) ; const stringifiedRequest = await stringifyRequest ( requests [ 0 ] ) ; const request = JSON . parse ( stringifiedRequest ) as RequestInfo ; expect ( request . response ! . status ) . toBe ( 500 ) ; expect ( request . response ! . statusText ) . toBe ( 'Internal Server Error' ) ; } ) ; } ) ;