From 150b4c6f0fc9a3a302a007bdcd3af26529c9670d Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 11 Oct 2024 11:40:10 +0800 Subject: [PATCH] Add missing tests for 100% coverage --- Server/.mocharc.js | 2 +- Server/controllers/checkController.js | 1 + .../tests/controllers/checkController.test.js | 24 ++++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Server/.mocharc.js b/Server/.mocharc.js index 87f578c2e..5c8840544 100644 --- a/Server/.mocharc.js +++ b/Server/.mocharc.js @@ -1,6 +1,6 @@ module.exports = { require: ["chai/register-expect.js"], // Include Chai's "expect" interface globally - // spec: "tests/**/*.test.js", // Specify test files + spec: "tests/**/*.test.js", // Specify test files timeout: 5000, // Set test-case timeout in milliseconds recursive: true, // Include subdirectories reporter: "spec", // Use the "spec" reporter diff --git a/Server/controllers/checkController.js b/Server/controllers/checkController.js index b0e98ed38..be09d795b 100644 --- a/Server/controllers/checkController.js +++ b/Server/controllers/checkController.js @@ -31,6 +31,7 @@ const createCheck = async (req, res, next) => { .status(200) .json({ success: true, msg: successMessages.CHECK_CREATE, data: check }); } catch (error) { + console.log(error); next(handleError(error, SERVICE_NAME, "createCheck")); } }; diff --git a/Server/tests/controllers/checkController.test.js b/Server/tests/controllers/checkController.test.js index d71bd807c..a3060d60a 100644 --- a/Server/tests/controllers/checkController.test.js +++ b/Server/tests/controllers/checkController.test.js @@ -43,7 +43,25 @@ describe("Check Controller - createCheck", () => { }; await createCheck(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); + expect(next.firstCall.args[0].status).to.equal(422); }); + + it("should call next with error if data retrieval fails", async () => { + req.params = { + monitorId: "monitorId", + }; + req.body = { + monitorId: "monitorId", + status: true, + responseTime: 100, + statusCode: 200, + message: "message", + }; + req.db.createCheck.rejects(new Error("error")); + await createCheck(req, res, next); + expect(next.firstCall.args[0]).to.be.an("error"); + }); + it("should return a success message if check is created", async () => { req.params = { monitorId: "monitorId", @@ -115,12 +133,12 @@ describe("Check Controller - getChecks", () => { }); it("should call next with error if data retrieval fails", async () => { + req.params = { + monitorId: "monitorId", + }; req.db.getChecks.rejects(new Error("error")); await getChecks(req, res, next); expect(next.firstCall.args[0]).to.be.an("error"); - req.db.getChecks.resolves([]); - req.db.getChecksCount.rejects(new Error("error")); - await getChecks(req, res, next); }); });