forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.cy.js
67 lines (58 loc) · 1.87 KB
/
spec.cy.js
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
// enables intelligent code completion for Cypress commands
// https://on.cypress.io/intelligent-code-completion
/// <reference types="cypress" />
/* eslint-disable no-console */
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing
// for now assume it is always something like "cache;desc="my example";dur=2002"
const parseTiming = (singleValue) => {
const parts = singleValue.split(';')
return {
name: parts[0],
description: parts[1].split('"')[1],
duration: parseFloat(parts[2].split('=')[1]), // in ms
}
}
const parseServerTimings = (value) => {
return value.split(',')
.map((s) => s.trim())
.map(parseTiming)
}
const logTiming = (timing) => {
return cy.log(`**${timing.name}** ${timing.description} ${timing.duration}ms`)
}
describe('Server', () => {
it('reports timings', function () {
cy.intercept('/').as('document')
cy.visit('/')
cy.wait('@document').its('response.headers.server-timing')
.then(parseServerTimings)
.then(console.table)
})
it('logs timings', function () {
cy.intercept('/').as('document')
cy.visit('/')
cy.wait('@document').its('response.headers.server-timing')
.then(parseServerTimings)
.then((timings) => {
timings.forEach(logTiming)
})
})
it('reports timings using performance entry', () => {
cy.visit('/')
cy.window().its('performance')
.invoke('getEntriesByType', 'navigation')
.its('0.serverTiming.0')
.then(logTiming)
// we can even assert that the duration is below certain limit
cy.window().its('performance')
.invoke('getEntriesByType', 'navigation')
.its('0.serverTiming')
.then((timings) => {
// find the cache timing among all server timings
return Cypress._.find(timings, { name: 'cache' })
})
.then((cacheTiming) => {
expect(cacheTiming).property('duration').to.be.lessThan(2100)
})
})
})