Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Nov 18, 2024
1 parent cb4ae6d commit 99df11b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
32 changes: 30 additions & 2 deletions test/e2e/onTTFB-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,40 @@ describe('onTTFB()', async function () {

it('reports the correct value for Early Hints', async function () {
await navigateTo(
'/test/ttfb?responseStart=10&earlyHintsDelay=50&attribution=1',
// '/test/ttfb?responseStart=10&earlyHintsDelay=50&attribution=1',
'/test/ttfb?earlyHintsDelay=50&attribution=1',
);

const ttfb = await getTTFBBeacon();

assert.strictEqual(ttfb.value, 10);
if ('finalResponseHeadersStart' in ttfb.attribution.navigationEntry) {
assert.strictEqual(
ttfb.value,
ttfb.attribution.navigationEntry.responseStart,
);
assert.strictEqual(
ttfb.value,
ttfb.attribution.navigationEntry.firstInterimResponseStart,
);
assert(
ttfb.value <
ttfb.attribution.navigationEntry.finalResponseHeadersStart,
);
} else if (
'firstInterimResponseStart' in ttfb.attribution.navigationEntry
) {
// TODO: Can remove these after Chrome 133 lands and above is used.
assert(ttfb.value < ttfb.attribution.navigationEntry.responseStart);
assert.strictEqual(
ttfb.value,
ttfb.attribution.navigationEntry.firstInterimResponseStart,
);
} else {
assert.strictEqual(
ttfb.value,
ttfb.attribution.navigationEntry.responseStart,
);
}
});
});
});
Expand Down
39 changes: 27 additions & 12 deletions test/views/ttfb.njk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
if (params.has('responseStart')) {
Object.defineProperty(navEntry, 'responseStart', {
value: Number(params.get('responseStart')),
enumerable: true,
writeable: true,
});
}
Expand All @@ -44,21 +46,34 @@
if (params.has('earlyHintsDelay')) {
const earlyHintsDelay = Number(params.get('earlyHintsDelay'))
const responseStart = navEntry.responseStart;
// Block for delay time—to avoid the library seeing future timestamps,
// and so not reporting a TTFB at all as it's invalid.
block(Number(params.get('earlyHintsDelay')));
Object.defineProperties(navEntry, {
'firstInterimResponseStart': {
value: Number(responseStart),
},
'finalResponseHeadersStart': {
value: Number(responseStart + earlyHintsDelay),
},
'responseStart': {
value: Number(responseStart + earlyHintsDelay),
}
});
// Chrome >= 133 has both finalResponseHeadersStart and firstInterimResponseStart
if ('finalResponseHeadersStart' in PerformanceNavigationTiming.prototype) {
Object.defineProperties(navEntry, {
'firstInterimResponseStart': {
value: Number(navEntry.responseStart),
enumerable: true,
},
'finalResponseHeadersStart': {
value: Number(navEntry.responseStart + earlyHintsDelay),
enumerable: true,
}
});
} else if ('firstInterimResponseStart' in PerformanceNavigationTiming.prototype) {
// TODO: Can remove these after Chrome 133 lands and above is used.
Object.defineProperties(navEntry, {
'firstInterimResponseStart': {
value: Number(navEntry.responseStart),
enumerable: true,
},
'responseStart': {
value: Number(navEntry.responseStart + earlyHintsDelay),
enumerable: true,
},
})
};
}
</script>

Expand Down

0 comments on commit 99df11b

Please sign in to comment.