1
1
import { Server } from "node:net" ;
2
- import { describe , test , expect , afterEach , vi } from "vitest" ;
2
+ import { describe , test , expect , beforeEach , afterEach , vi } from "vitest" ;
3
3
import { getPort , getRandomPort } from "../src" ;
4
4
import { _generateRange } from "../src/_internal" ;
5
5
import { blockPort } from "./utils" ;
@@ -90,19 +90,24 @@ describe("random port", () => {
90
90
} ) ;
91
91
92
92
describe ( "errors" , ( ) => {
93
+ beforeEach ( ( ) => {
94
+ vi . spyOn ( console , "log" ) . mockImplementation ( ( ) => undefined ) ;
95
+ } ) ;
96
+
97
+ afterEach ( ( ) => {
98
+ vi . resetAllMocks ( ) ;
99
+ } ) ;
100
+
93
101
test ( "invalid hostname" , async ( ) => {
94
- vi . spyOn ( console , "log" ) ;
95
102
await getPort ( { host : "http://localhost:8080" , verbose : true } ) . catch (
96
103
( error ) => error ,
97
104
) ;
98
105
expect ( console . log ) . toHaveBeenCalledWith (
99
106
'[get-port] Invalid hostname: "http://localhost:8080". Using "127.0.0.1" as fallback.' ,
100
107
) ;
101
- vi . resetAllMocks ( ) ;
102
108
} ) ;
103
109
104
110
test ( "invalid hostname (public)" , async ( ) => {
105
- vi . spyOn ( console , "log" ) ;
106
111
await getPort ( {
107
112
host : "http://localhost:8080" ,
108
113
verbose : true ,
@@ -111,7 +116,6 @@ describe("errors", () => {
111
116
expect ( console . log ) . toHaveBeenCalledWith (
112
117
'[get-port] Invalid hostname: "http://localhost:8080". Using "0.0.0.0" as fallback.' ,
113
118
) ;
114
- vi . resetAllMocks ( ) ;
115
119
} ) ;
116
120
117
121
test . skipIf ( isWindows ) ( "unavailable port" , async ( ) => {
0 commit comments