From c62dc7134408bb726af16550499a560fe117dee6 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 14 Dec 2022 15:57:07 -0500 Subject: [PATCH] feat: bind console.log always to the same value (#111) * feat: bind console.log always to the same value * vitest: clearmocks true --- src/greet.test.ts | 6 ++++-- src/greet.ts | 4 +++- vitest.config.ts | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/greet.test.ts b/src/greet.test.ts index f729115fc..bbebd1e67 100644 --- a/src/greet.test.ts +++ b/src/greet.test.ts @@ -1,12 +1,14 @@ import { describe, expect, it, vi } from "vitest"; +const logger = vi.spyOn(console, "log"); + import { greet } from "./greet.js"; const message = "Yay, testing!"; describe("greet", () => { it("logs to the console once when message is provided as a string", () => { - const logger = vi.spyOn(console, "log").mockImplementation(() => undefined); + logger.mockImplementation(() => undefined); greet(message); @@ -15,7 +17,7 @@ describe("greet", () => { }); it("logs to the console once when message is provided as an object", () => { - const logger = vi.spyOn(console, "log").mockImplementation(() => undefined); + logger.mockImplementation(() => undefined); greet({ message }); diff --git a/src/greet.ts b/src/greet.ts index a0d3b4c67..4941d28a5 100644 --- a/src/greet.ts +++ b/src/greet.ts @@ -1,8 +1,10 @@ import { GreetOptions } from "./types.js"; +const consoleLogBound = console.log.bind(console); + export function greet(options: GreetOptions | string) { const { - logger = console.log.bind(console), + logger = consoleLogBound, message, times = 1, } = typeof options === "string" ? { message: options } : options; diff --git a/vitest.config.ts b/vitest.config.ts index 4a5bf38ef..f6fe94f69 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { + clearMocks: true, coverage: { all: true, include: ["src"],