diff --git a/CHANGELOG.md b/CHANGELOG.md index eda2b25eeb..dbee3b6f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ _This release is scheduled to be released on 2021-10-01._ - Update weathergov provider to try fetching not just current, but also foreacst, when API URLs available. - Refactored clock layout. - Refactored methods from weatherproviders into weatherobject (isDaytime, updateSunTime). +- Use of `logger.js` in jest tests. ### Fixed diff --git a/js/logger.js b/js/logger.js index 93a5bb5350..38185153b2 100644 --- a/js/logger.js +++ b/js/logger.js @@ -22,7 +22,7 @@ root.Log = factory(root.config); } })(this, function (config) { - const logLevel = { + let 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), @@ -32,10 +32,13 @@ 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) + timeEnd: Function.prototype.bind.call(console.timeEnd, console) }; + if ((typeof exports === "object" && process.env.JEST_WORKER_ID === undefined) || typeof exports !== "object") { + logLevel.timeStamp = Function.prototype.bind.call(console.timeStamp, console); + } + logLevel.setLogLevel = function (newLevel) { if (newLevel) { Object.keys(logLevel).forEach(function (key, index) { diff --git a/modules/default/updatenotification/git_helper.js b/modules/default/updatenotification/git_helper.js index fbba63157e..6bda7337d6 100644 --- a/modules/default/updatenotification/git_helper.js +++ b/modules/default/updatenotification/git_helper.js @@ -2,17 +2,12 @@ const util = require("util"); const exec = util.promisify(require("child_process").exec); const fs = require("fs"); const path = require("path"); +const Log = require("logger"); class gitHelper { constructor() { this.gitRepos = []; this.baseDir = path.normalize(__dirname + "/../../../"); - if (process.env.JEST_WORKER_ID === undefined) { - this.Log = require("logger"); - } else { - // if we are running with jest - this.Log = require("../../../tests/unit/mocks/logger.js"); - } } getRefRegex(branch) { @@ -31,7 +26,7 @@ class gitHelper { async isGitRepo(moduleFolder) { let res = await this.execShell("cd " + moduleFolder + " && git remote -v"); if (res.stderr) { - this.Log.error("Failed to fetch git data for " + moduleFolder + ": " + res.stderr); + Log.error("Failed to fetch git data for " + moduleFolder + ": " + res.stderr); return false; } else { return true; @@ -44,7 +39,7 @@ class gitHelper { moduleFolder = moduleFolder + "modules/" + moduleName; } try { - this.Log.info("Checking git for module: " + moduleName); + Log.info("Checking git for module: " + moduleName); // Throws error if file doesn't exist fs.statSync(path.join(moduleFolder, ".git")); // Fetch the git or throw error if no remotes @@ -76,13 +71,13 @@ class gitHelper { // the hash is only needed for the mm repo res = await this.execShell("cd " + repo.folder + " && git rev-parse HEAD"); if (res.stderr) { - this.Log.error("Failed to get current commit hash for " + repo.module + ": " + res.stderr); + Log.error("Failed to get current commit hash for " + repo.module + ": " + res.stderr); } gitInfo.hash = res.stdout; } res = await this.execShell("cd " + repo.folder + " && git status -sb"); if (res.stderr) { - this.Log.error("Failed to get git status for " + repo.module + ": " + res.stderr); + Log.error("Failed to get git status for " + repo.module + ": " + res.stderr); // exit without git status info return; } diff --git a/package-lock.json b/package-lock.json index 8f95a5a4d2..5a1c3fb9b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "iconv-lite": "^0.6.3", "module-alias": "^2.2.2", "moment": "^2.29.1", - "node-fetch": "^2.6.1", + "node-fetch": "^2.6.2", "node-ical": "^0.13.0", "socket.io": "^4.2.0" }, @@ -36,7 +36,7 @@ "jsdom": "^17.0.0", "lodash": "^4.17.21", "nyc": "^15.1.0", - "prettier": "^2.3.2", + "prettier": "^2.4.0", "pretty-quick": "^3.1.1", "sinon": "^11.1.2", "spectron": "^15.0.0", @@ -6948,9 +6948,9 @@ } }, "node_modules/node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.2.tgz", + "integrity": "sha512-aLoxToI6RfZ+0NOjmWAgn9+LEd30YCkJKFSyWacNZdEKTit/ZMcKjGkTRo8uWEsnIb/hfKecNPEbln02PdWbcA==", "engines": { "node": "4.x || >=6.0.0" } @@ -7741,9 +7741,9 @@ } }, "node_modules/prettier": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", - "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.4.0.tgz", + "integrity": "sha512-DsEPLY1dE5HF3BxCRBmD4uYZ+5DCbvatnolqTqcxEgKVZnL2kUfyu7b8pPQ5+hTBkdhU9SLUmK0/pHb07RE4WQ==", "dev": true, "bin": { "prettier": "bin-prettier.js" @@ -8789,9 +8789,9 @@ } }, "node_modules/source-map-support": { - "version": "0.5.19", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", - "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", + "version": "0.5.20", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.20.tgz", + "integrity": "sha512-n1lZZ8Ve4ksRqizaBQgxXDgKwttHDhyfQjA6YZZn8+AroHbsIz+JjwxQDxbp+7y5OYCI8t1Yk7etjD9CRd2hIw==", "dev": true, "dependencies": { "buffer-from": "^1.0.0", @@ -15827,9 +15827,9 @@ } }, "node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.2.tgz", + "integrity": "sha512-aLoxToI6RfZ+0NOjmWAgn9+LEd30YCkJKFSyWacNZdEKTit/ZMcKjGkTRo8uWEsnIb/hfKecNPEbln02PdWbcA==" }, "node-ical": { "version": "0.13.0", @@ -16440,9 +16440,9 @@ "devOptional": true }, "prettier": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz", - "integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.4.0.tgz", + "integrity": "sha512-DsEPLY1dE5HF3BxCRBmD4uYZ+5DCbvatnolqTqcxEgKVZnL2kUfyu7b8pPQ5+hTBkdhU9SLUmK0/pHb07RE4WQ==", "dev": true }, "prettier-linter-helpers": { @@ -17243,9 +17243,9 @@ "dev": true }, "source-map-support": { - "version": "0.5.19", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", - "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", + "version": "0.5.20", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.20.tgz", + "integrity": "sha512-n1lZZ8Ve4ksRqizaBQgxXDgKwttHDhyfQjA6YZZn8+AroHbsIz+JjwxQDxbp+7y5OYCI8t1Yk7etjD9CRd2hIw==", "dev": true, "requires": { "buffer-from": "^1.0.0", diff --git a/package.json b/package.json index aa80ffc6e5..92350f0c67 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "jsdom": "^17.0.0", "lodash": "^4.17.21", "nyc": "^15.1.0", - "prettier": "^2.3.2", + "prettier": "^2.4.0", "pretty-quick": "^3.1.1", "sinon": "^11.1.2", "spectron": "^15.0.0", @@ -80,7 +80,7 @@ "iconv-lite": "^0.6.3", "module-alias": "^2.2.2", "moment": "^2.29.1", - "node-fetch": "^2.6.1", + "node-fetch": "^2.6.2", "node-ical": "^0.13.0", "socket.io": "^4.2.0" }, @@ -96,6 +96,9 @@ "projects": [ { "displayName": "unit", + "moduleNameMapper": { + "logger": "/js/logger.js" + }, "testMatch": [ "**/tests/unit/**/*.[jt]s?(x)" ], diff --git a/tests/unit/functions/updatenotification_spec.js b/tests/unit/functions/updatenotification_spec.js index ff64817767..91715c7191 100644 --- a/tests/unit/functions/updatenotification_spec.js +++ b/tests/unit/functions/updatenotification_spec.js @@ -2,28 +2,13 @@ const path = require("path"); const git_Helper = require("../../../modules/default/updatenotification/git_helper.js"); const gitHelper = new git_Helper.gitHelper(); gitHelper.add("default"); -let branch = ""; describe("Updatenotification", function () { - // it is assumed that we are at the HEAD of a branch when running this tests - // and we have no foreign modules installed. - - it("should return 0 for repo count", async function () { - const arr = await gitHelper.getRepos(); - expect(arr.length).toBe(0); - }, 15000); - it("should return valid output for git status", async function () { const arr = await gitHelper.getStatus(); expect(arr.length).toBe(1); const gitInfo = arr[0]; - branch = gitInfo.current; expect(gitInfo.current).not.toBe(""); + expect(gitInfo.hash).not.toBe(""); }, 15000); - - it("should return no refs for git fetch", async function () { - const baseDir = path.normalize(__dirname + "/../../../"); - const res = await gitHelper.execShell("cd " + baseDir + " && git fetch --dry-run"); - expect(res.stderr.match(gitHelper.getRefRegex(branch))).toBe(null); - }); }); diff --git a/tests/unit/global_vars/defaults_modules_spec.js b/tests/unit/global_vars/defaults_modules_spec.js index 03879a86d0..490a25101c 100644 --- a/tests/unit/global_vars/defaults_modules_spec.js +++ b/tests/unit/global_vars/defaults_modules_spec.js @@ -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); }); } }); diff --git a/tests/unit/global_vars/root_path_spec.js b/tests/unit/global_vars/root_path_spec.js index 9dfc3f30b8..1f86082bdd 100644 --- a/tests/unit/global_vars/root_path_spec.js +++ b/tests/unit/global_vars/root_path_spec.js @@ -1,49 +1,15 @@ const fs = require("fs"); const path = require("path"); -const vm = require("vm"); -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 root_path = path.join(__dirname, "../../.."); +const version = require(`${__dirname}/../../../package.json`).version; +describe("'global.root_path' set in js/app.js", function () { 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); }); }); diff --git a/tests/unit/mocks/logger.js b/tests/unit/mocks/logger.js deleted file mode 100644 index 56b5b1234e..0000000000 --- a/tests/unit/mocks/logger.js +++ /dev/null @@ -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; -});