summaryrefslogtreecommitdiffstats
path: root/test/fetch/resource-timing.js
blob: d266f28bdc85bca4092e1d364ab1be72771df879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict'

const { test } = require('tap')
const { createServer } = require('http')
const { nodeMajor, nodeMinor } = require('../../lib/core/util')
const { fetch } = require('../..')

const {
  PerformanceObserver,
  performance
} = require('perf_hooks')

const skip = nodeMajor < 18 || (nodeMajor === 18 && nodeMinor < 2)

test('should create a PerformanceResourceTiming after each fetch request', { skip }, (t) => {
  t.plan(8)

  const obs = new PerformanceObserver(list => {
    const expectedResourceEntryName = `http://localhost:${server.address().port}/`

    const entries = list.getEntries()
    t.equal(entries.length, 1)
    const [entry] = entries
    t.same(entry.name, expectedResourceEntryName)
    t.strictSame(entry.entryType, 'resource')

    t.ok(entry.duration >= 0)
    t.ok(entry.startTime >= 0)

    const entriesByName = list.getEntriesByName(expectedResourceEntryName)
    t.equal(entriesByName.length, 1)
    t.strictSame(entriesByName[0], entry)

    obs.disconnect()
    performance.clearResourceTimings()
  })

  obs.observe({ entryTypes: ['resource'] })

  const server = createServer((req, res) => {
    res.end('ok')
  }).listen(0, async () => {
    const body = await fetch(`http://localhost:${server.address().port}`)
    t.strictSame('ok', await body.text())
  })

  t.teardown(server.close.bind(server))
})

test('should include encodedBodySize in performance entry', { skip }, (t) => {
  t.plan(4)
  const obs = new PerformanceObserver(list => {
    const [entry] = list.getEntries()
    t.equal(entry.encodedBodySize, 2)
    t.equal(entry.decodedBodySize, 2)
    t.equal(entry.transferSize, 2 + 300)

    obs.disconnect()
    performance.clearResourceTimings()
  })

  obs.observe({ entryTypes: ['resource'] })

  const server = createServer((req, res) => {
    res.end('ok')
  }).listen(0, async () => {
    const body = await fetch(`http://localhost:${server.address().port}`)
    t.strictSame('ok', await body.text())
  })

  t.teardown(server.close.bind(server))
})