From c0ce52abe3de3686d448dbace9f0a73fae96cacc Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Fri, 24 Sep 2021 23:39:12 +0200 Subject: [PATCH] change getDocument, delay needed, now 2 tests moved --- js/logger.js | 6 ++-- tests/configs/modules/positions.js | 10 +++++-- tests/e2e/global-setup.js | 24 +++++++++------- tests/e2e/mock-console.js | 7 ++++- tests/e2e/modules_display_spec.js | 7 +++-- tests/e2e/modules_position_spec.js | 23 +++++++++++++++ tests/electron/modules_display_spec.js | 38 ------------------------- tests/electron/modules_position_spec.js | 38 ------------------------- 8 files changed, 57 insertions(+), 96 deletions(-) create mode 100644 tests/e2e/modules_position_spec.js delete mode 100644 tests/electron/modules_display_spec.js delete mode 100644 tests/electron/modules_position_spec.js diff --git a/js/logger.js b/js/logger.js index 7434ccd2..a42db925 100644 --- a/js/logger.js +++ b/js/logger.js @@ -27,11 +27,11 @@ let enableLog; if (typeof exports === "object") { // in nodejs and not running with jest - enableLog = (process.env.JEST_WORKER_ID === undefined); + enableLog = process.env.JEST_WORKER_ID === undefined; } else { // in browser and not running with jsdom - enableLog = (typeof window === 'object' && window.name === 'nodejs'); - }; + enableLog = typeof window === "object" && window.name === "nodejs"; + } if (enableLog) { logLevel = { diff --git a/tests/configs/modules/positions.js b/tests/configs/modules/positions.js index 479775c8..f4f5c453 100644 --- a/tests/configs/modules/positions.js +++ b/tests/configs/modules/positions.js @@ -3,7 +3,13 @@ * By Rodrigo Ramírez Norambuena https://rodrigoramirez.com * MIT Licensed. */ -let config = require(process.cwd() + "/tests/configs/default.js").configFactory({ +let config = { + port: 8080, + ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], + language: "en", + timeFormat: 24, + units: "metric", + modules: // Using exotic content. This is why don't accept go to JSON configuration file (function () { @@ -20,7 +26,7 @@ let config = require(process.cwd() + "/tests/configs/default.js").configFactory( } return modules; })() -}); +}; /*************** DO NOT EDIT THE LINE BELOW ***************/ if (typeof module !== "undefined") { diff --git a/tests/e2e/global-setup.js b/tests/e2e/global-setup.js index 415e946d..72bee653 100644 --- a/tests/e2e/global-setup.js +++ b/tests/e2e/global-setup.js @@ -1,7 +1,7 @@ const jsdom = require("jsdom"); const config = require("../configs/empty_ipWhiteList"); -exports.startApplication = function (configFilename, exec, callback) { +exports.startApplication = function (configFilename, exec) { jest.resetModules(); // Set config sample for use in test process.env.MM_CONFIG_FILE = configFilename; @@ -9,16 +9,6 @@ exports.startApplication = function (configFilename, exec, callback) { const app = require("app.js"); app.start(); - if (callback) { - const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080"); - jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => { - dom.window.onload = function () { - global.document = dom.window.document; - callback(); - }; - }); - }; - return app; }; @@ -27,3 +17,15 @@ exports.stopApplication = function (app) { app.stop(); } }; + +exports.getDocument = function (callback, ms) { + const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080"); + jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => { + dom.window.onload = function () { + global.document = dom.window.document; + setTimeout(() => { + callback(); + }, ms); + }; + }); +}; diff --git a/tests/e2e/mock-console.js b/tests/e2e/mock-console.js index a1ccfa55..745a8fbb 100644 --- a/tests/e2e/mock-console.js +++ b/tests/e2e/mock-console.js @@ -1,5 +1,10 @@ +/** + * Suppresses errors concerning web server already shut down. + * + * @param {string} err The error message. + */ function myError(err) { - if (err.includes("ECONNREFUSED")) { + if (err.includes("ECONNREFUSED") || err.includes("ECONNRESET") || err.includes("socket hang up")) { jest.fn(); } else { console.dir(err); diff --git a/tests/e2e/modules_display_spec.js b/tests/e2e/modules_display_spec.js index 5e07ae19..055ff8c1 100644 --- a/tests/e2e/modules_display_spec.js +++ b/tests/e2e/modules_display_spec.js @@ -3,21 +3,22 @@ let app = null; describe("Display of modules", function () { beforeAll(function (done) { - app = helpers.startApplication("tests/configs/modules/display.js", null, done); + app = helpers.startApplication("tests/configs/modules/display.js"); + helpers.getDocument(done); }); afterAll(function () { helpers.stopApplication(app); }); it("should show the test header", function () { - elem = document.querySelector("#module_0_helloworld .module-header"); + const elem = document.querySelector("#module_0_helloworld .module-header"); expect(elem).not.toBe(null); // textContent gibt hier lowercase zurück, das uppercase wird durch css realisiert, was daher nicht in textContent landet expect(elem.textContent).toBe("test_header"); }); it("should show no header if no header text is specified", function () { - elem = document.querySelector("#module_1_helloworld .module-header"); + const elem = document.querySelector("#module_1_helloworld .module-header"); expect(elem).not.toBe(null); expect(elem.textContent).toBe("undefined"); }); diff --git a/tests/e2e/modules_position_spec.js b/tests/e2e/modules_position_spec.js new file mode 100644 index 00000000..c23933b0 --- /dev/null +++ b/tests/e2e/modules_position_spec.js @@ -0,0 +1,23 @@ +const helpers = require("./global-setup"); +let app = null; + +describe("Position of modules", function () { + beforeAll(function (done) { + app = helpers.startApplication("tests/configs/modules/positions.js"); + helpers.getDocument(done, 1000); + }); + afterAll(function () { + helpers.stopApplication(app); + }); + + const positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"]; + + for (const position of positions) { + const className = position.replace("_", "."); + it("should show text in " + position, function () { + const elem = document.querySelector("." + className); + expect(elem).not.toBe(null); + expect(elem.textContent).toContain("Text in " + position); + }); + } +}); diff --git a/tests/electron/modules_display_spec.js b/tests/electron/modules_display_spec.js deleted file mode 100644 index 6132c62f..00000000 --- a/tests/electron/modules_display_spec.js +++ /dev/null @@ -1,38 +0,0 @@ -const helpers = require("./global-setup"); - -describe("Display of modules", function () { - helpers.setupTimeout(this); - - let app = null; - - beforeEach(function () { - return helpers - .startApplication({ - args: ["js/electron.js"] - }) - .then(function (startedApp) { - app = startedApp; - }); - }); - - afterEach(function () { - return helpers.stopApplication(app); - }); - - describe("Using helloworld", function () { - beforeAll(function () { - // Set config sample for use in test - process.env.MM_CONFIG_FILE = "tests/configs/modules/display.js"; - }); - - it("should show the test header", async () => { - const elem = await app.client.$("#module_0_helloworld .module-header", 10000); - return expect(await elem.getText("#module_0_helloworld .module-header")).toBe("TEST_HEADER"); - }); - - it("should show no header if no header text is specified", async () => { - const elem = await app.client.$("#module_1_helloworld .module-header", 10000); - return expect(await elem.getText("#module_1_helloworld .module-header")).toBe(""); - }); - }); -}); diff --git a/tests/electron/modules_position_spec.js b/tests/electron/modules_position_spec.js deleted file mode 100644 index 7ac1cadd..00000000 --- a/tests/electron/modules_position_spec.js +++ /dev/null @@ -1,38 +0,0 @@ -const helpers = require("./global-setup"); - -describe("Position of modules", function () { - helpers.setupTimeout(this); - - let app = null; - - describe("Using helloworld", function () { - afterAll(function () { - return helpers.stopApplication(app); - }); - - beforeAll(function () { - // Set config sample for use in test - process.env.MM_CONFIG_FILE = "tests/configs/modules/positions.js"; - return helpers - .startApplication({ - args: ["js/electron.js"] - }) - .then(function (startedApp) { - app = startedApp; - }); - }); - - const positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"]; - - for (const position of positions) { - const className = position.replace("_", "."); - it("should show text in " + position, function () { - return app.client.$("." + className).then((result) => { - return result.getText("." + className).then((text) => { - return expect(text).toContain("Text in " + position); - }); - }); - }); - } - }); -});