Skip to content

Commit

Permalink
Add tests for TTFB
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Dec 26, 2024
1 parent e5a6ea4 commit 750bd40
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
30 changes: 30 additions & 0 deletions test/e2e/onTTFB-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,36 @@ describe('onTTFB()', async function () {
assertValidEntry(ttfb.entries[0]);
});

it('works when calling the function twice with different options', async function () {
await navigateTo('/test/ttfb?doubleCall=1&reportAllChanges2=1');

await beaconCountIs(1, {instance: 1});
await beaconCountIs(1, {instance: 2});

const [ttfb1] = await getBeacons({instance: 1});
const [ttfb2] = await getBeacons({instance: 2});

assert(ttfb1.value >= 0);
assert(ttfb1.value >= ttfb1.entries[0].requestStart);
assert(ttfb1.value <= ttfb1.entries[0].loadEventEnd);
assert(ttfb1.id.match(/^v5-\d+-\d+$/));
assert.strictEqual(ttfb1.name, 'TTFB');
assert.strictEqual(ttfb1.value, ttfb1.delta);
assert.strictEqual(ttfb1.rating, 'good');
assert.strictEqual(ttfb1.navigationType, 'navigate');
assert.strictEqual(ttfb1.entries.length, 1);
assertValidEntry(ttfb1.entries[0]);

assert(ttfb2.id.match(/^v5-\d+-\d+$/));
assert(ttfb2.id !== ttfb1.id);
assert.strictEqual(ttfb2.value, ttfb1.value);
assert.strictEqual(ttfb2.delta, ttfb1.delta);
assert.strictEqual(ttfb2.name, ttfb1.name);
assert.strictEqual(ttfb2.rating, ttfb1.rating);
assert.deepEqual(ttfb2.entries, ttfb1.entries);
assert.strictEqual(ttfb2.navigationType, ttfb1.navigationType);
});

describe('attribution', function () {
it('includes attribution data on the metric object', async function () {
await navigateTo('/test/ttfb?attribution=1');
Expand Down
26 changes: 22 additions & 4 deletions test/views/ttfb.njk
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,33 @@
}
</script>

<script type="module">
const {onTTFB} = await __testImport('{{ modulePath }}');
<script type="module">
const {onTTFB} = await __testImport('{{ modulePath }}');
onTTFB((ttfb) => {
ttfb.instance = 1;
// Log for easier manual testing.
console.log(ttfb);
// Test sending the metric to an analytics endpoint.
navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(ttfb)));
}, {
reportAllChanges: self.__reportAllChanges,
});
if (self.__doubleCall) {
onTTFB((ttfb) => {
ttfb.instance = 2;
// Log for easier manual testing.
console.log(ttfb);
// Test sending the metric to an analytics endpoint.
navigator.sendBeacon(`/collect`, JSON.stringify(__toSafeObject(ttfb)));
}, {reportAllChanges: self.__reportAllChanges});
</script>
}, {
reportAllChanges: self.__reportAllChanges2,
});
}
</script>
{% endblock %}

0 comments on commit 750bd40

Please sign in to comment.