fix: await tests longer

This commit is contained in:
Anton Voylenko 2024-12-03 01:20:55 +02:00
commit 13daf31868
2 changed files with 25 additions and 17 deletions

View file

@ -22,4 +22,4 @@ jobs:
run: npm ci run: npm ci
- name: Run tests - name: Run tests
run: npm test run: npm test
timeout-minutes: 1 timeout-minutes: 5

View file

@ -10,12 +10,15 @@ process.env.BASE_WEBHOOK_URL = 'http://localhost:3000/localCallbackExample'
const app = require('../src/app') const app = require('../src/app')
jest.mock('qrcode-terminal') jest.mock('qrcode-terminal')
jest.setTimeout(5 * 60 * 1000)
let server let server
beforeAll(() => { beforeAll(() => {
fs.rmSync(process.env.SESSIONS_PATH, { recursive: true, force: true })
server = app.listen(3000) server = app.listen(3000)
}) })
beforeEach(async () => { beforeEach(() => {
if (fs.existsSync('./sessions_test/message_log.txt')) { if (fs.existsSync('./sessions_test/message_log.txt')) {
fs.writeFileSync('./sessions_test/message_log.txt', '') fs.writeFileSync('./sessions_test/message_log.txt', '')
} }
@ -23,18 +26,18 @@ beforeEach(async () => {
afterAll(() => { afterAll(() => {
server.close() server.close()
fs.rmSync('./sessions_test', { recursive: true, force: true }) fs.rmSync(process.env.SESSIONS_PATH, { recursive: true, force: true })
}) })
// Define test cases // Define test cases
describe('API health checks', () => { describe('API health checks', () => {
it('should return valid healthcheck', async () => { it('should return valid health check', async () => {
const response = await request(app).get('/ping') const response = await request(app).get('/ping')
expect(response.status).toBe(200) expect(response.status).toBe(200)
expect(response.body).toEqual({ message: 'pong', success: true }) expect(response.body).toEqual({ message: 'pong', success: true })
}) })
it('should return a valid callback', async () => { it('should return a valid callback status', async () => {
const response = await request(app).post('/localCallbackExample') const response = await request(app).post('/localCallbackExample')
.set('x-api-key', 'test_api_key') .set('x-api-key', 'test_api_key')
.send({ sessionId: '1', dataType: 'testDataType', data: 'testData' }) .send({ sessionId: '1', dataType: 'testDataType', data: 'testData' })
@ -70,7 +73,7 @@ describe('API Authentication Tests', () => {
expect(response2.body).toEqual({ success: true, message: 'Logged out successfully' }) expect(response2.body).toEqual({ success: true, message: 'Logged out successfully' })
expect(fs.existsSync('./sessions_test/session-1')).toBe(false) expect(fs.existsSync('./sessions_test/session-1')).toBe(false)
}, 10000) })
it('should setup and flush multiple client sessions', async () => { it('should setup and flush multiple client sessions', async () => {
const response = await request(app).get('/session/start/2').set('x-api-key', 'test_api_key') const response = await request(app).get('/session/start/2').set('x-api-key', 'test_api_key')
@ -89,7 +92,7 @@ describe('API Authentication Tests', () => {
expect(fs.existsSync('./sessions_test/session-2')).toBe(false) expect(fs.existsSync('./sessions_test/session-2')).toBe(false)
expect(fs.existsSync('./sessions_test/session-3')).toBe(false) expect(fs.existsSync('./sessions_test/session-3')).toBe(false)
}, 10000) })
}) })
describe('API Action Tests', () => { describe('API Action Tests', () => {
@ -100,7 +103,7 @@ describe('API Action Tests', () => {
expect(fs.existsSync('./sessions_test/session-4')).toBe(true) expect(fs.existsSync('./sessions_test/session-4')).toBe(true)
// Wait for message_log.txt to not be empty // Wait for message_log.txt to not be empty
const result = await waitForFileNotToBeEmpty('./sessions_test/message_log.txt') const result = await waitForFileNotToBeEmpty('./sessions_test/message_log.txt', 120_000, 1000)
.then(() => { return true }) .then(() => { return true })
.catch(() => { return false }) .catch(() => { return false })
expect(result).toBe(true) expect(result).toBe(true)
@ -117,24 +120,29 @@ describe('API Action Tests', () => {
expect(response2.status).toBe(200) expect(response2.status).toBe(200)
expect(response2.body).toEqual({ success: true, message: 'Logged out successfully' }) expect(response2.body).toEqual({ success: true, message: 'Logged out successfully' })
expect(fs.existsSync('./sessions_test/session-4')).toBe(false) expect(fs.existsSync('./sessions_test/session-4')).toBe(false)
}, 15000) })
}) })
// Function to wait for a specific item to be equal a specific value // Function to wait for a specific item to be equal a specific value
const waitForFileNotToBeEmpty = (filePath, maxWaitTime = 10000, interval = 100) => { const waitForFileNotToBeEmpty = (filePath, maxWaitTime = 10000, interval = 100) => {
const start = Date.now() const start = Date.now()
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const checkObject = () => { const checkObject = async () => {
const filecontent = fs.readFileSync(filePath, 'utf-8') try {
if (filecontent !== '') { const filecontent = await fs.promises.readFile(filePath, 'utf-8')
if (filecontent !== '') {
// Nested object exists, resolve the promise // Nested object exists, resolve the promise
resolve() resolve()
} else if (Date.now() - start > maxWaitTime) { } else if (Date.now() - start > maxWaitTime) {
// Maximum wait time exceeded, reject the promise // Maximum wait time exceeded, reject the promise
console.log('Timed out waiting for nested object') console.log('Timed out waiting for nested object')
reject(new Error('Timeout waiting for nested object')) reject(new Error('Timeout waiting for nested object'))
} else { } else {
// Nested object not yet created, continue waiting // Nested object not yet created, continue waiting
setTimeout(checkObject, interval)
}
} catch (ignore) {
// continue waiting
setTimeout(checkObject, interval) setTimeout(checkObject, interval)
} }
} }