mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-09-24 23:01:18 +08:00
test(api): always start server before testing (#49994)
This commit is contained in:
+2
-1
@@ -5,7 +5,8 @@ const config: Config = {
|
||||
testRegex: '\\.test\\.ts$',
|
||||
transform: {
|
||||
'^.+\\.ts$': 'ts-jest'
|
||||
}
|
||||
},
|
||||
setupFilesAfterEnv: ['<rootDir>/jest.start-server.ts']
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
||||
@@ -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
@@ -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' });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user