Skip to content

Commit 6aacf5a

Browse files
committed
fix(tests): ensure proper cleanup of async operations and resources
1 parent 6eea4e1 commit 6aacf5a

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

tests/index.test.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const mysql = require('mysql2');
22
const bcrypt = require('bcryptjs');
33
const ErrsoleMySQL = require('../lib/index'); // Adjust the path as needed
4-
const { describe } = require('@jest/globals');
5-
6-
/* globals expect, jest, beforeEach, it, afterEach */
4+
const cron = require('node-cron');
5+
/* globals expect, jest, beforeEach, it, afterEach, describe, afterAll */
76

87
jest.mock('mysql2', () => ({
98
createPool: jest.fn()
@@ -19,6 +18,7 @@ describe('ErrsoleMySQL', () => {
1918
let poolMock;
2019
let connectionMock;
2120
let originalConsoleError;
21+
let cronJob;
2222

2323
beforeEach(() => {
2424
connectionMock = {
@@ -42,16 +42,30 @@ describe('ErrsoleMySQL', () => {
4242
queueLimit: 0
4343
});
4444

45+
// Mock setInterval and cron.schedule
46+
jest.useFakeTimers();
47+
jest.spyOn(global, 'setInterval');
48+
cronJob = { stop: jest.fn() };
49+
jest.spyOn(cron, 'schedule').mockReturnValue(cronJob);
50+
4551
// Suppress console.error
4652
originalConsoleError = console.error;
4753
console.error = jest.fn();
4854
});
4955

5056
afterEach(() => {
5157
jest.clearAllMocks();
52-
58+
jest.useRealTimers();
5359
// Restore console.error
5460
console.error = originalConsoleError;
61+
// Clear the interval if it was set
62+
if (errsoleMySQL.flushIntervalId) {
63+
clearInterval(errsoleMySQL.flushIntervalId);
64+
}
65+
// Stop the cron job
66+
if (cronJob) {
67+
cronJob.stop();
68+
}
5569
});
5670

5771
describe('#initialize', () => {
@@ -70,6 +84,8 @@ describe('ErrsoleMySQL', () => {
7084
expect(poolMock.getConnection).toHaveBeenCalled();
7185
expect(poolMock.query).toHaveBeenCalledWith(expect.any(String), expect.any(Function));
7286
expect(errsoleMySQL.isConnectionInProgress).toBe(false);
87+
expect(setInterval).toHaveBeenCalled();
88+
expect(cron.schedule).toHaveBeenCalled();
7389
});
7490
});
7591

@@ -121,7 +137,6 @@ describe('ErrsoleMySQL', () => {
121137
expect(poolMock.query).not.toHaveBeenCalledWith('SET SESSION sort_buffer_size = 8388608', expect.any(Function));
122138
});
123139
});
124-
125140
describe('#createTables', () => {
126141
it('should create tables if they do not exist', async () => {
127142
poolMock.query.mockImplementation((query, cb) => cb(null, { affectedRows: 1 }));
@@ -900,4 +915,14 @@ describe('ErrsoleMySQL', () => {
900915
expect(errsoleMySQL.deleteExpiredLogsRunning).toBe(false);
901916
});
902917
});
918+
919+
afterAll(() => {
920+
// Ensure to clear any remaining intervals and cron jobs
921+
if (errsoleMySQL.flushIntervalId) {
922+
clearInterval(errsoleMySQL.flushIntervalId);
923+
}
924+
if (cronJob) {
925+
cronJob.stop();
926+
}
927+
});
903928
});

0 commit comments

Comments
 (0)