@@ -13,8 +13,8 @@ requests. It's easy to setup and you don't need a library like `nock` to get goi
13
13
for mocking under the surface. This means that any of the ` vi.fn() ` methods are also available. For more information on
14
14
the vitest mock API, check their docs [ here] ( https://vitest.dev/guide/mocking.html )
15
15
16
- It currently supports the mocking with the [ ` cross-fetch ` ] ( https://www.npmjs.com/package/cross- fetch ) polyfill, so it
17
- supports Node.js and any browser-like runtime .
16
+ As of version 0.4.0, ` vitest-fetch-mock ` mocks the global [ Fetch ] ( https://developer.mozilla.org/en-US/docs/Web/API/Window/ fetch ) method,
17
+ which should be present in all modern runtimes and browsers .
18
18
19
19
## Contents
20
20
@@ -631,8 +631,6 @@ the most used ones.
631
631
``` js
632
632
// api.js
633
633
634
- import ' cross-fetch' ;
635
-
636
634
export function APIRequest (who ) {
637
635
if (who === ' facebook' ) {
638
636
return fetch (' https://facebook.com' );
@@ -749,16 +747,14 @@ For convenience, all the conditional mocking functions also accept optional para
749
747
#### Conditional Mocking examples
750
748
751
749
``` js
752
- import crossFetch from ' cross-fetch' ;
753
-
754
- vi .mock (' cross-fetch' , async () => {
755
- const crossFetchActual = await requireActual (' cross-fetch' );
750
+ vi .mock (' fetch' , async () => {
751
+ const fetchActual = globalThis .fetch ;
756
752
const mocked = {};
757
- for (const key of Object .keys (crossFetchActual )) {
758
- if (typeof crossFetchActual [key] === ' function' ) {
759
- mocked[key] = vi .fn (crossFetchActual [key]);
753
+ for (const key of Object .keys (fetchActual )) {
754
+ if (typeof fetchActual [key] === ' function' ) {
755
+ mocked[key] = vi .fn (fetchActual [key]);
760
756
} else {
761
- mocked[key] = crossFetchActual [key];
757
+ mocked[key] = fetchActual [key];
762
758
}
763
759
}
764
760
return mocked;
@@ -768,18 +764,18 @@ describe('conditional mocking', () => {
768
764
const realResponse = ' REAL FETCH RESPONSE'
769
765
const mockedDefaultResponse = ' MOCKED DEFAULT RESPONSE'
770
766
const testUrl = defaultRequestUri
771
- let crossFetchSpy
767
+ let fetchSpy
772
768
beforeEach (() => {
773
769
fetch .resetMocks ()
774
770
fetch .mockResponse (mockedDefaultResponse)
775
- vi .mocked (crossFetch )
771
+ vi .mocked (fetch )
776
772
.mockImplementation (async () =>
777
773
Promise .resolve (new Response (realResponse))
778
774
)
779
775
})
780
776
781
777
afterEach (() => {
782
- vi .mocked (crossFetch ).mockRestore ()
778
+ vi .mocked (fetch ).mockRestore ()
783
779
})
784
780
785
781
const expectMocked = async (uri , response = mockedDefaultResponse ) => {
@@ -981,16 +977,14 @@ describe('conditional mocking', () => {
981
977
` ` `
982
978
983
979
` ` ` js
984
- import crossFetch from ' cross-fetch' ;
985
-
986
- vi .mock (' cross-fetch' , async () => {
987
- const crossFetchActual = await requireActual (' cross-fetch' );
980
+ vi .mock (' fetch' , async () => {
981
+ const fetchActual = globalThis .fetch ;
988
982
const mocked = {};
989
- for (const key of Object .keys (crossFetchActual )) {
990
- if (typeof crossFetchActual [key] === ' function' ) {
991
- mocked[key] = vi .fn (crossFetchActual [key]);
983
+ for (const key of Object .keys (fetchActual )) {
984
+ if (typeof fetchActual [key] === ' function' ) {
985
+ mocked[key] = vi .fn (fetchActual [key]);
992
986
} else {
993
- mocked[key] = crossFetchActual [key];
987
+ mocked[key] = fetchActual [key];
994
988
}
995
989
}
996
990
return mocked;
@@ -1007,15 +1001,15 @@ describe('conditional mocking complex', () => {
1007
1001
const realResponse = ' REAL FETCH RESPONSE' ;
1008
1002
const mockedDefaultResponse = ' MOCKED DEFAULT RESPONSE' ;
1009
1003
const testUrl = defaultRequestUri;
1010
- let crossFetchSpy ;
1004
+ let fetchSpy ;
1011
1005
beforeEach (() => {
1012
1006
fetch .resetMocks ();
1013
1007
fetch .mockResponse (mockedDefaultResponse);
1014
- vi .mocked (crossFetch ).mockImplementation (async () => Promise .resolve (new Response (realResponse)));
1008
+ vi .mocked (fetch ).mockImplementation (async () => Promise .resolve (new Response (realResponse)));
1015
1009
});
1016
1010
1017
1011
afterEach (() => {
1018
- vi .mocked (crossFetch ).mockRestore ();
1012
+ vi .mocked (fetch ).mockRestore ();
1019
1013
});
1020
1014
1021
1015
describe (' complex example' , () => {
0 commit comments