1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-08-10 22:11:50 +02:00

Chore: Silence expected warning while running tests in lib/ (#12450)

This commit is contained in:
Henry Heino
2025-06-10 16:13:13 -07:00
committed by GitHub
parent 7a611ac5c5
commit 303ccce7d2
2 changed files with 23 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
import { ErrorCode } from '../errors';
import { FolderEntity } from '../services/database/types';
import { createNTestNotes, setupDatabaseAndSynchronizer, sleep, switchClient, checkThrowAsync, createFolderTree, simulateReadOnlyShareEnv, expectThrow } from '../testing/test-utils';
import { createNTestNotes, setupDatabaseAndSynchronizer, sleep, switchClient, checkThrowAsync, createFolderTree, simulateReadOnlyShareEnv, expectThrow, withWarningSilenced } from '../testing/test-utils';
import Folder from './Folder';
import Note from './Note';
@@ -174,8 +174,10 @@ describe('models/Folder', () => {
await Folder.save({ id: f1.id, parent_id: f3.id });
const folders = await Folder.all();
// Should not loop indefinitely:
await Folder.addNoteCounts(folders);
// Should not loop indefinitely, okay to warn:
await withWarningSilenced(/has itself as a parent/, async () => {
await Folder.addNoteCounts(folders);
});
// Note count may be incorrect
expect(folders.find(folder => folder.id === f1.id)).toHaveProperty('note_count');
});

View File

@@ -1190,4 +1190,22 @@ export const runWithFakeTimers = async (callback: ()=> Promise<void>) => {
}
};
export const withWarningSilenced = async <T> (warningRegex: RegExp, task: ()=> Promise<T>): Promise<T> => {
// See https://jestjs.io/docs/jest-object#spied-methods-and-the-using-keyword, which
// shows how to use .spyOn to hide warnings
let warningMock;
try {
warningMock = jest.spyOn(console, 'warn');
warningMock.mockImplementation((message, ...args) => {
const fullMessage = [message, ...args].join(' ');
if (!fullMessage.match(warningRegex)) {
console.error(`Unexpected warning: ${message}`, ...args);
}
});
return await task();
} finally {
warningMock.mockRestore();
}
};
export { supportDir, createNoteAndResource, createTempFile, createTestShareData, simulateReadOnlyShareEnv, waitForFolderCount, afterAllCleanUp, exportDir, synchronizerStart, afterEachCleanUp, syncTargetName, setSyncTargetName, syncDir, createTempDir, isNetworkSyncTarget, kvStore, expectThrow, logger, expectNotThrow, resourceService, resourceFetcher, tempFilePath, allSyncTargetItemsEncrypted, msleep, setupDatabase, revisionService, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync, checkThrow, encryptionService, loadEncryptionMasterKey, fileContentEqual, decryptionWorker, currentClientId, id, ids, sortedIds, at, createNTestNotes, createNTestFolders, createNTestTags, TestApp };