Skip to content

Commit 100c8ef

Browse files
committed
Fix tests
1 parent 11ee260 commit 100c8ef

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

packages/hardhat-verify/src/internal/undici.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export async function sendPostRequest(
2828
});
2929
}
3030

31+
let mockDispatcher: Undici.Dispatcher | undefined;
32+
3133
function getDispatcher(): Undici.Dispatcher {
34+
if (mockDispatcher !== undefined) {
35+
return mockDispatcher;
36+
}
37+
3238
const { ProxyAgent, Agent } = require("undici") as typeof Undici;
3339
if (process.env.http_proxy !== undefined) {
3440
return new ProxyAgent(process.env.http_proxy);
@@ -37,6 +43,12 @@ function getDispatcher(): Undici.Dispatcher {
3743
return new Agent();
3844
}
3945

46+
export function setMockDispatcher(
47+
dispatcher: Undici.Dispatcher | undefined
48+
): void {
49+
mockDispatcher = dispatcher;
50+
}
51+
4052
export function isSuccessStatusCode(statusCode: number): boolean {
4153
return statusCode >= 200 && statusCode <= 299;
4254
}

packages/hardhat-verify/test/integration/mocks/etherscan.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
Dispatcher,
3-
getGlobalDispatcher,
4-
MockAgent,
5-
setGlobalDispatcher,
6-
} from "undici";
1+
import { MockAgent } from "undici";
2+
import { setMockDispatcher } from "../../../src/internal/undici";
73

84
const mockAgent = new MockAgent({
95
keepAliveTimeout: 10,
@@ -13,17 +9,15 @@ const mockAgent = new MockAgent({
139
const client = mockAgent.get("https://api-hardhat.etherscan.io");
1410

1511
export const mockEnvironment = () => {
16-
let globalDispatcher: Dispatcher;
1712
// enable network connections for everything but etherscan API
1813
mockAgent.enableNetConnect(/^(?!https:\/\/api-hardhat\.etherscan\.io)/);
1914

2015
before(() => {
21-
globalDispatcher = getGlobalDispatcher();
22-
setGlobalDispatcher(mockAgent);
16+
setMockDispatcher(mockAgent);
2317
});
2418

2519
after(() => {
26-
setGlobalDispatcher(globalDispatcher);
20+
setMockDispatcher(undefined);
2721
});
2822
};
2923

packages/hardhat-verify/test/integration/mocks/sourcify.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
Dispatcher,
3-
getGlobalDispatcher,
4-
MockAgent,
5-
setGlobalDispatcher,
6-
} from "undici";
1+
import { MockAgent } from "undici";
2+
import { setMockDispatcher } from "../../../src/internal/undici";
73

84
const mockAgent = new MockAgent({
95
keepAliveTimeout: 10,
@@ -13,17 +9,15 @@ const mockAgent = new MockAgent({
139
const client = mockAgent.get("https://sourcify.dev");
1410

1511
export const mockEnvironmentSourcify = () => {
16-
let globalDispatcher: Dispatcher;
1712
// enable network connections for everything but etherscan API
1813
mockAgent.enableNetConnect(/^(?!https:\/\/sourcify\.dev)/);
1914

2015
before(() => {
21-
globalDispatcher = getGlobalDispatcher();
22-
setGlobalDispatcher(mockAgent);
16+
setMockDispatcher(mockAgent);
2317
});
2418

2519
after(() => {
26-
setGlobalDispatcher(globalDispatcher);
20+
setMockDispatcher(undefined);
2721
});
2822
};
2923

0 commit comments

Comments
 (0)