@@ -273,8 +273,8 @@ describe('testing api', () => {
273
273
expect (res .data ).toEqual (' 12345' );
274
274
275
275
// assert on the times called and arguments given to fetch
276
- expect (fetch .mock . calls .length ).toEqual (1 );
277
- expect (fetch .mock . calls [0 ][ 0 ] ).toEqual (' https://google.com' );
276
+ expect (fetch .requests () .length ).toEqual (1 );
277
+ expect (fetch .requests () [0 ]. url ).toEqual (' https://google.com/ ' );
278
278
});
279
279
});
280
280
```
@@ -613,12 +613,13 @@ describe('getYear action creator', () => {
613
613
614
614
### Using ` fetch.mock ` to inspect the mock state of each fetch call
615
615
616
- ` fetch.mock ` by default uses [ Vitest's mocking functions] ( https://vitest.dev/api/#mockinstance-properties ) . Therefore
616
+ ` fetch.mock ` by default uses [ Vitest's mocking functions] ( https://vitest.dev/api/mock.html ) . Therefore
617
617
you can make assertions on the mock state. In this example we have an arbitrary function that makes a different fetch
618
618
request based on the argument you pass to it. In our test, we run Vitest's ` beforeEach() ` and make sure to reset our
619
- mock before each ` it() ` block as we will make assertions on the arguments we are passing to ` fetch() ` . The most uses
620
- property is the ` fetch.mock.calls ` array. It can give you information on each call, and their arguments which you can
621
- use for your ` expect() ` calls. Vitest also comes with some nice aliases for the most used ones.
619
+ mock before each ` it() ` block as we will make assertions on the arguments we are passing to ` fetch() ` . Then we use the
620
+ ` fetch.requests() ` function to give us a history of all non-aborted requests (normalized) that were made. It can give you
621
+ information on each call, and their arguments which you can use for your ` expect() ` calls. Vitest also comes with some
622
+ nice aliases for the most used ones.
622
623
623
624
``` js
624
625
// api.js
@@ -649,25 +650,26 @@ describe('testing api', () => {
649
650
fetch .mockResponse (JSON .stringify ({ secret_data: ' 12345' }));
650
651
APIRequest ();
651
652
652
- expect (fetch .mock .calls .length ).toEqual (1 );
653
- expect (fetch .mock .calls [0 ][0 ]).toEqual (' https://google.com' );
653
+ // there was one request, which was not aborted
654
+ expect (fetch .requests ().length ).toEqual (1 );
655
+ expect (fetch .requests ()[0 ].url ).toEqual (' https://google.com/' );
654
656
});
655
657
656
658
it (' calls facebook' , () => {
657
659
fetch .mockResponse (JSON .stringify ({ secret_data: ' 12345' }));
658
660
APIRequest (' facebook' );
659
661
660
- expect (fetch .mock . calls .length ).toEqual (2 );
661
- expect (fetch .mock . calls [0 ][ 0 ] ).toEqual (' https://facebook.com/someOtherResource' );
662
- expect (fetch .mock . calls [1 ][ 0 ] ).toEqual (' https://facebook.com' );
662
+ expect (fetch .requests () .length ).toEqual (2 );
663
+ expect (fetch .requests () [0 ]. url ).toEqual (' https://facebook.com/someOtherResource' );
664
+ expect (fetch .requests () [1 ]. url ).toEqual (' https://facebook.com/ ' );
663
665
});
664
666
665
667
it (' calls twitter' , () => {
666
668
fetch .mockResponse (JSON .stringify ({ secret_data: ' 12345' }));
667
669
APIRequest (' twitter' );
668
670
669
671
expect (fetch).toBeCalled (); // alias for expect(fetch.mock.calls.length).toEqual(1);
670
- expect (fetch). toBeCalledWith ( ' https://twitter.com' ); // alias for expect(fetch.mock.calls[0][0]).toEqual( );
672
+ expect (fetch . requests (). map ( v => v . url )). toContain ( ' https://twitter.com/ ' );
671
673
});
672
674
});
673
675
```
0 commit comments