Merge pull request #2601 from khassel/jest

This commit is contained in:
Michael Teeuw
2021-06-20 14:45:41 +02:00
committed by GitHub
7 changed files with 44 additions and 18 deletions

View File

@@ -36,6 +36,7 @@ Special thanks to the following contributors: @B1gG, @codac, @ezeholz, @khassel,
- Cleaned up error handling in newsfeed and calendar modules for real
- Updated default WEATHER module such that a provider can optionally set a custom unit-of-measure for precipitation (`weatherObject.precipitationUnits`)
- Update documentation.
- Update jest tests: Reset changes on js/logger.js, mock logger.js in global_vars tests.
### Removed

View File

@@ -22,7 +22,7 @@
root.Log = factory(root.config);
}
})(this, function (config) {
let logLevel = {
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),
@@ -32,14 +32,10 @@
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)
timeEnd: Function.prototype.bind.call(console.timeEnd, console),
timeStamp: Function.prototype.bind.call(console.timeStamp, console)
};
// the timeStamp instruction fails when running the tests so it is not added in test environment
if (typeof process === "object" && process.env.NODE_ENV.trim() !== "test") {
logLevel = Object.assign(logLevel, { timeStamp: Function.prototype.bind.call(console.timeStamp, console) });
}
logLevel.setLogLevel = function (newLevel) {
if (newLevel) {
Object.keys(logLevel).forEach(function (key, index) {

View File

@@ -100,14 +100,8 @@
"**/tests/unit/**/*.[jt]s?(x)"
],
"testPathIgnorePatterns": [
"<rootDir>/tests/unit/setup_unit.js"
],
"setupFiles": [
"<rootDir>/tests/unit/setup_unit.js"
],
"moduleNameMapper": {
"logger": "<rootDir>/js/logger.js"
}
"<rootDir>/tests/unit/mocks"
]
},
{
"displayName": "e2e",

View File

@@ -22,7 +22,15 @@ beforeAll(function () {
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
return require(filename);
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch {
// ignore
}
}
};
vm.runInNewContext(code, sandbox, fileName);

View File

@@ -22,7 +22,15 @@ beforeAll(function () {
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
return require(filename);
if (filename === "logger") {
return require("../mocks/logger.js");
} else {
try {
return require(filename);
} catch {
// ignore
}
}
};
vm.runInNewContext(code, sandbox, fileName);

View File

@@ -0,0 +1,20 @@
(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;
});

View File

@@ -1 +0,0 @@
console.log = () => {};