test: streamline test server initialization by removing unnecessary await and stopTestServer calls

This commit is contained in:
Carlos Santos 2025-04-23 13:52:25 +02:00
parent db44b43022
commit e0a74b4446
15 changed files with 53 additions and 127 deletions

View File

@ -4,21 +4,19 @@ import {
deleteAllRecordings,
deleteAllRooms,
startTestServer,
stopRecording,
stopTestServer
stopRecording
} from '../../../utils/helpers';
import { setupMultiRecordingsTestContext } from '../../../utils/test-scenarios';
import { expectValidationError } from '../../../utils/assertion-helpers';
describe('Recording API Tests', () => {
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await deleteAllRooms();
await deleteAllRecordings();
await stopTestServer();
});
describe('Bulk Delete Recording Tests', () => {

View File

@ -7,13 +7,13 @@ import {
startRecording,
startTestServer,
stopAllRecordings,
stopRecording,
stopTestServer
stopRecording
} from '../../../utils/helpers.js';
import { setInternalConfig } from '../../../../src/config/internal-config.js';
import { errorRoomNotFound } from '../../../../src/models/error.model.js';
import {
expectValidationError,
expectValidRecordingLocationHeader,
expectValidStartRecordingResponse,
expectValidStopRecordingResponse
@ -26,7 +26,7 @@ describe('Recording API Tests', () => {
let room: MeetRoom, moderatorCookie: string;
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
@ -34,7 +34,6 @@ describe('Recording API Tests', () => {
await disconnectFakeParticipants();
await deleteAllRooms();
await deleteAllRecordings();
await stopTestServer();
});
describe('Start Recording Tests', () => {
@ -133,44 +132,19 @@ describe('Recording API Tests', () => {
it('should reject request with roomId that becomes empty after sanitization', async () => {
const response = await startRecording('!@#$%^&*()', moderatorCookie);
expect(response.status).toBe(422);
expect(response.body.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
field: 'roomId',
message: expect.stringContaining('cannot be empty after sanitization')
})
])
);
expectValidationError(response, 'roomId', 'cannot be empty after sanitization');
});
it('should reject request with non-string roomId', async () => {
const response = await startRecording(123 as unknown as string, moderatorCookie);
expect(response.status).toBe(422);
expect(response.body.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
field: 'roomId',
message: expect.stringContaining('Expected string')
})
])
);
expectValidationError(response, 'roomId', 'Expected string');
});
it('should reject request with very long roomId', async () => {
const longRoomId = 'a'.repeat(101);
const response = await startRecording(longRoomId, moderatorCookie);
expect(response.status).toBe(422);
expect(response.body.details).toEqual(
expect.arrayContaining([
expect.objectContaining({
field: 'roomId',
message: expect.stringContaining('cannot exceed 100 characters')
})
])
);
expectValidationError(response, 'roomId', 'cannot exceed 100 characters');
});
it('should handle room that does not exist', async () => {

View File

@ -3,7 +3,7 @@ import {
createRoom,
deleteAllRooms,
startTestServer,
stopTestServer,
,
getRoom,
joinFakeParticipant,
disconnectFakeParticipants,
@ -12,11 +12,11 @@ import {
describe('Room API Tests', () => {
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {

View File

@ -6,7 +6,7 @@ import {
deleteAllRooms,
loginUserAsRole,
startTestServer,
stopTestServer
} from '../../../utils/helpers.js';
import { UserRole } from '../../../../src/typings/ce/user.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
@ -22,13 +22,13 @@ describe('Room API Tests', () => {
let userCookie: string;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
userCookie = await loginUserAsRole(UserRole.USER);
});
afterAll(async () => {
await deleteAllRooms();
await stopTestServer();
});
describe('Room Creation Tests', () => {

View File

@ -3,22 +3,21 @@ import {
createRoom,
deleteAllRooms,
startTestServer,
stopTestServer,
,
getRoom,
deleteRoom,
joinFakeParticipant,
sleep,
disconnectFakeParticipants
} from '../../../utils/helpers.js';
import ms from 'ms';
describe('Room API Tests', () => {
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {

View File

@ -3,7 +3,7 @@ import {
createRoom,
deleteAllRooms,
startTestServer,
stopTestServer,
,
getRoom,
sleep,
joinFakeParticipant,
@ -19,11 +19,11 @@ describe('Room Garbage Collector Tests', () => {
setInternalConfig({
MIN_FUTURE_TIME_FOR_ROOM_AUTODELETION_DATE: '0s'
});
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {

View File

@ -1,19 +1,20 @@
import { describe, it, expect, beforeAll, afterAll, afterEach } from '@jest/globals';
import { createRoom, deleteAllRooms, startTestServer, stopTestServer, getRoom } from '../../../utils/helpers.js';
import { createRoom, deleteAllRooms, startTestServer, , getRoom } from '../../../utils/helpers.js';
import ms from 'ms';
import {
expectSuccessRoomResponse,
expectValidationError,
expectValidRoom,
expectValidRoomWithFields
} from '../../../utils/assertion-helpers.js';
describe('Room API Tests', () => {
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {
@ -100,10 +101,7 @@ describe('Room API Tests', () => {
it('should fail when roomId becomes empty after sanitization', async () => {
const response = await getRoom('!!-*!@#$%^&*()_+{}|:"<>?');
expect(response.status).toBe(422);
// Expect an error message indicating the resulting roomId is empty.
expect(response.body.error).toContain('Unprocessable Entity');
expect(JSON.stringify(response.body.details)).toContain('roomId cannot be empty after sanitization');
expectValidationError(response, 'roomId', 'cannot be empty after sanitization');
});
});
});

View File

@ -1,5 +1,5 @@
import { describe, it, beforeAll, afterAll, afterEach } from '@jest/globals';
import { createRoom, deleteAllRooms, getRooms, startTestServer, stopTestServer } from '../../../utils/helpers.js';
import { createRoom, deleteAllRooms, getRooms, startTestServer, } from '../../../utils/helpers.js';
import ms from 'ms';
import {
expectSuccessRoomsResponse,
@ -13,11 +13,11 @@ describe('Room API Tests', () => {
const validAutoDeletionDate = Date.now() + ms('2h');
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {

View File

@ -3,18 +3,18 @@ import {
createRoom,
deleteAllRooms,
startTestServer,
stopTestServer,
,
getRoom,
updateRoomPreferences
} from '../../../utils/helpers.js';
describe('Room API Tests', () => {
beforeAll(async () => {
await startTestServer();
startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
afterEach(async () => {

View File

@ -1,7 +1,7 @@
import request from 'supertest';
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
import { Express } from 'express';
import { loginUserAsRole, startTestServer, stopTestServer } from '../../../utils/helpers.js';
import { loginUserAsRole, startTestServer, } from '../../../utils/helpers.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
import { UserRole } from '../../../../src/typings/ce/index.js';
@ -11,11 +11,11 @@ describe('Authentication API Tests', () => {
let app: Express;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
});
afterAll(async () => {
await stopTestServer();
});
describe('Login Tests', () => {

View File

@ -1,7 +1,7 @@
import request from 'supertest';
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
import { Express } from 'express';
import { createRoom, generateParticipantToken, startTestServer, stopTestServer } from '../../../utils/helpers.js';
import { createRoom, generateParticipantToken, startTestServer, } from '../../../utils/helpers.js';
import { UserRole } from '../../../../src/typings/ce/index.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
import { MeetRoomHelper } from '../../../../src/helpers/room.helper.js';
@ -18,7 +18,7 @@ describe('Meeting API Security Tests', () => {
let publisherCookie: string;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
// Get cookie for admin
adminCookie = await loginUserAsRole(UserRole.ADMIN);
@ -35,7 +35,7 @@ describe('Meeting API Security Tests', () => {
afterAll(async () => {
await deleteAllRooms();
await stopTestServer();
}, 20000);
describe('End Meeting Tests', () => {

View File

@ -1,7 +1,7 @@
import request from 'supertest';
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
import { Express } from 'express';
import { createRoom, startTestServer, stopTestServer } from '../../../utils/helpers.js';
import { createRoom, startTestServer, } from '../../../utils/helpers.js';
import { AuthMode, UserRole } from '../../../../src/typings/ce/index.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
import { MeetRoomHelper } from '../../../../src/helpers/room.helper.js';
@ -22,7 +22,7 @@ describe('Participant API Security Tests', () => {
let publisherSecret: string;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
// Get cookies for admin and user
userCookie = await loginUserAsRole(UserRole.USER);
@ -38,7 +38,7 @@ describe('Participant API Security Tests', () => {
afterAll(async () => {
await deleteAllRooms();
await stopTestServer();
}, 20000);
describe('Generate Participant Token Tests', () => {

View File

@ -7,7 +7,7 @@ import {
generateParticipantToken,
loginUserAsRole,
startTestServer,
stopTestServer
} from '../../../utils/helpers.js';
import { MEET_API_KEY } from '../../../../src/environment.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
@ -30,7 +30,7 @@ describe('Recording API Security Tests', () => {
let publisherCookie: string;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
// Get cookies for admin and user
userCookie = await loginUserAsRole(UserRole.USER);
@ -49,7 +49,7 @@ describe('Recording API Security Tests', () => {
afterAll(async () => {
await deleteAllRooms();
await stopTestServer();
}, 20000);
describe('Start Recording Tests', () => {

View File

@ -1,7 +1,7 @@
import request from 'supertest';
import { describe, it, expect, beforeAll, beforeEach, afterAll } from '@jest/globals';
import { Express } from 'express';
import { createRoom, generateParticipantToken, startTestServer, stopTestServer } from '../../../utils/helpers.js';
import { createRoom, generateParticipantToken, startTestServer, } from '../../../utils/helpers.js';
import { AuthMode, UserRole } from '../../../../src/typings/ce/index.js';
import { MEET_API_KEY } from '../../../../src/environment.js';
import INTERNAL_CONFIG from '../../../../src/config/internal-config.js';
@ -17,7 +17,7 @@ describe('Room API Security Tests', () => {
let adminCookie: string;
beforeAll(async () => {
app = await startTestServer();
app = startTestServer();
// Get cookies for admin and user
userCookie = await loginUserAsRole(UserRole.USER);
@ -26,7 +26,7 @@ describe('Room API Security Tests', () => {
afterAll(async () => {
await deleteAllRooms();
await stopTestServer();
}, 20000);
describe('Create Room Tests', () => {

View File

@ -1,10 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import request, { Response } from 'supertest';
import { Express } from 'express';
import { Server } from 'http';
import { createApp, registerDependencies } from '../../src/server.js';
import {
SERVER_PORT,
MEET_API_KEY,
MEET_USER,
MEET_SECRET,
@ -35,8 +33,6 @@ const CREDENTIALS = {
};
let app: Express;
let server: Server;
const fakeParticipantsProcesses = new Map<string, ChildProcess>();
export const sleep = (time: StringValue) => {
@ -46,56 +42,17 @@ export const sleep = (time: StringValue) => {
/**
* Starts the test server
*/
export const startTestServer = async (): Promise<Express> => {
registerDependencies();
app = createApp();
return await new Promise<Express>((resolve, reject) => {
server = app.listen(SERVER_PORT, async () => {
try {
// Check if the server is responding by hitting the health check route
const response = await request(app).get('/meet/health');
if (response.status === 200) {
console.log('Test server started and healthy!');
resolve(app);
} else {
reject(new Error('Test server not healthy'));
}
} catch (error: any) {
reject(new Error('Failed to initialize server or global preferences: ' + error.message));
}
});
// Handle server errors
server.on('error', (error: any) => reject(new Error(`Test server startup error: ${error.message}`)));
});
};
/**
* Stops the test server
*/
export const stopTestServer = async (): Promise<void> => {
if (!server) {
throw new Error('Server is not running');
export const startTestServer = (): Express => {
if (app) {
return app;
}
return new Promise<void>((resolve, reject) => {
server.close((err) => {
if (err) {
reject(new Error(`Failed to stop server: ${err.message}`));
} else {
console.log('Test server stopped.');
resolve();
}
// Clear the app instance
app = undefined as unknown as Express;
server = undefined as unknown as Server;
});
});
registerDependencies();
app = createApp();
return app;
};
/**
* Updates global security preferences
*/