2022-06-14 17:10:53 +02:00
|
|
|
import { ConsoleLogService } from "@bitwarden/common/services/consoleLog.service";
|
2020-12-11 17:44:57 +01:00
|
|
|
|
2022-09-08 00:02:53 +02:00
|
|
|
import { interceptConsole, restoreConsole } from "../shared/interceptConsole";
|
2020-12-11 17:44:57 +01:00
|
|
|
|
2022-09-08 00:02:53 +02:00
|
|
|
let caughtMessage: any;
|
2020-12-11 17:44:57 +01:00
|
|
|
|
|
|
|
describe("ConsoleLogService", () => {
|
|
|
|
let logService: ConsoleLogService;
|
|
|
|
beforeEach(() => {
|
|
|
|
caughtMessage = {};
|
|
|
|
interceptConsole(caughtMessage);
|
|
|
|
logService = new ConsoleLogService(true);
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-12-11 17:44:57 +01:00
|
|
|
afterAll(() => {
|
|
|
|
restoreConsole();
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-12-11 17:44:57 +01:00
|
|
|
it("filters messages below the set threshold", () => {
|
2022-02-22 15:39:11 +01:00
|
|
|
logService = new ConsoleLogService(true, () => true);
|
2020-12-11 17:44:57 +01:00
|
|
|
logService.debug("debug");
|
|
|
|
logService.info("info");
|
|
|
|
logService.warning("warning");
|
|
|
|
logService.error("error");
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-11 17:44:57 +01:00
|
|
|
expect(caughtMessage).toEqual({});
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
2020-12-11 17:44:57 +01:00
|
|
|
it("only writes debug messages in dev mode", () => {
|
|
|
|
logService = new ConsoleLogService(false);
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-11 17:44:57 +01:00
|
|
|
logService.debug("debug message");
|
|
|
|
expect(caughtMessage.log).toBeUndefined();
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
2020-12-11 17:44:57 +01:00
|
|
|
it("writes debug/info messages to console.log", () => {
|
|
|
|
logService.debug("this is a debug message");
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage).toMatchObject({
|
|
|
|
log: { "0": "this is a debug message" },
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
logService.info("this is an info message");
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage).toMatchObject({
|
|
|
|
log: { "0": "this is an info message" },
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("writes warning messages to console.warn", () => {
|
|
|
|
logService.warning("this is a warning message");
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage).toMatchObject({
|
|
|
|
warn: { 0: "this is a warning message" },
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("writes error messages to console.error", () => {
|
|
|
|
logService.error("this is an error message");
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage).toMatchObject({
|
|
|
|
error: { 0: "this is an error message" },
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|