diff --git a/source/index.ts b/source/index.ts index b8de2d7..77a989a 100644 --- a/source/index.ts +++ b/source/index.ts @@ -80,7 +80,11 @@ const timer = (request: ClientRequestWithTimings): Timings => { request.prependOnceListener('abort', (): void => { timings.abort = Date.now(); - timings.phases.total = Date.now() - timings.start; + + // Let the `end` response event be responsible for setting the total phase + if (!timings.response) { + timings.phases.total = Date.now() - timings.start; + } }); const onSocket = (socket: Socket): void => { diff --git a/tests/test.ts b/tests/test.ts index a42608c..78fb6a3 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -183,16 +183,36 @@ test('sets `total` on response error', async t => { t.is(timings.phases.total, timings.error! - timings.start); }); -test('sets `total` on abort', async t => { +test.cb('sets `total` on abort', t => { const request = http.get(server.url!); request.abort(); const timings = timer(request); - await pEvent(request, 'abort'); + process.nextTick(() => { + t.is(typeof timings.abort, 'number'); + t.is(timings.phases.total, timings.abort! - timings.start); + t.falsy((request as any).res); - t.is(typeof timings.abort, 'number'); - t.is(timings.phases.total, timings.abort! - timings.start); + t.end(); + }); +}); + +test.cb('sets `total` on abort - after `response` event', t => { + const request = http.get(server.url!); + const timings = timer(request); + + request.once('response', response => { + request.abort(); + + response.once('end', () => { + t.is(typeof timings.abort, 'number'); + t.is(timings.phases.total, timings.end! - timings.start); + t.truthy((request as any).res); + + t.end(); + }); + }); }); test('doesn\'t throw when someone used `.prependOnceListener()`', t => {