11import { vi as vitest } from 'vitest' ;
22import type { Mock } from '@vitest/spy' ;
33
4- // type-definitions
4+ declare global {
5+ // eslint-disable-next-line no-var
6+ var fetchMock : FetchMock ;
7+
8+ // eslint-disable-next-line @typescript-eslint/no-namespace
9+ namespace NodeJS {
10+ interface Global {
11+ fetchMock : FetchMock ;
12+ }
13+ }
14+ }
15+
516export type FetchMock = Mock < typeof global . fetch > & FetchMockObject ;
617
718class FetchMockObject {
@@ -16,6 +27,7 @@ class FetchMockObject {
1627 // enable/disable
1728 enableMocks ( ) : FetchMock {
1829 globalThis . fetch = this . mockedFetch ;
30+ globalThis . fetchMock = this . chainingResultProvider ( ) ;
1931 return this . chainingResultProvider ( ) ;
2032 }
2133
@@ -324,7 +336,7 @@ export interface MockResponse extends MockParams {
324336export default function createFetchMock ( vi : typeof vitest ) : FetchMock {
325337 const isMocking = vi . fn ( always ( true ) ) ;
326338
327- const originalFetch = globalThis . fetch ;
339+ const originalFetch = globalThis . fetch ;
328340 const mockedFetch = vi . fn ( ( input , requestInit ) => {
329341 if ( ! isMocking ( input , requestInit ) ) {
330342 return originalFetch ( input , requestInit ) ;
@@ -333,7 +345,7 @@ export default function createFetchMock(vi: typeof vitest): FetchMock {
333345 } ) as FetchMock ;
334346
335347 const fetchMock : FetchMock = mockedFetch as FetchMock ;
336- const fetchMockObject = new FetchMockObject ( mockedFetch , globalThis . fetch , ( ) => fetchMock ) ;
348+ const fetchMockObject = new FetchMockObject ( mockedFetch , originalFetch , ( ) => fetchMock ) ;
337349
338350 copyMethods ( fetchMockObject , fetchMock ) ;
339351
0 commit comments