do logger mocking in logger.js, remove sandbox stuff from unit tests

This commit is contained in:
Karsten Hassel 2021-09-09 20:50:35 +02:00
parent 06389e35f9
commit 2b147bb98b
4 changed files with 53 additions and 123 deletions

View File

@ -9,11 +9,13 @@
*/
(function (root, factory) {
if (typeof exports === "object") {
// add timestamps in front of log messages
require("console-stamp")(console, {
pattern: "yyyy-mm-dd HH:MM:ss.l",
include: ["debug", "log", "info", "warn", "error"]
});
if (process.env.JEST_WORKER_ID === undefined) {
// add timestamps in front of log messages
require("console-stamp")(console, {
pattern: "yyyy-mm-dd HH:MM:ss.l",
include: ["debug", "log", "info", "warn", "error"]
});
}
// Node, CommonJS-like
module.exports = factory(root.config);
@ -22,29 +24,46 @@
root.Log = factory(root.config);
}
})(this, function (config) {
const 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)
};
let logLevel;
if (process.env.JEST_WORKER_ID === undefined) {
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)
};
logLevel.setLogLevel = function (newLevel) {
if (newLevel) {
Object.keys(logLevel).forEach(function (key, index) {
if (!newLevel.includes(key.toLocaleUpperCase())) {
logLevel[key] = function () {};
}
});
}
};
logLevel.setLogLevel = function (newLevel) {
if (newLevel) {
Object.keys(logLevel).forEach(function (key, index) {
if (!newLevel.includes(key.toLocaleUpperCase())) {
logLevel[key] = function () {};
}
});
}
};
} else {
logLevel = {
debug: function () {},
log: function () {},
info: function () {},
warn: function () {},
error: function () {},
group: function () {},
groupCollapsed: function () {},
groupEnd: function () {},
time: function () {},
timeEnd: function () {},
timeStamp: function () {}
};
}
return logLevel;
});

View File

@ -1,49 +1,14 @@
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const basedir = path.join(__dirname, "../../..");
const root_path = path.join(__dirname, "../../..");
describe("Default modules set in modules/default/defaultmodules.js", function () {
let sandbox = null;
beforeAll(function () {
const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);
sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
process: {
on: function () {},
env: {}
}
};
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch (ignore) {
// ignore
}
}
};
vm.runInNewContext(code, sandbox, fileName);
});
const expectedDefaultModules = require("../../../modules/default/defaultmodules");
for (const defaultModule of expectedDefaultModules) {
it(`contains a folder for modules/default/${defaultModule}"`, function () {
expect(fs.existsSync(path.join(sandbox.global.root_path, "modules/default", defaultModule))).toBe(true);
expect(fs.existsSync(path.join(root_path, "modules/default", defaultModule))).toBe(true);
});
}
});

View File

@ -1,49 +1,15 @@
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const root_path = path.join(__dirname, "../../..");
const version = require(`${__dirname}/../../../package.json`).version;
describe("'global.root_path' set in js/app.js", function () {
let sandbox = null;
beforeAll(function () {
const basedir = path.join(__dirname, "../../..");
const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);
sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
process: {
on: function () {},
env: {}
}
};
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch (ignore) {
// ignore
}
}
};
vm.runInNewContext(code, sandbox, fileName);
});
const expectedSubPaths = ["modules", "serveronly", "js", "js/app.js", "js/main.js", "js/electron.js", "config"];
expectedSubPaths.forEach((subpath) => {
it(`contains a file/folder "${subpath}"`, function () {
expect(fs.existsSync(path.join(sandbox.global.root_path, subpath))).toBe(true);
expect(fs.existsSync(path.join(root_path, subpath))).toBe(true);
});
});
@ -57,6 +23,6 @@ describe("'global.root_path' set in js/app.js", function () {
it("should expect the global.version equals package.json file", function () {
const versionPackage = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
expect(sandbox.global.version).toBe(versionPackage);
expect(version).toBe(versionPackage);
});
});

View File

@ -1,20 +0,0 @@
(function (root, factory) {
// Node, CommonJS-like
module.exports = factory(root.config);
})(this, function (config) {
let logLevel = {
debug: function () {},
log: function () {},
info: function () {},
warn: function () {},
error: function () {},
group: function () {},
groupCollapsed: function () {},
groupEnd: function () {},
time: function () {},
timeEnd: function () {},
timeStamp: function () {}
};
return logLevel;
});