[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?
This commit is contained in:
Kristjan ESPERANTO
2025-11-03 19:47:01 +01:00
committed by GitHub
parent b542f33a0a
commit 462abf7027
30 changed files with 2370 additions and 3562 deletions

View File

@@ -65,8 +65,8 @@ function App () {
async function loadConfig () {
Log.log("Loading config ...");
const defaults = require(`${__dirname}/defaults`);
if (process.env.JEST_WORKER_ID !== undefined) {
// if we are running with jest
if (global.mmTestMode) {
// if we are running in test mode
defaults.address = "0.0.0.0";
}
@@ -185,10 +185,10 @@ function App () {
if (defaultModules.includes(moduleName)) {
const defaultModuleFolder = path.resolve(`${global.root_path}/modules/default/`, module);
if (process.env.JEST_WORKER_ID === undefined) {
if (!global.mmTestMode) {
moduleFolder = defaultModuleFolder;
} else {
// running in Jest, allow defaultModules placed under moduleDir for testing
// running in test mode, allow defaultModules placed under moduleDir for testing
if (env.modulesDir === "modules" || env.modulesDir === "tests/mocks") {
moduleFolder = defaultModuleFolder;
}

View File

@@ -76,8 +76,8 @@ function createWindow () {
const electronOptions = Object.assign({}, electronOptionsDefaults, config.electronOptions);
if (process.env.JEST_WORKER_ID !== undefined && process.env.MOCK_DATE !== undefined) {
// if we are running with jest and we want to mock the current date
if (process.env.MOCK_DATE !== undefined) {
// if we are running tests and we want to mock the current date
const fakeNow = new Date(process.env.MOCK_DATE).valueOf();
Date = class extends Date {
constructor (...args) {
@@ -114,8 +114,8 @@ function createWindow () {
// Open the DevTools if run with "node --run start:dev"
if (process.argv.includes("dev")) {
if (process.env.JEST_WORKER_ID !== undefined) {
// if we are running with jest
if (process.env.mmTestMode) {
// if we are running tests
const devtools = new BrowserWindow(electronOptions);
mainWindow.webContents.setDevToolsWebContents(devtools.webContents);
}
@@ -169,8 +169,8 @@ function createWindow () {
// Quit when all windows are closed.
app.on("window-all-closed", function () {
if (process.env.JEST_WORKER_ID !== undefined) {
// if we are running with jest
if (process.env.mmTestMode) {
// if we are running tests
app.quit();
} else {
createWindow();

View File

@@ -84,7 +84,7 @@ const Loader = (function () {
if (window.name !== "jsdom") {
moduleFolder = defaultModuleFolder;
} else {
// running in Jest, allow defaultModules placed under moduleDir for testing
// running in test mode, allow defaultModules placed under moduleDir for testing
if (envVars.modulesDir === "modules") {
moduleFolder = defaultModuleFolder;
}

View File

@@ -1,7 +1,7 @@
// This logger is very simple, but needs to be extended.
(function (root, factory) {
if (typeof exports === "object") {
if (process.env.JEST_WORKER_ID === undefined) {
if (process.env.mmTestMode !== "true") {
const { styleText } = require("node:util");
// add timestamps in front of log messages
@@ -78,8 +78,8 @@
let logLevel;
let enableLog;
if (typeof exports === "object") {
// in nodejs and not running with jest
enableLog = process.env.JEST_WORKER_ID === undefined;
// in nodejs and not running in test mode
enableLog = process.env.mmTestMode !== "true";
} else {
// in browser and not running with jsdom
enableLog = typeof window === "object" && window.name !== "jsdom";
@@ -97,7 +97,7 @@
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)
timeStamp: console.timeStamp ? Function.prototype.bind.call(console.timeStamp, console) : function () {}
};
logLevel.setLogLevel = function (newLevel) {

View File

@@ -5,8 +5,8 @@
* @param {Promise} callback function to call when the timer expires
*/
const scheduleTimer = function (timer, intervalMS, callback) {
if (process.env.JEST_WORKER_ID === undefined) {
// only set timer when not running in jest
if (process.env.mmTestMode !== "true") {
// only set timer when not running in test mode
let tmr = timer;
clearTimeout(tmr);
tmr = setTimeout(function () {

View File

@@ -34,7 +34,7 @@ module.exports = {
].join("\n");
Log.info(systemDataString);
// Return is currently only for jest
// Return is currently only for tests
return systemDataString;
} catch (error) {
Log.error(error);
@@ -65,8 +65,10 @@ module.exports = {
if (results && results.length > 0) {
// get the position parts and replace space with underscore
const positionName = results[1].replace(" ", "_");
// add it to the list
modulePositions.push(positionName);
// add it to the list only if not already present (avoid duplicates)
if (!modulePositions.includes(positionName)) {
modulePositions.push(positionName);
}
}
});
try {