Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/model/http/har.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ export interface HarEntry extends HarFormat.Entry {
_pinned?: true;
}

const compareHarEntryStartTimes = (
a: Pick<HarFormat.Entry, 'startedDateTime'>,
b: Pick<HarFormat.Entry, 'startedDateTime'>
) => dateFns.parse(a.startedDateTime).getTime() -
dateFns.parse(b.startedDateTime).getTime();

export interface HarWebSocketMessage {
type: 'send' | 'receive';
opcode: 1 | 2;
Expand Down Expand Up @@ -119,7 +125,9 @@ export async function generateHar(
const errors = otherEvents.filter(e => e.isTlsFailure()) as FailedTlsConnection[];

const sourcePages = getSourcesAsHarPages(exchanges);
const entries = await Promise.all(exchanges.map(e => generateHarHttpEntry(e, options)));
const entries = (await Promise.all(
exchanges.map(e => generateHarHttpEntry(e, options))
)).sort(compareHarEntryStartTimes);
const errorEntries = errors.map(generateHarTlsError);

return {
Expand Down Expand Up @@ -529,11 +537,7 @@ export async function parseHar(harContents: unknown): Promise<ParsedHar> {
const pinnedIds: string[] = []

har.log.entries
.sort((a, b) => {
const aStartTime = dateFns.parse(a.startedDateTime).getTime();
const bStartTime = dateFns.parse(b.startedDateTime).getTime();
return aStartTime - bStartTime;
})
.sort(compareHarEntryStartTimes)
.forEach((entry, i) => {
const id = baseId + i;
const isWebSocket = entry._resourceType === 'websocket';
Expand Down
19 changes: 19 additions & 0 deletions test/unit/model/http/har.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from '../../../test-setup';

import { generateHar } from '../../../../src/model/http/har';
import { getExchangeData } from '../../unit-test-helpers';

describe('HAR generation', () => {
it('orders entries chronologically regardless of selection order', async () => {
const olderExchange = getExchangeData({ statusCode: 403 });
olderExchange.timingEvents.startTime = Date.parse('2026-07-17T20:35:23.997+01:00');

const newerExchange = getExchangeData({ statusCode: 200 });
newerExchange.timingEvents.startTime = Date.parse('2026-07-17T20:36:12.733+01:00');

const har = await generateHar([newerExchange, olderExchange]);

expect(har.log.entries.map(entry => entry.response.status))
.to.deep.equal([403, 200]);
});
});
Loading