Skip to content

Commit 73e5297

Browse files
committed
Fix Database and IndexController tests
1 parent df80359 commit 73e5297

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Diff for: tests/controllers/IndexController.spec.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { IndexController } from '../../src/controllers/IndexController';
2-
import { Request, Response } from 'express';
2+
import { NextFunction, Request, Response } from 'express';
33

44
describe('IndexController', () => {
55
it('should respond as expected', () => {
66
const request = {};
77
const response = {
88
json: jest.fn(),
99
};
10+
const next: NextFunction = jest.fn();
1011

11-
IndexController(request as Request, response as unknown as Response);
12+
IndexController(request as Request, response as unknown as Response, next);
1213

13-
expect(response.json).toBeCalledWith(['ok']);
14+
expect(response.json).toBeCalledWith(
15+
expect.objectContaining({
16+
status: 200,
17+
}),
18+
);
1419
});
1520
});

Diff for: tests/database/index.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
import { AppDataSource, initialiseDataSource, destroyDataSource } from '../../src/database';
22

33
describe('DataSource Utility', () => {
4+
let initCount: number = 0;
45
const originalConsoleError = console.error;
56

67
beforeEach(async () => {
78
console.error = jest.fn();
89

10+
jest.spyOn(AppDataSource, 'initialize').mockImplementation(async () => {
11+
initCount++;
12+
if (initCount > 1) {
13+
throw new Error('initialize called more than once before destroying.');
14+
}
15+
Object.defineProperty(AppDataSource, 'isInitialized', {
16+
value: true,
17+
writable: true,
18+
configurable: true,
19+
});
20+
return AppDataSource;
21+
});
22+
23+
jest.spyOn(AppDataSource, 'destroy').mockImplementation(async () => {
24+
initCount = 0;
25+
Object.defineProperty(AppDataSource, 'isInitialized', {
26+
value: false,
27+
writable: true,
28+
configurable: true,
29+
});
30+
});
31+
932
if (!AppDataSource.isInitialized) {
1033
await initialiseDataSource();
1134
}

0 commit comments

Comments
 (0)