-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbrowser-check.spec.ts
More file actions
361 lines (326 loc) · 12.4 KB
/
browser-check.spec.ts
File metadata and controls
361 lines (326 loc) · 12.4 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import fs from 'node:fs'
import path from 'node:path'
import { describe, it, expect, beforeEach, afterAll } from 'vitest'
import { BrowserCheck, CheckGroup } from '../index'
import { Project, Session } from '../project'
import { Diagnostics } from '../diagnostics'
const runtimes = {
'2022.10': { name: '2022.10', default: false, stage: 'CURRENT', description: 'Main updates are Playwright 1.28.0, Node.js 16.x and Typescript support. We are also dropping support for Puppeteer', dependencies: { '@playwright/test': '1.28.0', '@opentelemetry/api': '1.0.4', '@opentelemetry/sdk-trace-base': '1.0.1', '@faker-js/faker': '5.5.3', aws4: '1.11.0', axios: '0.27.2', btoa: '1.2.1', chai: '4.3.7', 'chai-string': '1.5.0', 'crypto-js': '4.1.1', expect: '29.3.1', 'form-data': '4.0.0', jsonwebtoken: '8.5.1', lodash: '4.17.21', mocha: '10.1.0', moment: '2.29.2', node: '16.x', otpauth: '9.0.2', playwright: '1.28.0', typescript: '4.8.4', uuid: '9.0.0' } },
}
describe('BrowserCheck', () => {
beforeEach(() => {
Session.resetSharedFiles()
})
afterAll(() => {
Session.resetSharedFiles()
})
it('should correctly load file dependencies', async () => {
Session.basePath = __dirname
Session.availableRuntimes = runtimes
const getFilePath = (filename: string) => path.join(__dirname, 'fixtures', 'browser-check', filename)
const bundle = await BrowserCheck.bundle(getFilePath('entrypoint.js'), '2022.10')
Session.basePath = undefined
expect(bundle).toMatchObject({
script: fs.readFileSync(getFilePath('entrypoint.js')).toString(),
scriptPath: 'fixtures/browser-check/entrypoint.js',
dependencies: [
0,
1,
],
})
expect(Session.sharedFiles).toEqual([
{
path: 'fixtures/browser-check/dep1.js',
content: fs.readFileSync(getFilePath('dep1.js')).toString(),
},
{
path: 'fixtures/browser-check/dep2.js',
content: fs.readFileSync(getFilePath('dep2.js')).toString(),
},
])
})
it('should fail to bundle if runtime is not specified and default runtime is not set', async () => {
const getFilePath = (filename: string) => path.join(__dirname, 'fixtures', 'api-check', filename)
const bundle = async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _bundle = await BrowserCheck.bundle(getFilePath('entrypoint.js'), undefined)
}
Session.basePath = __dirname
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = undefined
await expect(bundle()).rejects.toThrow('runtime is not set')
Session.basePath = undefined
Session.defaultRuntimeId = undefined
})
it('should successfully bundle if runtime is not specified but default runtime is set', async () => {
const getFilePath = (filename: string) => path.join(__dirname, 'fixtures', 'api-check', filename)
const bundle = async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _bundle = await BrowserCheck.bundle(getFilePath('entrypoint.js'), undefined)
}
Session.basePath = __dirname
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.10'
await expect(bundle()).resolves.not.toThrow()
Session.basePath = undefined
Session.defaultRuntimeId = undefined
})
it('should fail to bundle if runtime is not supported even if default runtime is set', async () => {
const getFilePath = (filename: string) => path.join(__dirname, 'fixtures', 'api-check', filename)
const bundle = async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _bundle = await BrowserCheck.bundle(getFilePath('entrypoint.js'), '9999.99')
}
Session.basePath = __dirname
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.02'
await expect(bundle()).rejects.toThrow('9999.99 is not supported')
Session.basePath = undefined
Session.defaultRuntimeId = undefined
})
it('should not synthesize runtime if not specified even if default runtime is set', async () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.02'
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
})
const bundle = await browserCheck.bundle()
const payload = bundle.synthesize()
expect(payload.runtimeId).toBeUndefined()
Session.defaultRuntimeId = undefined
})
it('should synthesize runtime if specified', async () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.availableRuntimes = runtimes
Session.defaultRuntimeId = '2022.02'
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
runtimeId: '2022.02',
code: { content: 'console.log("test check")' },
})
const bundle = await browserCheck.bundle()
const payload = bundle.synthesize()
expect(payload.runtimeId).toEqual('2022.02')
Session.defaultRuntimeId = undefined
})
it('should apply default check settings', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.checkDefaults = { tags: ['default tags'] }
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
})
Session.checkDefaults = undefined
expect(browserCheck).toMatchObject({ tags: ['default tags'] })
})
it('should overwrite default check settings with check-specific config', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.checkDefaults = { tags: ['default tags'] }
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
tags: ['test check'],
code: { content: 'console.log("test check")' },
})
Session.checkDefaults = undefined
expect(browserCheck).toMatchObject({ tags: ['test check'] })
})
it('should apply browser check defaults instead of generic check defaults', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
Session.checkDefaults = { tags: ['check default'] }
Session.browserCheckDefaults = { tags: ['browser check default'] }
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
})
Session.checkDefaults = undefined
Session.browserCheckDefaults = undefined
expect(browserCheck).toMatchObject({ tags: ['browser check default'] })
})
it('should support setting groups with `groupId`', async () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const group = new CheckGroup('main-group', { name: 'Main Group', locations: [] })
const check = new BrowserCheck('main-check', {
name: 'Main Check',
code: { content: '' },
groupId: group.ref(),
})
const bundle = await check.bundle()
expect(bundle.synthesize()).toMatchObject({ groupId: { ref: 'main-group' } })
})
it('should support setting groups with `group`', async () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const group = new CheckGroup('main-group', { name: 'Main Group', locations: [] })
const check = new BrowserCheck('main-check', {
name: 'Main Check',
code: { content: '' },
group,
})
const bundle = await check.bundle()
expect(bundle.synthesize()).toMatchObject({ groupId: { ref: 'main-group' } })
})
describe('playwrightConfig', () => {
it('should set from check defaults', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const defaults = {
playwrightConfig: {
use: {
baseURL: 'https://example.org',
},
},
}
Session.checkDefaults = {
...defaults,
}
Session.browserCheckDefaults = {}
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
})
Session.checkDefaults = undefined
Session.browserCheckDefaults = undefined
expect(browserCheck).toMatchObject(defaults)
})
it('should set from browser check defaults', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const defaults = {
playwrightConfig: {
use: {
baseURL: 'https://example.org',
},
},
}
Session.checkDefaults = {}
Session.browserCheckDefaults = {
...defaults,
}
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
})
Session.checkDefaults = undefined
Session.browserCheckDefaults = undefined
expect(browserCheck).toMatchObject(defaults)
})
it('should not override with default if set', () => {
Session.project = new Project('project-id', {
name: 'Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
const defaults = {
playwrightConfig: {
use: {
baseURL: 'https://example.org/foo',
},
},
}
Session.checkDefaults = {
...defaults,
}
Session.browserCheckDefaults = {
...defaults,
}
const custom = {
playwrightConfig: {
use: {
baseURL: 'https://example.org/bar',
}
}
}
const browserCheck = new BrowserCheck('test-check', {
name: 'Test Check',
code: { content: 'console.log("test check")' },
...custom,
})
Session.checkDefaults = undefined
Session.browserCheckDefaults = undefined
expect(browserCheck).toMatchObject(custom)
})
})
describe('validation', () => {
beforeEach(() => {
Session.project = new Project('validation-test-project', {
name: 'Validation Test Project',
repoUrl: 'https://github.com/checkly/checkly-cli',
})
})
it('should validate SSL check domain format', async () => {
const browserCheck = new BrowserCheck('test-browser', {
name: 'Test Browser',
code: { content: 'test' },
sslCheckDomain: 'invalid domain'
})
const diagnostics = new Diagnostics()
await browserCheck.validate(diagnostics)
expect(diagnostics.observations).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('Invalid FQDN')
})
])
)
})
it('should accept valid SSL check domain', async () => {
const browserCheck = new BrowserCheck('test-browser', {
name: 'Test Browser',
code: { content: 'test' },
sslCheckDomain: 'app.checklyhq.com'
})
const diagnostics = new Diagnostics()
await browserCheck.validate(diagnostics)
expect(diagnostics.isFatal()).toBe(false)
})
it('should validate various invalid SSL check domains', async () => {
const invalidDomains = [
'domain with spaces.com', // Contains spaces
'', // Empty string
'example..com', // Double dots
'-sub.example.com' // Starts with hyphen
]
for (const [i, domain] of invalidDomains.entries()) {
const browserCheck = new BrowserCheck(`test-browser-invalid-${i}`, {
name: 'Test Browser',
code: { content: 'test' },
sslCheckDomain: domain
})
const diagnostics = new Diagnostics()
await browserCheck.validate(diagnostics)
expect(diagnostics.observations).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('Invalid FQDN')
})
])
)
}
})
})
})