Skip to content

Commit

Permalink
refactor: move createLogger into its own file (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Feb 12, 2021
1 parent 245cee3 commit 7e42ba9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
14 changes: 14 additions & 0 deletions src/createLogger.ts
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,
});
12 changes: 1 addition & 11 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createLogger } from "../createLogger";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
HandlerFunction,
Logger,
Options,
State,
WebhookEventHandlerError,
Expand Down Expand Up @@ -49,13 +49,3 @@ export function createEventHandler<TTransformed>(
receive: receive.bind(null, state),
};
}

export function createLogger(logger?: Partial<Logger>) {
return {
debug: () => {},
info: /* istanbul ignore next: unused */ () => {},
warn: console.warn.bind(console),
error: console.error.bind(console),
...logger,
};
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IncomingMessage, ServerResponse } from "http";
import { createEventHandler, createLogger } from "./event-handler/index";
import { createLogger } from "./createLogger";
import { createEventHandler } from "./event-handler/index";
import { createMiddleware } from "./middleware/index";
import { middleware } from "./middleware/middleware";
import { verifyAndReceive } from "./middleware/verify-and-receive";
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { debug } from "debug";
import { createEventHandler, createLogger } from "../event-handler/index";
import { createLogger } from "../createLogger";
import { createEventHandler } from "../event-handler/index";
import { middleware } from "./middleware";
import { Options, State } from "../types";

Expand Down
8 changes: 1 addition & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
WebhookEventMap,
WebhookEventName,
} from "@octokit/webhooks-definitions/schema";
import { Logger } from "./createLogger";
import type { emitterEventNames } from "./generated/webhook-names";

export type EmitterWebhookEventName = typeof emitterEventNames[number];
Expand Down Expand Up @@ -38,13 +39,6 @@ type Hooks = {
[key: string]: Function[];
};

export interface Logger {
debug: (message: string) => unknown;
info: (message: string) => unknown;
warn: (message: string) => unknown;
error: (message: string) => unknown;
}

export interface State extends Options<any> {
eventHandler?: any;
hooks: Hooks;
Expand Down
46 changes: 46 additions & 0 deletions test/unit/createLogger-test.ts
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();
});
});
});

0 comments on commit 7e42ba9

Please sign in to comment.