Skip to content

Commit c62dc71

Browse files
feat: bind console.log always to the same value (#111)
* feat: bind console.log always to the same value * vitest: clearmocks true
1 parent 5758ee8 commit c62dc71

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/greet.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { describe, expect, it, vi } from "vitest";
22

3+
const logger = vi.spyOn(console, "log");
4+
35
import { greet } from "./greet.js";
46

57
const message = "Yay, testing!";
68

79
describe("greet", () => {
810
it("logs to the console once when message is provided as a string", () => {
9-
const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);
11+
logger.mockImplementation(() => undefined);
1012

1113
greet(message);
1214

@@ -15,7 +17,7 @@ describe("greet", () => {
1517
});
1618

1719
it("logs to the console once when message is provided as an object", () => {
18-
const logger = vi.spyOn(console, "log").mockImplementation(() => undefined);
20+
logger.mockImplementation(() => undefined);
1921

2022
greet({ message });
2123

src/greet.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { GreetOptions } from "./types.js";
22

3+
const consoleLogBound = console.log.bind(console);
4+
35
export function greet(options: GreetOptions | string) {
46
const {
5-
logger = console.log.bind(console),
7+
logger = consoleLogBound,
68
message,
79
times = 1,
810
} = typeof options === "string" ? { message: options } : options;

vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineConfig } from "vitest/config";
22

33
export default defineConfig({
44
test: {
5+
clearMocks: true,
56
coverage: {
67
all: true,
78
include: ["src"],

0 commit comments

Comments
 (0)