From 6289efbbadd4845bd247b48929187556b261f982 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Mon, 17 Apr 2023 12:54:50 +0200 Subject: [PATCH] test(api): always start server before testing (#49994) --- api/jest.config.ts | 3 ++- api/jest.start-server.ts | 18 ++++++++++++++++++ api/src/server.test.ts | 23 ++++------------------- 3 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 api/jest.start-server.ts diff --git a/api/jest.config.ts b/api/jest.config.ts index 60394011495..bf1941a3a2b 100644 --- a/api/jest.config.ts +++ b/api/jest.config.ts @@ -5,7 +5,8 @@ const config: Config = { testRegex: '\\.test\\.ts$', transform: { '^.+\\.ts$': 'ts-jest' - } + }, + setupFilesAfterEnv: ['/jest.start-server.ts'] }; export default config; diff --git a/api/jest.start-server.ts b/api/jest.start-server.ts new file mode 100644 index 00000000000..7e2a58238d7 --- /dev/null +++ b/api/jest.start-server.ts @@ -0,0 +1,18 @@ +import { build } from './src/app'; + +declare global { + // eslint-disable-next-line no-var + var fastifyTestInstance: Awaited>; +} + +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(); +}); diff --git a/api/src/server.test.ts b/api/src/server.test.ts index 6837b59b220..84525770fc3 100644 --- a/api/src/server.test.ts +++ b/api/src/server.test.ts @@ -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>; - - 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' }); });