-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move
createLogger
into its own file (#462)
- Loading branch information
Showing
6 changed files
with
66 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface Logger { | ||
debug: (message: string) => unknown; | ||
info: (message: string) => unknown; | ||
warn: (message: string) => unknown; | ||
error: (message: string) => unknown; | ||
} | ||
|
||
export const createLogger = (logger?: Partial<Logger>): Logger => ({ | ||
debug: () => {}, | ||
info: () => {}, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console), | ||
...logger, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createLogger } from "../../src/createLogger"; | ||
|
||
const noop = () => {}; | ||
|
||
describe("createLogger", () => { | ||
jest.spyOn(console, "warn").mockImplementation(noop); | ||
jest.spyOn(console, "error").mockImplementation(noop); | ||
|
||
describe("when nothing is passed", () => { | ||
it("provides default implementations for all log levels", () => { | ||
const logger = createLogger(); | ||
|
||
expect(() => logger.debug("hello world")).not.toThrow(); | ||
expect(() => logger.info("hello world")).not.toThrow(); | ||
expect(() => logger.warn("hello world")).not.toThrow(); | ||
expect(() => logger.error("hello world")).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe("when some log levels are provided", () => { | ||
const partialLogger = { | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
|
||
it("uses the provided implementations for the given log levels", () => { | ||
const logger = createLogger(partialLogger); | ||
|
||
logger.debug("hello world"); | ||
logger.error("hello world"); | ||
|
||
expect(partialLogger.debug).toHaveBeenCalledTimes(1); | ||
expect(partialLogger.debug).toHaveBeenCalledWith("hello world"); | ||
|
||
expect(partialLogger.error).toHaveBeenCalledTimes(1); | ||
expect(partialLogger.error).toHaveBeenCalledWith("hello world"); | ||
}); | ||
|
||
it("provides default implementations for the remaining log levels", () => { | ||
const logger = createLogger(partialLogger); | ||
|
||
expect(() => logger.info("hello world")).not.toThrow(); | ||
expect(() => logger.warn("hello world")).not.toThrow(); | ||
}); | ||
}); | ||
}); |