File tree Expand file tree Collapse file tree 3 files changed +32
-10
lines changed Expand file tree Collapse file tree 3 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 1
1
import { vi as vitest } from 'vitest' ;
2
2
import type { Mock } from '@vitest/spy' ;
3
3
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
+
5
16
export type FetchMock = Mock < typeof global . fetch > & FetchMockObject ;
6
17
7
18
class FetchMockObject {
@@ -16,6 +27,7 @@ class FetchMockObject {
16
27
// enable/disable
17
28
enableMocks ( ) : FetchMock {
18
29
globalThis . fetch = this . mockedFetch ;
30
+ globalThis . fetchMock = this . chainingResultProvider ( ) ;
19
31
return this . chainingResultProvider ( ) ;
20
32
}
21
33
@@ -333,7 +345,7 @@ export default function createFetchMock(vi: typeof vitest): FetchMock {
333
345
} ) as FetchMock ;
334
346
335
347
const fetchMock : FetchMock = mockedFetch as FetchMock ;
336
- const fetchMockObject = new FetchMockObject ( mockedFetch , globalThis . fetch , ( ) => fetchMock ) ;
348
+ const fetchMockObject = new FetchMockObject ( mockedFetch , originalFetch , ( ) => fetchMock ) ;
337
349
338
350
copyMethods ( fetchMockObject , fetchMock ) ;
339
351
Original file line number Diff line number Diff line change @@ -747,6 +747,16 @@ describe('conditional mocking', () => {
747
747
} ) ;
748
748
} ) ;
749
749
750
+ it ( 'works globally' , async ( ) => {
751
+ const fm = createFetchMock ( vi ) ;
752
+ fm . enableMocks ( ) ;
753
+
754
+ fetchMock . mockResponseOnce ( 'foo' ) ;
755
+ expect ( await request ( ) ) . toBe ( 'foo' ) ;
756
+
757
+ fm . disableMocks ( ) ;
758
+ } ) ;
759
+
750
760
it ( 'enable/disable' , async ( ) => {
751
761
expect ( vi . isMockFunction ( globalThis . fetch ) ) . toBe ( false ) ;
752
762
const fetch = createFetchMock ( vi ) ;
Original file line number Diff line number Diff line change 1
1
import setupFm , { type MockResponse } from 'vitest-fetch-mock' ;
2
2
import { vi } from 'vitest' ;
3
3
4
- const fetchMock = setupFm ( vi ) ;
4
+ const fm = setupFm ( vi ) ;
5
5
6
6
fetchMock . mockResponse ( JSON . stringify ( { foo : "bar" } ) ) ;
7
7
fetchMock . mockResponse ( JSON . stringify ( { foo : "bar" } ) , {
@@ -108,10 +108,10 @@ function someSyncStringHandler(): string {
108
108
return JSON . stringify ( { foo : "bar" } ) ;
109
109
}
110
110
111
- fetchMock . enableMocks ( ) ;
112
- fetchMock . disableMocks ( ) ;
113
- fetchMock . doMock ( ) ;
114
- fetchMock . dontMock ( ) ;
115
- fetchMock . doMockOnce ( ) ;
116
- fetchMock . dontMockOnce ( ) ;
117
- fetchMock . mockOnce ( ) ;
111
+ fm . enableMocks ( ) ;
112
+ fm . disableMocks ( ) ;
113
+ fm . doMock ( ) ;
114
+ fm . dontMock ( ) ;
115
+ fm . doMockOnce ( ) ;
116
+ fm . dontMockOnce ( ) ;
117
+ fm . mockOnce ( ) ;
You can’t perform that action at this time.
0 commit comments