fix logger.js, move jsdom in startApplication

This commit is contained in:
Karsten Hassel 2021-09-24 21:30:51 +02:00
parent 0ef6f89d44
commit a1c7f20990
4 changed files with 27 additions and 16 deletions

View File

@ -24,7 +24,16 @@
} }
})(this, function (config) { })(this, function (config) {
let logLevel; let logLevel;
if ((typeof exports === "object" && process.env.JEST_WORKER_ID === undefined) || typeof exports !== "object") { let enableLog;
if (typeof exports === "object") {
// in nodejs and not running with jest
enableLog = (process.env.JEST_WORKER_ID === undefined);
} else {
// in browser and not running with jsdom
enableLog = (typeof window === 'object' && window.name === 'nodejs');
};
if (enableLog) {
logLevel = { logLevel = {
debug: Function.prototype.bind.call(console.debug, console), debug: Function.prototype.bind.call(console.debug, console),
log: Function.prototype.bind.call(console.log, console), log: Function.prototype.bind.call(console.log, console),
@ -35,8 +44,8 @@
groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console), groupCollapsed: Function.prototype.bind.call(console.groupCollapsed, console),
groupEnd: Function.prototype.bind.call(console.groupEnd, console), groupEnd: Function.prototype.bind.call(console.groupEnd, console),
time: Function.prototype.bind.call(console.time, console), time: Function.prototype.bind.call(console.time, console),
timeEnd: Function.prototype.bind.call(console.timeEnd, console) timeEnd: Function.prototype.bind.call(console.timeEnd, console),
// timeStamp: Function.prototype.bind.call(console.timeStamp, console) timeStamp: Function.prototype.bind.call(console.timeStamp, console)
}; };
logLevel.setLogLevel = function (newLevel) { logLevel.setLogLevel = function (newLevel) {

View File

@ -130,7 +130,8 @@
"<rootDir>/js/" "<rootDir>/js/"
], ],
"testPathIgnorePatterns": [ "testPathIgnorePatterns": [
"<rootDir>/tests/e2e/global-setup.js" "<rootDir>/tests/e2e/global-setup.js",
"<rootDir>/tests/e2e/mock-console.js"
] ]
} }
] ]

View File

@ -1,6 +1,7 @@
const jsdom = require("jsdom"); const jsdom = require("jsdom");
const config = require("../configs/empty_ipWhiteList");
exports.startApplication = function (configFilename, exec) { exports.startApplication = function (configFilename, exec, callback) {
jest.resetModules(); jest.resetModules();
// Set config sample for use in test // Set config sample for use in test
process.env.MM_CONFIG_FILE = configFilename; process.env.MM_CONFIG_FILE = configFilename;
@ -8,6 +9,16 @@ exports.startApplication = function (configFilename, exec) {
const app = require("app.js"); const app = require("app.js");
app.start(); 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; return app;
}; };
@ -16,12 +27,3 @@ exports.stopApplication = function (app) {
app.stop(); app.stop();
} }
}; };
exports.getDocument = function (url, callback) {
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
dom.window.onload = function () {
global.document = dom.window.document;
callback();
};
});
};

View File

@ -3,8 +3,7 @@ let app = null;
describe("Display of modules", function () { describe("Display of modules", function () {
beforeAll(function (done) { beforeAll(function (done) {
app = helpers.startApplication("tests/configs/modules/display.js"); app = helpers.startApplication("tests/configs/modules/display.js", null, done);
helpers.getDocument("http://localhost:8080", done);
}); });
afterAll(function () { afterAll(function () {
helpers.stopApplication(app); helpers.stopApplication(app);