From 49720c709c977049d594bd2b17df23bdb56815d0 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Tue, 5 Oct 2021 19:13:57 +0800 Subject: [PATCH] improve the test with a single command only "npm test" --- package.json | 4 ++-- server/server.js | 17 ++++++++++------- server/util-server.js | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 04f70a053..7202f20de 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "start-server": "node server/server.js", "start-server-dev": "cross-env NODE_ENV=development node server/server.js", "build": "vite build", - "prepare-test-server": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/", - "test": "node test/prepare-jest.js && jest", + "test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test", + "jest": "node test/prepare-jest.js && jest", "tsc": "tsc", "vite-preview-dist": "vite preview --host", "build-docker": "npm run build-docker-debian && npm run build-docker-alpine", diff --git a/server/server.js b/server/server.js index 0fbe8325b..55448a79a 100644 --- a/server/server.js +++ b/server/server.js @@ -37,7 +37,7 @@ console.log("Importing this project modules"); debug("Importing Monitor"); const Monitor = require("./model/monitor"); debug("Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret, checkLogin } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, checkLogin, startUnitTest } = require("./util-server"); debug("Importing Notification"); const { Notification } = require("./notification"); @@ -64,12 +64,11 @@ const port = parseInt(process.env.PORT || args.port || 3001); const sslKey = process.env.SSL_KEY || args["ssl-key"] || undefined; const sslCert = process.env.SSL_CERT || args["ssl-cert"] || undefined; -// Demo Mode? -const demoMode = args["demo"] || false; - -if (demoMode) { - console.log("==== Demo Mode ===="); -} +/** + * Run unit test after the server is ready + * @type {boolean} + */ +const testMode = !!args["test"] || false; console.log("Creating express and socket.io instance"); const app = express(); @@ -1223,6 +1222,10 @@ exports.entryPage = "dashboard"; } startMonitors(); checkVersion.startInterval(); + + if (testMode) { + startUnitTest(); + } }); })(); diff --git a/server/util-server.js b/server/util-server.js index 66c50d8c2..4aa965875 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -5,6 +5,7 @@ const { debug } = require("../src/util"); const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); const { Resolver } = require("dns"); +const child_process = require("child_process"); /** * Init or reset JWT secret @@ -292,3 +293,22 @@ exports.checkLogin = (socket) => { throw new Error("You are not logged in."); } }; + +exports.startUnitTest = async () => { + console.log("Starting unit test..."); + const npm = /^win/.test(process.platform) ? "npm.cmd" : "npm"; + const child = child_process.spawn(npm, ["run", "jest"]); + + child.stdout.on("data", (data) => { + console.log(data.toString()); + }); + + child.stderr.on("data", (data) => { + console.log(data.toString()); + }); + + child.on("close", function (code) { + console.log("Jest exit code: " + code); + process.kill(process.pid, "SIGINT"); + }); +};