diff --git a/jest.setup.ts b/jest.setup.ts index 2ac8cec..d8d34fd 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -1,3 +1,6 @@ +import { socksDispatcher } from 'fetch-socks'; +import { ProxyAgent, setGlobalDispatcher } from 'undici'; + jest.mock('randomstring', () => ({ generate: jest.fn(() => 'mocked-random-string'), })); @@ -5,3 +8,12 @@ jest.mock('randomstring', () => ({ beforeEach(() => { jest.spyOn(Date, 'now').mockImplementation(() => 639837296000); }); + +if (process.env['ALL_PROXY']) { + const proxyUrl = new URL(process.env['ALL_PROXY']); + if (proxyUrl.protocol === 'socks5:') { + setGlobalDispatcher(socksDispatcher({ type: 5, host: proxyUrl.hostname, port: parseInt(proxyUrl.port) })); + } else { + setGlobalDispatcher(new ProxyAgent({ uri: proxyUrl.href })); + } +}