Skip to content

Commit 1b255be

Browse files
author
Lasim
committed
test: refactor console logging in deleteDbConfig tests for clarity and consistency
1 parent 5c6345b commit 1b255be

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

services/backend/tests/unit/db/config.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,22 @@ describe('Database Configuration', () => {
134134
});
135135

136136
describe('deleteDbConfig', () => {
137-
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
138-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
139-
140-
beforeEach(() => {
141-
consoleLogSpy.mockClear();
142-
consoleErrorSpy.mockClear();
143-
})
144-
145137
it('should delete the configuration file successfully', async () => {
146138
process.env.NODE_ENV = 'development'; // Set to non-test mode to enable logging
139+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
147140
mockUnlink.mockResolvedValue(undefined);
148141

149142
await deleteDbConfig();
150143
expect(mockUnlink).toHaveBeenCalledWith(TEST_CONFIG_FILE_PATH);
151144
expect(consoleLogSpy).toHaveBeenCalledWith(`[INFO] Database configuration deleted from ${TEST_CONFIG_FILE_PATH}`);
145+
146+
consoleLogSpy.mockRestore();
152147
});
153148

154149
it('should log info and not throw if the file does not exist (ENOENT)', async () => {
155150
process.env.NODE_ENV = 'development'; // Set to non-test mode to enable logging
151+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
152+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
156153
const error = new Error('File not found') as NodeJS.ErrnoException;
157154
error.code = 'ENOENT';
158155
mockUnlink.mockRejectedValue(error);
@@ -161,16 +158,24 @@ describe('Database Configuration', () => {
161158
expect(mockUnlink).toHaveBeenCalledWith(TEST_CONFIG_FILE_PATH);
162159
expect(consoleLogSpy).toHaveBeenCalledWith('[INFO] Database configuration file not found, nothing to delete.');
163160
expect(consoleErrorSpy).not.toHaveBeenCalled(); // No error should be logged
161+
162+
consoleLogSpy.mockRestore();
163+
consoleErrorSpy.mockRestore();
164164
});
165165

166166
it('should log an error and re-throw for other unlink errors', async () => {
167+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
168+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
167169
const error = new Error('Delete permission denied');
168170
mockUnlink.mockRejectedValue(error);
169171

170172
await expect(deleteDbConfig()).rejects.toThrow(error);
171173
expect(mockUnlink).toHaveBeenCalledWith(TEST_CONFIG_FILE_PATH);
172174
expect(consoleErrorSpy).toHaveBeenCalledWith('[ERROR] Failed to delete database configuration:', error);
173175
expect(consoleLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('nothing to delete'));
176+
177+
consoleLogSpy.mockRestore();
178+
consoleErrorSpy.mockRestore();
174179
});
175180
});
176181

0 commit comments

Comments
 (0)