2021-12-16 13:36:21 +01:00
|
|
|
import { ConsoleLogService } from "jslib-common/services/consoleLog.service";
|
2020-12-11 17:44:57 +01:00
|
|
|
|
|
|
|
const originalConsole = console;
|
|
|
|
let caughtMessage: any;
|
|
|
|
|
2022-02-22 15:39:11 +01:00
|
|
|
declare let console: any;
|
2020-12-11 17:44:57 +01:00
|
|
|
|
|
|
|
export function interceptConsole(interceptions: any): object {
|
2021-12-16 13:36:21 +01:00
|
|
|
console = {
|
|
|
|
log: function () {
|
2022-02-22 15:39:11 +01:00
|
|
|
// eslint-disable-next-line
|
2021-12-16 13:36:21 +01:00
|
|
|
interceptions.log = arguments;
|
|
|
|
},
|
|
|
|
warn: function () {
|
2022-02-22 15:39:11 +01:00
|
|
|
// eslint-disable-next-line
|
2021-12-16 13:36:21 +01:00
|
|
|
interceptions.warn = arguments;
|
|
|
|
},
|
|
|
|
error: function () {
|
2022-02-22 15:39:11 +01:00
|
|
|
// eslint-disable-next-line
|
2021-12-16 13:36:21 +01:00
|
|
|
interceptions.error = arguments;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return interceptions;
|
2020-12-11 17:44:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function restoreConsole() {
|
2021-12-16 13:36:21 +01:00
|
|
|
console = originalConsole;
|
2020-12-11 17:44:57 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:36:21 +01:00
|
|
|
describe("ConsoleLogService", () => {
|
|
|
|
let logService: ConsoleLogService;
|
|
|
|
beforeEach(() => {
|
|
|
|
caughtMessage = {};
|
|
|
|
interceptConsole(caughtMessage);
|
|
|
|
logService = new ConsoleLogService(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
restoreConsole();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("filters messages below the set threshold", () => {
|
2022-02-22 15:39:11 +01:00
|
|
|
logService = new ConsoleLogService(true, () => true);
|
2021-12-16 13:36:21 +01:00
|
|
|
logService.debug("debug");
|
|
|
|
logService.info("info");
|
|
|
|
logService.warning("warning");
|
|
|
|
logService.error("error");
|
|
|
|
|
|
|
|
expect(caughtMessage).toEqual({});
|
|
|
|
});
|
|
|
|
it("only writes debug messages in dev mode", () => {
|
|
|
|
logService = new ConsoleLogService(false);
|
|
|
|
|
|
|
|
logService.debug("debug message");
|
|
|
|
expect(caughtMessage.log).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-12-16 13:36:21 +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
|
|
|
});
|
2021-12-16 13:36:21 +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
|
|
|
});
|
2021-12-16 13:36:21 +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
|
|
|
});
|
|
|
|
|
|
|
|
it("times with output to info", async () => {
|
|
|
|
logService.time();
|
|
|
|
await new Promise((r) => setTimeout(r, 250));
|
|
|
|
const duration = logService.timeEnd();
|
|
|
|
expect(duration[0]).toBe(0);
|
|
|
|
expect(duration[1]).toBeGreaterThan(0);
|
|
|
|
expect(duration[1]).toBeLessThan(500 * 10e6);
|
|
|
|
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage).toEqual(expect.arrayContaining([]));
|
2021-12-16 13:36:21 +01:00
|
|
|
expect(caughtMessage.log.length).toBe(1);
|
2022-03-28 16:00:42 +02:00
|
|
|
expect(caughtMessage.log[0]).toEqual(expect.stringMatching(/^default: \d+\.?\d*ms$/));
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("filters time output", async () => {
|
2022-02-22 15:39:11 +01:00
|
|
|
logService = new ConsoleLogService(true, () => true);
|
2021-12-16 13:36:21 +01:00
|
|
|
logService.time();
|
|
|
|
logService.timeEnd();
|
|
|
|
|
|
|
|
expect(caughtMessage).toEqual({});
|
|
|
|
});
|
2020-12-11 17:44:57 +01:00
|
|
|
});
|