-
Notifications
You must be signed in to change notification settings - Fork 308
/
setup.ts
56 lines (45 loc) · 1.47 KB
/
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
import { ServerInstance } from '@chainlink/external-adapter-framework'
import { AddressInfo } from 'net'
import * as nock from 'nock'
import * as process from 'process'
import request, { SuperTest, Test } from 'supertest'
export type SuiteContext = {
req: SuperTest<Test> | null
server: () => Promise<ServerInstance>
fastify?: ServerInstance
}
export type EnvVariables = { [key: string]: string }
export type TestOptions = { cleanNock?: boolean; fastify?: boolean }
export const setupExternalAdapterTest = (
envVariables: NodeJS.ProcessEnv,
context: SuiteContext,
options: TestOptions = { cleanNock: true, fastify: false },
): void => {
let fastify: ServerInstance
beforeAll(async () => {
process.env['METRICS_ENABLED'] = 'false'
for (const key in envVariables) {
process.env[key] = envVariables[key]
}
if (process.env['RECORD']) {
nock.recorder.rec()
}
fastify = await context.server()
// eslint-disable-next-line require-atomic-updates
context.req = request(`localhost:${(fastify.server.address() as AddressInfo).port}`)
// Only for edge cases when someone needs to use the fastify instance outside this function
if (options.fastify) {
// eslint-disable-next-line require-atomic-updates
context.fastify = fastify
}
})
afterAll(async () => {
if (process.env['RECORD']) {
nock.recorder.play()
}
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
await fastify.close()
})
}