Files
MagicMirror/tests/electron/helpers/global-setup.js
Kristjan ESPERANTO 462abf7027 [tests] migrate from jest to vitest (#3940)
This is a big change, but I think it's a good move, as `vitest` is much
more modern than `jest`.

I'm excited about the UI watch feature (run `npm run test:ui`), for
example - it's really helpful and saves time when debugging tests. I had
to adjust a few tests because they had time related issues, but
basically we are now testing the same things - even a bit better and
less flaky (I hope).

What do you think?
2025-11-03 19:47:01 +01:00

95 lines
2.5 KiB
JavaScript

// see https://playwright.dev/docs/api/class-electronapplication
// https://github.com/microsoft/playwright/issues/6347#issuecomment-1085850728
// https://www.anycodings.com/1questions/958135/can-i-set-the-date-for-playwright-browser
const { _electron: electron } = require("playwright");
exports.startApplication = async (configFilename, systemDate = null, electronParams = [], timezone = "GMT") => {
global.electronApp = null;
global.page = null;
process.env.MM_CONFIG_FILE = configFilename;
process.env.TZ = timezone;
if (systemDate) {
process.env.MOCK_DATE = systemDate;
}
process.env.mmTestMode = "true";
// check environment for DISPLAY or WAYLAND_DISPLAY
if (process.env.WAYLAND_DISPLAY) {
electronParams.unshift("js/electron.js", "--enable-features=UseOzonePlatform", "--ozone-platform=wayland");
} else {
electronParams.unshift("js/electron.js");
}
// Pass environment variables to Electron process
const env = {
...process.env,
MM_CONFIG_FILE: configFilename,
TZ: timezone,
mmTestMode: "true"
};
if (systemDate) {
env.MOCK_DATE = systemDate;
}
global.electronApp = await electron.launch({
args: electronParams,
env: env
});
await global.electronApp.firstWindow();
for (const win of global.electronApp.windows()) {
const title = await win.title();
expect(["MagicMirror²", "DevTools"]).toContain(title);
if (title === "MagicMirror²") {
global.page = win;
if (systemDate) {
await global.page.evaluate((systemDate) => {
Date.now = () => {
return new Date(systemDate).valueOf();
};
}, systemDate);
}
}
}
};
exports.stopApplication = async (timeout = 10000) => {
const app = global.electronApp;
global.electronApp = null;
global.page = null;
process.env.MOCK_DATE = undefined;
if (!app) {
return;
}
const killElectron = () => {
try {
const electronProcess = typeof app.process === "function" ? app.process() : null;
if (electronProcess && !electronProcess.killed) {
electronProcess.kill("SIGKILL");
}
} catch (error) {
// Ignore errors caused by Playwright already tearing down the connection
}
};
try {
await Promise.race([
app.close(),
new Promise((_, reject) => setTimeout(() => reject(new Error("Electron close timeout")), timeout))
]);
} catch (error) {
killElectron();
}
};
exports.getElement = async (selector, state = "visible") => {
expect(global.page).not.toBeNull();
const elem = global.page.locator(selector);
await elem.waitFor({ state: state });
expect(elem).not.toBeNull();
return elem;
};