postchain-client
Version:
Client library for accessing a Postchain node through REST.
95 lines • 4.44 kB
JavaScript
import { debug, info, error, warning, setLogLevel, getLogLevel, logger } from "../../src/logger";
import { LogLevel } from "../../src/logger";
describe("logger", () => {
let consoleSpy;
beforeEach(() => {
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => undefined);
});
afterEach(() => {
consoleSpy.mockRestore();
});
it("should log debug message when log level is set to debug", () => {
setLogLevel(LogLevel.Debug);
debug("Debug message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("TestModule"));
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Debug message"));
});
it("should NOT log debug message when log level is set to warning", () => {
setLogLevel(LogLevel.Warning);
debug("Debug message", "TestModule");
expect(consoleSpy).not.toHaveBeenCalled();
});
it("should log info message when log level is set to info", () => {
setLogLevel(LogLevel.Info);
info("Info message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("TestModule"));
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Info message"));
});
it("should log error message when log level is set to error", () => {
setLogLevel(LogLevel.Error);
error("Error message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("TestModule"));
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Error message"));
});
it("should log warning message when log level is set to warning", () => {
setLogLevel(LogLevel.Warning);
warning("Warning message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("TestModule"));
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Warning message"));
});
it("should get the current log level", () => {
setLogLevel(LogLevel.Debug);
expect(getLogLevel()).toEqual(LogLevel.Debug);
});
it("should create a logger instance with the specified module name", () => {
const log = logger("TestModule");
log.debug("Debug message");
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("TestModule"));
});
it("should only log messages for the specified log level: DEBUG", () => {
setLogLevel(LogLevel.Debug);
debug("Debug message");
info("Info message", "TestModule");
warning("Warning message", "TestModule");
error("Error message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(4);
});
it("should only log messages for the specified log level: INFO", () => {
setLogLevel(LogLevel.Info);
debug("Debug message");
info("Info message", "TestModule");
warning("Warning message", "TestModule");
error("Error message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(3);
});
it("should only log messages for the specified log level: WARNING", () => {
setLogLevel(LogLevel.Warning);
debug("Debug message");
info("Info message", "TestModule");
warning("Warning message", "TestModule");
error("Error message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(2);
});
it("should only log messages for the specified log level: ERROR", () => {
setLogLevel(LogLevel.Error);
debug("Debug message");
info("Info message", "TestModule");
warning("Warning message", "TestModule");
error("Error message", "TestModule");
expect(consoleSpy).toHaveBeenCalledTimes(1);
});
it("should only log messages for the specified log level: DISABLED", () => {
setLogLevel(LogLevel.Disabled);
debug("Debug message");
info("Info message", "TestModule");
warning("Warning message", "TestModule");
error("Error message", "TestModule");
expect(consoleSpy).not.toHaveBeenCalled();
});
});
//# sourceMappingURL=logger.test.js.map