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, deleteAllRecordings,
deleteAllRooms, deleteAllRooms,
startTestServer, startTestServer,
stopRecording, stopRecording
stopTestServer
} from '../../../utils/helpers'; } from '../../../utils/helpers';
import { setupMultiRecordingsTestContext } from '../../../utils/test-scenarios'; import { setupMultiRecordingsTestContext } from '../../../utils/test-scenarios';
import { expectValidationError } from '../../../utils/assertion-helpers'; import { expectValidationError } from '../../../utils/assertion-helpers';
describe('Recording API Tests', () => { describe('Recording API Tests', () => {
beforeAll(async () => { beforeAll(async () => {
await startTestServer(); startTestServer();
}); });
afterAll(async () => { afterAll(async () => {
await deleteAllRooms(); await deleteAllRooms();
await deleteAllRecordings(); await deleteAllRecordings();
await stopTestServer();
}); });
describe('Bulk Delete Recording Tests', () => { describe('Bulk Delete Recording Tests', () => {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,19 +1,20 @@
import { describe, it, expect, beforeAll, afterAll, afterEach } from '@jest/globals'; 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 ms from 'ms';
import { import {
expectSuccessRoomResponse, expectSuccessRoomResponse,
expectValidationError,
expectValidRoom, expectValidRoom,
expectValidRoomWithFields expectValidRoomWithFields
} from '../../../utils/assertion-helpers.js'; } from '../../../utils/assertion-helpers.js';
describe('Room API Tests', () => { describe('Room API Tests', () => {
beforeAll(async () => { beforeAll(async () => {
await startTestServer(); startTestServer();
}); });
afterAll(async () => { afterAll(async () => {
await stopTestServer();
}); });
afterEach(async () => { afterEach(async () => {
@ -100,10 +101,7 @@ describe('Room API Tests', () => {
it('should fail when roomId becomes empty after sanitization', async () => { it('should fail when roomId becomes empty after sanitization', async () => {
const response = await getRoom('!!-*!@#$%^&*()_+{}|:"<>?'); const response = await getRoom('!!-*!@#$%^&*()_+{}|:"<>?');
expect(response.status).toBe(422); expectValidationError(response, 'roomId', 'cannot be empty after sanitization');
// 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');
}); });
}); });
}); });

View File

@ -1,5 +1,5 @@
import { describe, it, beforeAll, afterAll, afterEach } from '@jest/globals'; 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 ms from 'ms';
import { import {
expectSuccessRoomsResponse, expectSuccessRoomsResponse,
@ -13,11 +13,11 @@ describe('Room API Tests', () => {
const validAutoDeletionDate = Date.now() + ms('2h'); const validAutoDeletionDate = Date.now() + ms('2h');
beforeAll(async () => { beforeAll(async () => {
await startTestServer(); startTestServer();
}); });
afterAll(async () => { afterAll(async () => {
await stopTestServer();
}); });
afterEach(async () => { afterEach(async () => {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,10 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import request, { Response } from 'supertest'; import request, { Response } from 'supertest';
import { Express } from 'express'; import { Express } from 'express';
import { Server } from 'http';
import { createApp, registerDependencies } from '../../src/server.js'; import { createApp, registerDependencies } from '../../src/server.js';
import { import {
SERVER_PORT,
MEET_API_KEY, MEET_API_KEY,
MEET_USER, MEET_USER,
MEET_SECRET, MEET_SECRET,
@ -35,8 +33,6 @@ const CREDENTIALS = {
}; };
let app: Express; let app: Express;
let server: Server;
const fakeParticipantsProcesses = new Map<string, ChildProcess>(); const fakeParticipantsProcesses = new Map<string, ChildProcess>();
export const sleep = (time: StringValue) => { export const sleep = (time: StringValue) => {
@ -46,56 +42,17 @@ export const sleep = (time: StringValue) => {
/** /**
* Starts the test server * Starts the test server
*/ */
export const startTestServer = async (): Promise<Express> => { export const startTestServer = (): Express => {
registerDependencies(); if (app) {
app = createApp(); return app;
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');
} }
return new Promise<void>((resolve, reject) => { registerDependencies();
server.close((err) => { app = createApp();
if (err) { return app;
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;
});
});
}; };
/** /**
* Updates global security preferences * Updates global security preferences
*/ */