Fix tests
This commit is contained in:
1 parent
e0d4f0d0f6
commit
ef4ca52285
3 files changed
+47
-25
No files matched your search
@@ -3,18 +3,21 @@ const config = {
|
|||||||
preset: "ts-jest/presets/default-esm",
|
preset: "ts-jest/presets/default-esm",
|
||||||
testEnvironment: "node",
|
testEnvironment: "node",
|
||||||
rootDir: "./",
|
rootDir: "./",
|
||||||
roots: ["<rootDir>/tests"],
|
roots: ["<rootDir>/tests"],
|
||||||
testMatch: ["**/*.test.ts"],
|
testMatch: ["**/*.test.ts", "**/*.test.js"],
|
||||||
verbose: true,
|
verbose: true,
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
"^@/(.*)\\.js$": "<rootDir>/src/$1.ts",
|
"^@/(.*)\\.js$": "<rootDir>/src/$1.ts",
|
||||||
"^@/(.*)$": "<rootDir>/src/$1",
|
"^@/(.*)$": "<rootDir>/src/$1",
|
||||||
"^(\\.\\.?/.*)\\.js$": "$1"
|
"^(\\.\\.?/.*)\\.js$": "$1"
|
||||||
},
|
},
|
||||||
extensionsToTreatAsEsm: [".ts"],
|
extensionsToTreatAsEsm: [".ts"],
|
||||||
transform: {
|
transform: {
|
||||||
"^.+\\.(t|j)s$": ["ts-jest", { useESM: true, tsconfig: "./tsconfig.jest.json" }]
|
"^.+\\.(t|j)s$": ["ts-jest", { useESM: true, tsconfig: "./tsconfig.jest.json" }]
|
||||||
},
|
},
|
||||||
|
transformIgnorePatterns: [
|
||||||
|
"/node_modules/"
|
||||||
|
],
|
||||||
collectCoverageFrom: ["src/**/*.{ts,js}", "!src/**/*.d.ts"],
|
collectCoverageFrom: ["src/**/*.{ts,js}", "!src/**/*.d.ts"],
|
||||||
coverageDirectory: "coverage",
|
coverageDirectory: "coverage",
|
||||||
coverageReporters: ["text", "lcov"],
|
coverageReporters: ["text", "lcov"],
|
||||||
|
|||||||
@@ -151,24 +151,42 @@ describe("StatusService.updateMonitorStatus", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("StatusService.updateMonitorStats", () => {
|
describe("StatusService.updateMonitorStats", () => {
|
||||||
const service = new StatusService();
|
const service = new StatusService();
|
||||||
let findOneSpy;
|
let findOneSpy;
|
||||||
beforeEach(() => {
|
let createSpy;
|
||||||
findOneSpy = jest.spyOn(MonitorStats, "findOne");
|
beforeEach(() => {
|
||||||
jest.useFakeTimers();
|
findOneSpy = jest.spyOn(MonitorStats, "findOne");
|
||||||
});
|
createSpy = jest.spyOn(MonitorStats, "create");
|
||||||
afterEach(() => {
|
jest.useFakeTimers();
|
||||||
jest.useRealTimers();
|
});
|
||||||
jest.restoreAllMocks();
|
afterEach(() => {
|
||||||
});
|
jest.useRealTimers();
|
||||||
it("throws ApiError when monitor stats are missing", async () => {
|
jest.restoreAllMocks();
|
||||||
findOneSpy.mockResolvedValue(null);
|
});
|
||||||
const { monitor } = buildMonitor({});
|
it("creates MonitorStats when missing and returns saved stats", async () => {
|
||||||
const statusResponse = buildStatusResponse({ status: "up" });
|
const now = new Date("2024-02-01T00:00:00Z");
|
||||||
await expect(service.updateMonitorStats(monitor, statusResponse, false)).rejects.toBeInstanceOf(ApiError);
|
jest.setSystemTime(now);
|
||||||
await expect(service.updateMonitorStats(monitor, statusResponse, false)).rejects.toMatchObject({ message: "MonitorStats not found", status: 500 });
|
findOneSpy.mockResolvedValue(null);
|
||||||
expect(findOneSpy).toHaveBeenCalledWith({ monitorId: monitor._id });
|
const { monitor } = buildMonitor({});
|
||||||
|
const statusResponse = buildStatusResponse({ status: "up", responseTime: 123 });
|
||||||
|
const stats = buildStats({ totalChecks: 0, totalUpChecks: 0, totalDownChecks: 0 });
|
||||||
|
const saveMock = stats.save;
|
||||||
|
createSpy.mockResolvedValue(stats);
|
||||||
|
|
||||||
|
const result = await service.updateMonitorStats(monitor, statusResponse, false);
|
||||||
|
|
||||||
|
expect(findOneSpy).toHaveBeenCalledWith({ monitorId: monitor._id });
|
||||||
|
expect(createSpy).toHaveBeenCalledWith({
|
||||||
|
monitorId: monitor._id,
|
||||||
|
currentStreakStartedAt: expect.any(Number),
|
||||||
});
|
});
|
||||||
|
expect(saveMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(result).toBe(stats);
|
||||||
|
expect(stats.totalChecks).toBe(1);
|
||||||
|
expect(stats.totalUpChecks).toBe(1);
|
||||||
|
expect(stats.lastResponseTime).toBe(123);
|
||||||
|
expect(stats.lastCheckTimestamp).toBe(now.getTime());
|
||||||
|
});
|
||||||
it("increments counters and preserves streak when status unchanged", async () => {
|
it("increments counters and preserves streak when status unchanged", async () => {
|
||||||
const now = new Date("2024-02-01T12:00:00Z");
|
const now = new Date("2024-02-01T12:00:00Z");
|
||||||
jest.setSystemTime(now);
|
jest.setSystemTime(now);
|
||||||
|
|||||||
@@ -5,8 +5,9 @@
|
|||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"module": "esnext",
|
"module": "esnext",
|
||||||
"moduleResolution": "nodenext",
|
"moduleResolution": "nodenext",
|
||||||
"types": ["jest", "node"]
|
"types": ["jest", "node"],
|
||||||
|
"allowJs": true
|
||||||
},
|
},
|
||||||
"include": ["tests/**/*.ts", "src/**/*.ts"],
|
"include": ["tests/**/*.ts", "tests/**/*.js", "src/**/*.ts"],
|
||||||
"exclude": ["dist", "node_modules"]
|
"exclude": ["dist", "node_modules"]
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user