/* * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors * SPDX-License-Identifier: AGPL-3.0-only */ import { jest } from '@jest/globals'; import { Injectable } from '@nestjs/common'; import { bindThis } from '@/decorators.js'; /** * Console implementation where all members are jest mocks. */ @Injectable() export class MockConsole implements Console { public readonly Console = MockConsole; /** * Resets all mocks in the console. */ @bindThis public mockReset(): void { for (const func of Object.values(this)) { if (typeof(func) === 'function' && 'mockReset' in func) { func.mockReset(); } } } /** * Asserts that no errors and/or warnings have been logged. */ @bindThis public assertNoErrors(opts?: { orWarnings?: boolean }): void { expect(this.error).not.toHaveBeenCalled(); if (opts?.orWarnings) { expect(this.warn).not.toHaveBeenCalled(); } } public readonly error = jest.fn(); public readonly warn = jest.fn(); public readonly info = jest.fn(); public readonly log = jest.fn(); public readonly debug = jest.fn(); public readonly trace = jest.fn(); public readonly assert = jest.fn(); public readonly clear = jest.fn(); public readonly count = jest.fn(); public readonly countReset = jest.fn(); public readonly dir = jest.fn(); public readonly dirxml = jest.fn(); public readonly group = jest.fn(); public readonly groupCollapsed = jest.fn(); public readonly groupEnd = jest.fn(); public readonly table = jest.fn(); public readonly time = jest.fn(); public readonly timeEnd = jest.fn(); public readonly timeLog = jest.fn(); public readonly profile = jest.fn(); public readonly profileEnd = jest.fn(); public readonly timeStamp = jest.fn(); }