Files
MagicMirror/tests/utils/translation_test_environment.js
Kristjan ESPERANTO ed95826d19 refactor: convert Translator to ES module (#4202)
Another step towards ES modules: this converts `Translator` to a proper
module and imports it where needed, instead of relying on it being
globally available.

The goal is to gradually get rid of the old global-script style in the
core. It makes dependencies explicit, reduces hidden coupling and keeps
the codebase easier to maintain going forward.

The code change is tiny - the bigger and harder part was adapting the
tests to the new loading behavior.
2026-07-12 21:36:12 +02:00

44 lines
1.2 KiB
JavaScript

const path = require("node:path");
const { pathToFileURL } = require("node:url");
const { JSDOM } = require("jsdom");
const TRANSLATOR_MODULE_URL = pathToFileURL(path.join(__dirname, "..", "..", "js", "translator.js")).href;
/**
* Reset mutable Translator state between tests.
* Clears all non-function plain-object properties to remain resilient
* when additional translation stores are introduced.
* @param {object} Translator The shared Translator module instance.
*/
function resetTranslatorState (Translator) {
for (const [key, value] of Object.entries(Translator)) {
if (typeof value === "function") {
continue;
}
if (Object.prototype.toString.call(value) === "[object Object]") {
Translator[key] = {};
}
}
}
/**
* Set up DOM globals used by translation tests.
* @param {number} [port] Base URI port used to resolve relative translation paths.
* @returns {void}
*/
function setupTranslationTestEnvironment (port = 3000) {
const dom = new JSDOM("", { url: `http://localhost:${port}` });
global.document = dom.window.document;
if (!global.Log) {
global.Log = { log: vi.fn(), error: vi.fn() };
}
}
module.exports = {
setupTranslationTestEnvironment,
TRANSLATOR_MODULE_URL,
resetTranslatorState
};