test(api): always start server before testing (#49994)

This commit is contained in:
Oliver Eyton-Williams
2023-04-17 17:54:50 +07:00
committed by GitHub
parent d23676ad86
commit 6289efbbad
3 changed files with 24 additions and 20 deletions
+2 -1
View File
@@ -5,7 +5,8 @@ const config: Config = {
testRegex: '\\.test\\.ts$',
transform: {
'^.+\\.ts$': 'ts-jest'
}
},
setupFilesAfterEnv: ['<rootDir>/jest.start-server.ts']
};
export default config;
+18
View File
@@ -0,0 +1,18 @@
import { build } from './src/app';
declare global {
// eslint-disable-next-line no-var
var fastifyTestInstance: Awaited<ReturnType<typeof build>>;
}
beforeAll(async () => {
const fastify = await build();
await fastify.ready();
global.fastifyTestInstance = fastify;
});
afterAll(async () => {
// Due to a prisma bug, this is not enough, we need to --force-exit jest:
// https://github.com/prisma/prisma/issues/18146
await fastifyTestInstance?.close();
});
+4 -19
View File
@@ -1,28 +1,13 @@
import request, { Response } from 'supertest';
import { build } from './app';
import request from 'supertest';
describe('GET /', () => {
let res: undefined | Response;
let fastify: undefined | Awaited<ReturnType<typeof build>>;
beforeAll(async () => {
fastify = await build();
await fastify.ready();
}, 20000);
afterAll(async () => {
// Due to a prisma bug, this is not enough, we need to --force-exit jest:
// https://github.com/prisma/prisma/issues/18146
await fastify?.close();
});
test('have a 200 response', async () => {
res = await request(fastify?.server).get('/');
const res = await request(fastifyTestInstance?.server).get('/');
expect(res?.statusCode).toBe(200);
});
test('return { "hello": "world"}', () => {
test('return { "hello": "world"}', async () => {
const res = await request(fastifyTestInstance?.server).get('/');
expect(res?.body).toEqual({ hello: 'world' });
});