diff --git a/js/logger.js b/js/logger.js index 7ea12a79..d469e702 100644 --- a/js/logger.js +++ b/js/logger.js @@ -26,18 +26,18 @@ let logLevel; if ((typeof exports === "object" && process.env.JEST_WORKER_ID === undefined) || typeof exports !== "object") { logLevel = { - debug: Function.prototype.bind.call(console.debug, console), - log: Function.prototype.bind.call(console.log, console), - info: Function.prototype.bind.call(console.info, console), - warn: Function.prototype.bind.call(console.warn, console), - error: Function.prototype.bind.call(console.error, console), - group: Function.prototype.bind.call(console.group, console), - groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console), - groupEnd: Function.prototype.bind.call(console.groupEnd, console), - time: Function.prototype.bind.call(console.time, console), - timeEnd: Function.prototype.bind.call(console.timeEnd, console), - timeStamp: Function.prototype.bind.call(console.timeStamp, console) - } + debug: Function.prototype.bind.call(console.debug, console), + log: Function.prototype.bind.call(console.log, console), + info: Function.prototype.bind.call(console.info, console), + warn: Function.prototype.bind.call(console.warn, console), + error: Function.prototype.bind.call(console.error, console), + group: Function.prototype.bind.call(console.group, console), + groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console), + groupEnd: Function.prototype.bind.call(console.groupEnd, console), + time: Function.prototype.bind.call(console.time, console), + timeEnd: Function.prototype.bind.call(console.timeEnd, console), + timeStamp: Function.prototype.bind.call(console.timeStamp, console) + }; logLevel.setLogLevel = function (newLevel) { if (newLevel) { diff --git a/js/server.js b/js/server.js index 8db8b48c..35071091 100644 --- a/js/server.js +++ b/js/server.js @@ -95,8 +95,7 @@ function Server(config, callback) { this.close = function () { server.close(); - } - + }; } module.exports = Server; diff --git a/package.json b/package.json index e48f96f0..833010d1 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,9 @@ ], "modulePaths": [ "/js/" + ], + "testPathIgnorePatterns": [ + "/tests/e2e/global-setup.js" ] } ] diff --git a/tests/e2e/env_spec.js b/tests/e2e/env_spec.js index 91e6564a..825da0e6 100644 --- a/tests/e2e/env_spec.js +++ b/tests/e2e/env_spec.js @@ -1,15 +1,13 @@ const fetch = require("node-fetch"); -const app = require("app.js"); +const helpers = require("./global-setup"); +let app = null; describe("Electron app environment", function () { beforeAll(function () { - process.env.MM_CONFIG_FILE = "tests/configs/env.js"; - - app.start(); + app = helpers.startApplication("tests/configs/env.js"); }); - afterAll(function () { - app.stop(); + helpers.stopApplication(app); }); it("get request from http://localhost:8080 should return 200", function (done) { diff --git a/tests/e2e/fonts.js b/tests/e2e/fonts.js index 81327a87..105b9977 100644 --- a/tests/e2e/fonts.js +++ b/tests/e2e/fonts.js @@ -1,5 +1,6 @@ const fetch = require("node-fetch"); -const app = require("app.js"); +const helpers = require("./global-setup"); +let app = null; describe("All font files from roboto.css should be downloadable", function () { const fontFiles = []; @@ -15,13 +16,10 @@ describe("All font files from roboto.css should be downloadable", function () { } beforeAll(function () { - process.env.MM_CONFIG_FILE = "tests/configs/without_modules.js"; - - app.start(); + app = helpers.startApplication("tests/configs/without_modules.js"); }); - afterAll(function () { - app.stop(); + helpers.stopApplication(app); }); test.each(fontFiles)("should return 200 HTTP code for file '%s'", (fontFile, done) => { diff --git a/tests/e2e/global-setup.js b/tests/e2e/global-setup.js new file mode 100644 index 00000000..1f9bce3b --- /dev/null +++ b/tests/e2e/global-setup.js @@ -0,0 +1,16 @@ +exports.startApplication = function (configFilename, exec) { + jest.resetModules(); + // Set config sample for use in test + process.env.MM_CONFIG_FILE = configFilename; + if (exec) exec; + const app = require("app.js"); + app.start(); + + return app; +}; + +exports.stopApplication = function (app) { + if (app) { + app.stop(); + } +}; diff --git a/tests/e2e/ipWhitelist_spec.js b/tests/e2e/ipWhitelist_spec.js index 5f90274c..eba92a3e 100644 --- a/tests/e2e/ipWhitelist_spec.js +++ b/tests/e2e/ipWhitelist_spec.js @@ -1,19 +1,14 @@ const fetch = require("node-fetch"); -const app = require("app.js"); +const helpers = require("./global-setup"); +let app = null; describe("ipWhitelist directive configuration", function () { - beforeAll(function () { - app.start(); - }); - - afterAll(function () { - app.stop(); - }); - describe("Set ipWhitelist without access", function () { beforeAll(function () { - // Set config sample for use in test - process.env.MM_CONFIG_FILE = "tests/configs/noIpWhiteList.js"; + app = helpers.startApplication("tests/configs/noIpWhiteList.js"); + }); + afterAll(function () { + helpers.stopApplication(app); }); it("should return 403", function (done) { @@ -26,8 +21,10 @@ describe("ipWhitelist directive configuration", function () { describe("Set ipWhitelist []", function () { beforeAll(function () { - // Set config sample for use in test - process.env.MM_CONFIG_FILE = "tests/configs/empty_ipWhiteList.js"; + app = helpers.startApplication("tests/configs/empty_ipWhiteList.js"); + }); + afterAll(function () { + helpers.stopApplication(app); }); it("should return 200", function (done) { diff --git a/tests/e2e/port_config.js b/tests/e2e/port_config.js index 3e50d347..6f6756a4 100644 --- a/tests/e2e/port_config.js +++ b/tests/e2e/port_config.js @@ -1,18 +1,16 @@ const fetch = require("node-fetch"); -const app = require("app.js"); +const helpers = require("./global-setup"); +let app = null; describe("port directive configuration", function () { - beforeAll(function () { - process.env.MM_CONFIG_FILE = "tests/configs/port_8090.js"; - - app.start(); - }); - - afterAll(function () { - app.stop(); - }); - describe("Set port 8090", function () { + beforeAll(function () { + app = helpers.startApplication("tests/configs/port_8090.js"); + }); + afterAll(function () { + helpers.stopApplication(app); + }); + it("should return 200", function (done) { fetch("http://localhost:8090").then((res) => { expect(res.status).toBe(200); @@ -23,11 +21,10 @@ describe("port directive configuration", function () { describe("Set port 8100 on environment variable MM_PORT", function () { beforeAll(function () { - process.env.MM_PORT = 8100; + app = helpers.startApplication("tests/configs/port_8090.js", (process.env.MM_PORT = 8100)); }); - afterAll(function () { - delete process.env.MM_PORT; + helpers.stopApplication(app); }); it("should return 200", function (done) { diff --git a/tests/e2e/vendor_spec.js b/tests/e2e/vendor_spec.js index 1aef4de7..e8dcde11 100644 --- a/tests/e2e/vendor_spec.js +++ b/tests/e2e/vendor_spec.js @@ -1,15 +1,13 @@ const fetch = require("node-fetch"); -const app = require("app.js"); +const helpers = require("./global-setup"); +let app = null; describe("Vendors", function () { beforeAll(function () { - process.env.MM_CONFIG_FILE = "tests/configs/env.js"; - - app.start(); + app = helpers.startApplication("tests/configs/env.js"); }); - afterAll(function () { - app.stop(); + helpers.stopApplication(app); }); describe("Get list vendors", function () {