-
Notifications
You must be signed in to change notification settings - Fork 4
/
jest.setup.ts
125 lines (101 loc) · 3.23 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
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
import { flush as flushSentry, type Event } from '@sentry/node'
import * as Sentry from '@sentry/node'
import crypto from 'crypto'
import { setupServer } from 'msw/node'
import { createPool, type Pool } from 'mysql2/promise'
import {
defaultSpreadsheetApi,
givenSpreadheetApi,
MockKratos,
} from './__tests__/__utils__'
import { createCache, createNamespacedCache } from '~/cache'
import { type Cache } from '~/context/cache'
import { Database } from '~/database'
import { initializeSentry } from '~/internals/sentry'
import { timeToMilliseconds, Time, Timer } from '~/timer'
beforeAll(() => {
initializeSentry({
dsn: 'https://[email protected]/0',
environment: 'testing',
context: 'testing',
testTransport: () => {
return {
send: async (envelope: unknown) => {
const [, items] = envelope as unknown[][]
;(items as Sentry.Event[][]).forEach(([_, data]) => {
global.sentryEvents.push(data)
})
return Promise.resolve({})
},
flush: async () => Promise.resolve(true),
}
},
})
const timer = new MockTimer()
const server = setupServer()
const kratos = new MockKratos()
global.pool = createPool(process.env.MYSQL_URI)
global.server = server
global.timer = timer
global.kratos = kratos
global.server.listen({ onUnhandledRequest: 'bypass' })
process.env.OPENAI_API_KEY = 'fake-test-key-we-are-mocking-responses'
})
beforeEach(async () => {
global.databaseForTests = new Database(global.pool)
await global.databaseForTests.beginTransaction()
const baseCache = createCache({ timer: global.timer })
global.cache = createNamespacedCache(baseCache, generateRandomString(10))
await global.cache.ready()
givenSpreadheetApi(defaultSpreadsheetApi())
global.timer.flush()
global.sentryEvents = []
global.kratos.identities = []
process.env.ENVIRONMENT = 'local'
})
afterEach(async () => {
await global.databaseForTests.rollbackAllTransactions()
await flushSentry()
global.server.resetHandlers()
await global.cache.quit()
// redis.quit() creates a thread to close the connection.
// We wait until all threads have been run once to ensure the connection closes.
await new Promise((resolve) => setImmediate(resolve))
})
afterAll(async () => {
global.server.close()
await global.databaseForTests.close()
})
class MockTimer implements Timer {
private currentTime = 0
public now() {
return this.currentTime
}
public flush() {
this.currentTime = Date.now()
}
// We make this synchronous function asynchronous just to make clear that this would be asynchronous in production.
// eslint-disable-next-line @typescript-eslint/require-await
public async waitFor(time: Time) {
this.currentTime += timeToMilliseconds(time)
}
public setCurrentDate(date: Date) {
this.currentTime = date.getTime()
}
}
function generateRandomString(length: number) {
return crypto
.randomBytes(Math.ceil(length / 2))
.toString('hex')
.slice(0, length)
}
declare global {
/* eslint-disable no-var */
var cache: Cache
var server: ReturnType<typeof import('msw/node').setupServer>
var timer: MockTimer
var sentryEvents: Event[]
var kratos: MockKratos
var pool: Pool
var databaseForTests: Database
}