Files
MagicMirror/tests/unit/classes/module_spec.js
T
Veeck 4b4a59534f Release 2.37.0 (#4194)
## Release Notes
Thanks to: @angeldeejay, @egeekial, @khassel, @KristjanESPERANTO,
@MikeBishop, @rejas
> ⚠️ This release needs nodejs version >=22.21.1 <23 || >=24 (no change
to previous release)

[Compare to previous Release
v2.36.0](https://github.com/MagicMirrorOrg/MagicMirror/compare/v2.36.0...develop)


### [core]
- Prepare Release 2.37.0 (#4193)
- fix(electron): map IPv6 :: wildcard to localhost (#4188)
- refactor(main): modernize DOM update flow with async/await (#4186)
- refactor(main): simplify _updateDom with async/await (#4185)
- fix(security): prevent unauthorized secret expansion in socket
payloads (#4184)
- refactor(main): simplify updateDomWithContent async flow (#4182)
- fix: modules losing data after HTTP 304 responses (#4180)
- chore: add missing core defaults (#4181)
- fix(server): enforce ipWhitelist for Socket.IO too (#4169)
- feat(systeminfo): include Git hash and branch in system information
log (#4167)
- feat(electron): support object-based electronSwitches (#4161)
- systeminformation thread not ending: move error handling from utils to
app (#4160)
- fix systeminformation thread not ending (#4155)
- refactor: use ES module imports in browser core (#4158)
- refactor(core): remove old Object.assign polyfill (#4157)
- refactor: rewrite Module as an ES6 class (#4151)
- refactor: rewrite NodeHelper as an ES6 class (#4147)
- update eletron to v42 (#4144)
- refactor(utils): drop ajv dependency (#4142)
- fix(systeminformation): output right 'used node' version (from parent
process) (#4141)
- fix: skip postinstall git clean when not in a git repository (#4139)
- Remove unnecessary conditionals and fix falsy property check in
imperial conversion (#4135)
- update version in package.json

### [dependencies]
- update dependencies (#4191)
- Bump actions/checkout from 6 to 7 (#4190)
- chore: update dependencies and adjust import path for SunCalc (#4189)
- update dependencies incl. electron and revert
yauzl-electron-install-fix (#4183)
- update dependencies, add electron fix in package.json (#4175)
- chore: update dependencies (#4162)
- Bump actions/dependency-review-action from 4 to 5 (#4152)
- Unify linting: replace Stylelint and markdownlint with ESLint (#4148)
- update dependencies and workflows to node v26 (#4140)

### [modules/alert]
- CodeQL cleanup for alerts #18, #19, #20 (#4153)
- fix: resolve CodeQL alerts #24 and #26 (#4145)
- fix(electron): resolve CodeQL alerts #22 and #25 in electron.js
(#4136)

### [modules/calendar]
- perf(calendar): pre-filter ICS data before parsing (#4168)
- perf(calendar): use async ICS parsing to avoid blocking event loop
(#4143)

### [modules/newsfeed]
- [newsfeed] add allowBasicHtmlTags option for basic emphasis (#4176)

### [modules/updatenotification]
- fix(updatenotification): don't spawn a child process when running
under PM2 (#4166)
- fix(updatenotification): use process.argv[0] as restart binary (#4163)
- fix(updatenotification): preserve start mode on restart (#4156)
- fix(updatenotification): fix ref diff parsing for fetch --dry-run
(#4138)
- refactor(updatenotification): replace pm2 usage with node logic
(#4134)

### [modules/weather]
- feat(weather): add Buienradar provider (#4164)

### [testing]
- remove warning in unit tests (for nodejs >= v25) (#4149)
- polish HTTP 304 docs/test/handling (#4129)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: BugHaver <43462320+bughaver@users.noreply.github.com>
Co-authored-by: BugHaver <43462320+lsaadeh@users.noreply.github.com>
Co-authored-by: Karsten Hassel <hassel@gmx.de>
Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com>
Co-authored-by: Koen Konst <koenspero@gmail.com>
Co-authored-by: Koen Konst <c.h.konst@avisi.nl>
Co-authored-by: Bugsounet - Cédric <github@bugsounet.fr>
Co-authored-by: dathbe <github@beffa.us>
Co-authored-by: veeck <gitkraken@veeck.de>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Marcel <m-idler@users.noreply.github.com>
Co-authored-by: sam detweiler <sdetweil@gmail.com>
Co-authored-by: Kevin G. <crazylegstoo@gmail.com>
Co-authored-by: Jboucly <33218155+jboucly@users.noreply.github.com>
Co-authored-by: Jboucly <contact@jboucly.fr>
Co-authored-by: Jarno <54169345+jarnoml@users.noreply.github.com>
Co-authored-by: Jordan Welch <JordanHWelch@gmail.com>
Co-authored-by: Blackspirits <blackspirits@gmail.com>
Co-authored-by: Samed Ozdemir <samed@xsor.io>
Co-authored-by: in-voker <58696565+in-voker@users.noreply.github.com>
Co-authored-by: Andrés Vanegas Jiménez <142350+angeldeejay@users.noreply.github.com>
Co-authored-by: cgillinger <christian.gillinger@gmail.com>
Co-authored-by: Sonny B <43247590+sonnyb9@users.noreply.github.com>
Co-authored-by: sonnyb9 <sonnyb9@users.noreply.github.com>
Co-authored-by: Morgan McBee <egeekial@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Mike Bishop <mbishop@evequefou.be>
2026-07-01 22:01:42 +02:00

300 lines
8.0 KiB
JavaScript

const path = require("node:path");
const { pathToFileURL } = require("node:url");
describe("File js/module (cloneObject)", () => {
describe("Test function cloneObject", () => {
let clone;
let Module;
let originalWindow;
let originalLog;
let originalConfig;
let originalMM;
let originalTranslator;
let originalNunjucks;
beforeAll(async () => {
originalWindow = global.window;
originalLog = global.Log;
originalConfig = global.config;
originalMM = global.MM;
originalTranslator = global.Translator;
originalNunjucks = global.nunjucks;
global.window = { mmVersion: "2.0.0" };
global.Log = { log: () => {}, info: () => {}, warn: () => {}, error: () => {}, debug: () => {} };
global.config = { language: "en" };
global.MM = {
hideModule: () => {},
showModule: () => {},
sendNotification: () => {},
updateDom: () => {}
};
global.Translator = {
load: () => Promise.resolve(),
translate: () => ""
};
global.nunjucks = {
Environment () {
this.addFilter = () => {};
this.renderString = () => "";
this.render = (_template, _data, callback) => callback(null, "");
},
WebLoader () {},
runtime: {
markSafe: (str) => str
}
};
const modulePath = pathToFileURL(path.join(__dirname, "..", "..", "..", "js", "module.js")).href;
const loaded = await import(`${modulePath}?test=${Date.now()}`);
clone = loaded.cloneObject;
Module = loaded.Module;
});
afterAll(() => {
global.window = originalWindow;
global.Log = originalLog;
global.config = originalConfig;
global.MM = originalMM;
global.Translator = originalTranslator;
global.nunjucks = originalNunjucks;
});
it("should clone object", () => {
const expected = { name: "Rodrigo", web: "https://rodrigoramirez.com", project: "MagicMirror" };
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
});
it("should clone array", () => {
const expected = [1, null, undefined, "TEST"];
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
});
it("should clone number", () => {
let expected = 1;
let obj = clone(expected);
expect(obj).toBe(expected);
expected = 1.23;
obj = clone(expected);
expect(obj).toBe(expected);
});
it("should clone string", () => {
const expected = "Perfect stranger";
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should clone regex", () => {
const expected = /.*Magic/;
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
});
it("should clone date", () => {
const expected = new Date("2026-05-11T20:00:00.000Z");
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
});
it("should return URL by reference", () => {
const expected = new URL("https://magicmirror.builders/path?q=1");
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should return map by reference", () => {
const mapValue = { nested: [1, 2, 3] };
const expected = new Map([["module", mapValue]]);
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should return set by reference", () => {
const setValue = { nested: true };
const expected = new Set([setValue]);
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should return class instances by reference", () => {
class ModuleDefaults {
constructor () {
this.enabled = true;
}
}
const expected = new ModuleDefaults();
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should clone undefined", () => {
const expected = undefined;
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should clone null", () => {
const expected = null;
const obj = clone(expected);
expect(obj).toBe(expected);
});
it("should clone nested object", () => {
const expected = {
name: "fewieden",
link: "https://github.com/fewieden",
versions: ["2.0", "2.1", "2.2"],
answerForAllQuestions: 42,
properties: {
items: [{ foo: "bar" }, { lorem: "ipsum" }],
invalid: undefined,
nothing: null
}
};
const obj = clone(expected);
expect(obj).toEqual(expected);
expect(expected === obj).toBe(false);
expect(expected.versions === obj.versions).toBe(false);
expect(expected.properties === obj.properties).toBe(false);
expect(expected.properties.items === obj.properties.items).toBe(false);
expect(expected.properties.items[0] === obj.properties.items[0]).toBe(false);
expect(expected.properties.items[1] === obj.properties.items[1]).toBe(false);
});
describe("Test Module.create", () => {
let info;
beforeEach(() => {
info = global.Log.info;
});
afterEach(() => {
global.Log.info = info;
Module.definitions = {};
});
it("should create module instance with dynamic class name", () => {
const moduleName = "MMM-TestModule";
Module.register(moduleName, {
defaults: {}
});
const moduleInstance = Module.create(moduleName);
expect(moduleInstance.constructor.name).toBe(moduleName);
});
it("should use fallback class name for empty module name", () => {
const moduleName = "";
Module.register(moduleName, {
defaults: {}
});
const moduleInstance = Module.create(moduleName);
expect(moduleInstance.constructor.name).toBe("AnonymousModule");
});
it("should not throw when init is not a function", () => {
const moduleName = "MMM-TestModuleNoInitFunction";
Module.register(moduleName, {
init: null,
defaults: {}
});
expect(() => Module.create(moduleName)).not.toThrow();
});
it("should support lifecycle super call pattern", () => {
const moduleName = "MMM-TestSuperCall";
let loggedMessage;
global.Log.info = (message) => {
loggedMessage = message;
};
Module.register(moduleName, {
defaults: {},
start () {
this.didStart = true;
Module.prototype.start.call(this);
}
});
const moduleInstance = Module.create(moduleName);
moduleInstance.name = moduleName;
moduleInstance.start();
expect(moduleInstance.didStart).toBe(true);
expect(loggedMessage).toBe(`Starting module: ${moduleName}`);
});
it("should set config when defaults are undefined", () => {
const moduleName = "MMM-TestNoDefaults";
Module.register(moduleName, {});
const moduleInstance = Module.create(moduleName);
moduleInstance.setConfig({ foo: "bar" }, false);
expect(moduleInstance.config).toEqual({ foo: "bar" });
moduleInstance.setConfig({ nested: { value: 1 } }, true);
expect(moduleInstance.config).toEqual({ nested: { value: 1 } });
});
it("should initialize lifecycle fields in setData", () => {
const moduleName = "MMM-TestSetData";
Module.register(moduleName, {
defaults: { fromDefaults: true }
});
const moduleInstance = Module.create(moduleName);
moduleInstance.setData({
name: moduleName,
identifier: "module_1",
config: { fromConfig: true },
configDeepMerge: false
});
expect(moduleInstance.name).toBe(moduleName);
expect(moduleInstance.identifier).toBe("module_1");
expect(moduleInstance.hidden).toBe(false);
expect(moduleInstance.hasAnimateIn).toBe(false);
expect(moduleInstance.hasAnimateOut).toBe(false);
expect(moduleInstance.config).toEqual({ fromDefaults: true, fromConfig: true });
});
it("should not share defaults object across module instances", () => {
const moduleName = "MMM-TestDefaultsIsolation";
Module.register(moduleName, {
defaults: {
nested: { value: 1 },
list: [1]
}
});
const firstModuleInstance = Module.create(moduleName);
const secondModuleInstance = Module.create(moduleName);
firstModuleInstance.defaults.nested.value = 42;
firstModuleInstance.defaults.list.push(2);
expect(secondModuleInstance.defaults).toEqual({
nested: { value: 1 },
list: [1]
});
});
});
});
});