-
Notifications
You must be signed in to change notification settings - Fork 1
/
jest.setup.ts
98 lines (81 loc) · 2.44 KB
/
jest.setup.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
87
88
89
90
91
92
93
94
95
96
97
98
// eslint-disable-next-line import/no-unassigned-import
import '@testing-library/jest-dom'
import { jest } from '@jest/globals'
import { type Event as SentryEvent } from '@sentry/types'
import * as cryptoNode from 'crypto'
import { http } from 'msw'
import { setupServer } from 'msw/node'
import {
currentTestEnvironment,
defaultApiServer,
defaultFrontendServer,
getDefaultCFEnvironment,
givenApi,
givenFrontend,
Uuid,
} from './__tests__/__utils__'
const timeout = currentTestEnvironment().getNeededTimeout()
if (timeout) {
jest.setTimeout(timeout)
}
beforeAll(() => {
globalThis.server = setupServer()
globalThis.server.listen({ onUnhandledRequest: 'error' })
})
beforeEach(() => {
globalThis.uuids = new Array<Uuid>()
givenApi(defaultApiServer())
givenFrontend(defaultFrontendServer())
globalThis.sentryEvents = []
mockSentryServer()
addGlobalMocks()
})
afterEach(() => {
globalThis.server.resetHandlers()
})
afterAll(() => {
globalThis.server.close()
})
function addGlobalMocks() {
// `@cloudflare/workers-types` defines `crypto` to be on the
// [self](https://developer.mozilla.org/en-US/docs/Web/API/Window/self)
// property which makes sense for a workers environment. However when we run
// the tests via node `self` is not defined and we need to set global variables
// via `global` or `globalThis`.
//
// @ts-expect-error When running node `self` is not defined but `globalThis` is
globalThis.crypto = {
subtle: {
digest(encoding: string, message: Uint8Array) {
return Promise.resolve(
cryptoNode
.createHash(encoding.toLowerCase().replace('-', ''))
.update(message)
.digest(),
)
},
},
randomUUID: cryptoNode.randomUUID,
} as unknown as typeof crypto
}
function mockSentryServer() {
const { hostname, pathname } = new URL(getDefaultCFEnvironment().SENTRY_DSN)
const sentryUrl = `https://${hostname}/api${pathname}/envelope/`
globalThis.server.use(
http.post(sentryUrl, async ({ request }) => {
const reqText = await request.text()
const events = reqText
.split('\n')
.map((x) => JSON.parse(x) as SentryEvent)
globalThis.sentryEvents.push(...events)
return new Response()
}),
)
}
export {}
declare global {
// eslint-disable-next-line no-var
var server: ReturnType<typeof import('msw/node').setupServer>
// eslint-disable-next-line no-var
var sentryEvents: SentryEvent[]
}