Skip to content

Commit 0ce7d3a

Browse files
authored
test: mock console.log implementation in error tests (#104)
1 parent 5e52f8e commit 0ce7d3a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

test/index.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Server } from "node:net";
2-
import { describe, test, expect, afterEach, vi } from "vitest";
2+
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
33
import { getPort, getRandomPort } from "../src";
44
import { _generateRange } from "../src/_internal";
55
import { blockPort } from "./utils";
@@ -90,19 +90,24 @@ describe("random port", () => {
9090
});
9191

9292
describe("errors", () => {
93+
beforeEach(() => {
94+
vi.spyOn(console, "log").mockImplementation(() => undefined);
95+
});
96+
97+
afterEach(() => {
98+
vi.resetAllMocks();
99+
});
100+
93101
test("invalid hostname", async () => {
94-
vi.spyOn(console, "log");
95102
await getPort({ host: "http://localhost:8080", verbose: true }).catch(
96103
(error) => error,
97104
);
98105
expect(console.log).toHaveBeenCalledWith(
99106
'[get-port] Invalid hostname: "http://localhost:8080". Using "127.0.0.1" as fallback.',
100107
);
101-
vi.resetAllMocks();
102108
});
103109

104110
test("invalid hostname (public)", async () => {
105-
vi.spyOn(console, "log");
106111
await getPort({
107112
host: "http://localhost:8080",
108113
verbose: true,
@@ -111,7 +116,6 @@ describe("errors", () => {
111116
expect(console.log).toHaveBeenCalledWith(
112117
'[get-port] Invalid hostname: "http://localhost:8080". Using "0.0.0.0" as fallback.',
113118
);
114-
vi.resetAllMocks();
115119
});
116120

117121
test.skipIf(isWindows)("unavailable port", async () => {

0 commit comments

Comments
 (0)