feat: Use Vitest for testing (#25284)

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
NerivecandKoen Kanters authored and GitHub committed 2024-12-22 20:19:45 +01:00
1 parent 5d059b7bcb
commit 06f44533af
51 files changed
+2606 -4771

No files matched your search

+14 -13
View File
@@ -59,11 +59,9 @@ class Logger {
// winston.config.syslog.levels sets 'warning' as 'red'
format: winston.format.combine(
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
winston.format.printf(
/* istanbul ignore next */ (info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
},
),
winston.format.printf((info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
),
}),
);
@@ -77,10 +75,13 @@ class Logger {
if (settings.get().advanced.log_symlink_current) {
const current = settings.get().advanced.log_directory.replace('%TIMESTAMP%', 'current');
const actual = './' + timestamp;
/* istanbul ignore next */
/* v8 ignore start */
if (fs.existsSync(current)) {
fs.unlinkSync(current);
}
/* v8 ignore stop */
fs.symlinkSync(actual, current);
}
@@ -88,11 +89,9 @@ class Logger {
// NOTE: the initiation of the logger even when not added as transport tries to create the logging directory
const transportFileOptions: winston.transports.FileTransportOptions = {
filename: path.join(this.directory, logFilename),
format: winston.format.printf(
/* istanbul ignore next */ (info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
},
),
format: winston.format.printf((info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
};
if (settings.get().advanced.log_rotation) {
@@ -106,7 +105,7 @@ class Logger {
this.cleanup();
}
/* istanbul ignore next */
/* v8 ignore start */
if (this.output.includes('syslog')) {
logging += `, syslog`;
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-expressions
@@ -125,6 +124,7 @@ class Logger {
// @ts-expect-error untyped transport
this.logger.add(new winston.transports.Syslog(options));
}
/* v8 ignore stop */
this.setDebugNamespaceIgnore(settings.get().advanced.log_debug_namespace_ignore);
@@ -240,7 +240,7 @@ class Logger {
// Workaround for https://github.com/winstonjs/winston/issues/1629.
// https://github.com/Koenkk/zigbee2mqtt/pull/10905
/* istanbul ignore next */
/* v8 ignore start */
public async end(): Promise<void> {
this.logger.end();
@@ -259,6 +259,7 @@ class Logger {
}
});
}
/* v8 ignore stop */
}
export default new Logger();
+1 -2
View File
@@ -87,7 +87,7 @@ export const defaults: RecursivePartial<Settings> = {
log_output: ['console', 'file'],
log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'),
log_file: 'log.log',
log_level: /* istanbul ignore next */ process.env.DEBUG ? 'debug' : 'info',
log_level: /* v8 ignore next */ process.env.DEBUG ? 'debug' : 'info',
log_namespaced_levels: {},
log_syslog: {},
log_debug_to_mqtt_frontend: false,
@@ -353,7 +353,6 @@ function applyEnvironmentVariables(settings: Partial<Settings>): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = (envVariable.toLowerCase() === 'true') as any;
} else {
/* istanbul ignore else */
if (type.indexOf('string') >= 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = envVariable as any;
+4 -7
View File
@@ -54,14 +54,15 @@ function setValue(currentSettings: any, path: string[], value: unknown, createPa
currentSettings[key] = value;
} else {
if (!currentSettings[key]) {
/* istanbul ignore else */
if (createPathIfNotExist) {
currentSettings[key] = {};
/* v8 ignore start */
} else {
// invalid path
// ignored in test since currently call is always guarded by get-validated path, so this is never reached
return false;
}
/* v8 ignore stop */
}
currentSettings = currentSettings[key];
@@ -407,7 +408,6 @@ function migrateToThree(
const changeToObject = (currentSettings: Partial<Settings>, path: string[]): ReturnType<SettingsCustomHandler['execute']> => {
const [validPath, previousValue] = getValue(currentSettings, path);
/* istanbul ignore else */
if (validPath) {
if (typeof previousValue === 'boolean') {
setValue(currentSettings, path, {enabled: previousValue});
@@ -456,8 +456,8 @@ export function migrateIfNecessary(): void {
);
}
/* istanbul ignore next */
const finalVersion = process.env.JEST_WORKER_ID ? settings.testing.CURRENT_VERSION : settings.CURRENT_VERSION;
/* v8 ignore next */
const finalVersion = process.env.VITEST_WORKER_ID ? settings.testing.CURRENT_VERSION : settings.CURRENT_VERSION;
// when same version as current, nothing left to do
while (currentSettings.version !== finalVersion) {
@@ -473,7 +473,6 @@ export function migrateIfNecessary(): void {
backupSettings(currentSettings.version || 1);
// each version should only bump to the next version so as to gradually migrate if necessary
/* istanbul ignore else */
if (currentSettings.version == undefined) {
// migrating from 1 (`version` did not exist) to 2
migrationNotesFileName = 'migration-1-to-2.log';
@@ -518,13 +517,11 @@ export function migrateIfNecessary(): void {
for (const customHandler of customHandlers) {
const [validPath, previousValue, changed] = customHandler.execute(currentSettings);
/* istanbul ignore else */
if (validPath && changed && (!customHandler.noteIf || customHandler.noteIf(previousValue))) {
migrationNotes.add(`[SPECIAL] ${customHandler.note}`);
}
}
/* istanbul ignore else */
if (migrationNotesFileName && migrationNotes.size > 0) {
migrationNotes.add(`For more details, see https://github.com/Koenkk/zigbee2mqtt/discussions/24198`);
const migrationNotesFilePath = data.joinPath(migrationNotesFileName);
+3 -2
View File
@@ -61,10 +61,11 @@ async function getZigbee2MQTTVersion(includeCommitHash = true): Promise<{commitH
if (err) {
try {
commitHash = fs.readFileSync(path.join(__dirname, '..', '..', 'dist', '.hash'), 'utf-8');
/* v8 ignore start */
} catch {
/* istanbul ignore next */
commitHash = 'unknown';
}
/* v8 ignore stop */
} else {
commitHash = commit.shortHash;
}
@@ -369,7 +370,7 @@ function deviceNotCoordinator(device: zh.Device): boolean {
return device.type !== 'Coordinator';
}
/* istanbul ignore next */
/* v8 ignore next */
const noop = (): void => {};
export default {