-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestHelpers.ts
37 lines (30 loc) · 932 Bytes
/
testHelpers.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
import type { FastifyInstance, LightMyRequestResponse } from 'fastify';
import { test } from 'uvu';
import build from './src/app';
interface TestContext {
request: (
query: string,
variables?: Record<string, string | number>,
) => Promise<LightMyRequestResponse | undefined>;
}
export function createTestContext(): TestContext {
const ctx = {} as TestContext;
let serverInstance: FastifyInstance | null = null;
test.before(async () => {
serverInstance = await build({ logger: false });
});
test.before.each(async (meta) => {
console.log(meta.__test__);
async function request(query: string, variables = {} as Record<string, string>) {
return serverInstance?.inject({
method: 'POST',
url: 'graphql',
headers: { 'content-type': 'application/json' },
payload: { query, variables },
});
}
Object.assign(ctx, { request });
});
test.after(() => serverInstance?.close());
return ctx;
}