/
vercel-analytics.test.ts
86 lines (73 loc) · 2.29 KB
/
vercel-analytics.test.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check } from 'next-test-utils'
describe('vercel analytics', () => {
const isDev = (global as any).isNextDev
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
function runTests({ assetPrefix }: { assetPrefix?: boolean }) {
beforeAll(async () => {
next = await createNext({
files: __dirname,
dependencies: {
swr: '2.0.0-rc.0',
react: 'latest',
'react-dom': 'latest',
sass: 'latest',
},
skipStart: true,
env: {
VERCEL_ANALYTICS_ID: 'fake-analytics-id',
},
})
if (assetPrefix) {
const content = await next.readFile('next.config.js')
await next.patchFile(
'next.config.js',
content
.replace('// assetPrefix', 'assetPrefix')
.replace('// beforeFiles', 'beforeFiles')
)
}
await next.start()
})
afterAll(() => next.destroy())
// Analytics events are only sent in production
;(isDev ? describe.skip : describe)('Vercel analytics', () => {
it('should send web vitals to Vercel analytics', async () => {
let eventsCount = 0
let countEvents = false
const browser = await next.browser('/client-nested', {
beforePageLoad(page) {
page.route(
'https://vitals.vercel-insights.com/v1/vitals',
(route) => {
if (countEvents) {
eventsCount += 1
}
route.fulfill()
}
)
},
})
// Start counting analytics events
countEvents = true
// Refresh will trigger CLS and LCP. When page loads FCP and TTFB will trigger:
await browser.refresh()
// After interaction LCP and FID will trigger
await browser.elementByCss('button').click()
// Make sure all registered events in performance-relayer has fired
await check(() => eventsCount, /6/)
})
})
}
describe('without assetPrefix', () => {
runTests({})
})
describe('with assetPrefix', () => {
runTests({ assetPrefix: true })
})
})