diff --git a/babel.config.js b/babel.config.js index 6bb8a01a5..d4c895475 100644 --- a/babel.config.js +++ b/babel.config.js @@ -4,8 +4,4 @@ if (process.env.TEST_FRONTEND) { config.presets = [ "@babel/preset-env" ]; } -if (process.env.TEST_BACKEND) { - config.plugins = [ "babel-plugin-rewire" ]; -} - module.exports = config; diff --git a/package-lock.json b/package-lock.json index da1524c79..a1b012418 100644 --- a/package-lock.json +++ b/package-lock.json @@ -90,7 +90,6 @@ "@vue/compiler-sfc": "~3.3.4", "@vuepic/vue-datepicker": "~3.4.8", "aedes": "^0.46.3", - "babel-plugin-rewire": "~1.2.0", "bootstrap": "5.1.3", "chart.js": "~4.2.1", "chartjs-adapter-dayjs-4": "~1.0.4", @@ -6623,12 +6622,6 @@ "@babel/core": "^7.4.0 || ^8.0.0-0 <8.0.0" } }, - "node_modules/babel-plugin-rewire": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/babel-plugin-rewire/-/babel-plugin-rewire-1.2.0.tgz", - "integrity": "sha512-JBZxczHw3tScS+djy6JPLMjblchGhLI89ep15H3SyjujIzlxo5nr6Yjo7AXotdeVczeBmWs0tF8PgJWDdgzAkQ==", - "dev": true - }, "node_modules/babel-preset-current-node-syntax": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/babel-preset-current-node-syntax/-/babel-preset-current-node-syntax-1.0.1.tgz", diff --git a/package.json b/package.json index 9f57e99b7..3a9056d6c 100644 --- a/package.json +++ b/package.json @@ -153,7 +153,6 @@ "@vue/compiler-sfc": "~3.3.4", "@vuepic/vue-datepicker": "~3.4.8", "aedes": "^0.46.3", - "babel-plugin-rewire": "~1.2.0", "bootstrap": "5.1.3", "chart.js": "~4.2.1", "chartjs-adapter-dayjs-4": "~1.0.4", diff --git a/server/util-server.js b/server/util-server.js index 8ab592ebc..985fddb63 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -1047,3 +1047,13 @@ module.exports.grpcQuery = async (options) => { }); }; + +// For unit test, export functions +if (process.env.TEST_BACKEND) { + module.exports.__test = { + parseCertificateInfo, + }; + module.exports.__getPrivateFunction = (functionName) => { + return module.exports.__test[functionName]; + }; +} diff --git a/test/backend.spec.js b/test/backend.spec.js index 0132fb770..bc5ee810c 100644 --- a/test/backend.spec.js +++ b/test/backend.spec.js @@ -1,5 +1,5 @@ const { genSecret, DOWN, log} = require("../src/util"); -const utilServerRewire = require("../server/util-server"); +const utilServer = require("../server/util-server"); const Discord = require("../server/notification-providers/discord"); const axios = require("axios"); const { UptimeKumaServer } = require("../server/uptime-kuma-server"); @@ -14,13 +14,13 @@ jest.mock("axios"); describe("Test parseCertificateInfo", () => { it("should handle undefined", async () => { - const parseCertificateInfo = utilServerRewire.__get__("parseCertificateInfo"); + const parseCertificateInfo = utilServer.__getPrivateFunction("parseCertificateInfo"); const info = parseCertificateInfo(undefined); expect(info).toEqual(undefined); }, 5000); it("should handle normal cert chain", async () => { - const parseCertificateInfo = utilServerRewire.__get__("parseCertificateInfo"); + const parseCertificateInfo = utilServer.__getPrivateFunction("parseCertificateInfo"); const chain1 = { fingerprint: "CF:2C:F3:6A:FE:6B:10:EC:44:77:C8:95:BB:96:2E:06:1F:0E:15:DA", @@ -52,7 +52,7 @@ describe("Test parseCertificateInfo", () => { }, 5000); it("should handle cert chain with strange circle", async () => { - const parseCertificateInfo = utilServerRewire.__get__("parseCertificateInfo"); + const parseCertificateInfo = utilServer.__getPrivateFunction("parseCertificateInfo"); const chain1 = { fingerprint: "CF:2C:F3:6A:FE:6B:10:EC:44:77:C8:95:BB:96:2E:06:1F:0E:15:DA", @@ -92,7 +92,7 @@ describe("Test parseCertificateInfo", () => { }, 5000); it("should handle cert chain with last undefined (should be happen in real, but just in case)", async () => { - const parseCertificateInfo = utilServerRewire.__get__("parseCertificateInfo"); + const parseCertificateInfo = utilServer.__getPrivateFunction("parseCertificateInfo"); const chain1 = { fingerprint: "CF:2C:F3:6A:FE:6B:10:EC:44:77:C8:95:BB:96:2E:06:1F:0E:15:DA", @@ -213,22 +213,22 @@ describe("Test Discord Notification Provider", () => { describe("The function filterAndJoin", () => { it("should join and array of strings to one string", () => { - const result = utilServerRewire.filterAndJoin([ "one", "two", "three" ]); + const result = utilServer.filterAndJoin([ "one", "two", "three" ]); expect(result).toBe("onetwothree"); }); it("should join strings using a given connector", () => { - const result = utilServerRewire.filterAndJoin([ "one", "two", "three" ], "-"); + const result = utilServer.filterAndJoin([ "one", "two", "three" ], "-"); expect(result).toBe("one-two-three"); }); it("should filter null, undefined and empty strings before joining", () => { - const result = utilServerRewire.filterAndJoin([ undefined, "", "three" ], "--"); + const result = utilServer.filterAndJoin([ undefined, "", "three" ], "--"); expect(result).toBe("three"); }); it("should return an empty string if all parts are filtered out", () => { - const result = utilServerRewire.filterAndJoin([ undefined, "", "" ], "--"); + const result = utilServer.filterAndJoin([ undefined, "", "" ], "--"); expect(result).toBe(""); }); });