1- /**
2- * @vitest -environment node
3- */
1+ // @vitest -environment node
42import { http , HttpResponse , delay } from 'msw'
53import { setupServer } from 'msw/node'
64
@@ -24,7 +22,7 @@ afterAll(() => {
2422
2523it ( 'supports generator function as response resolver' , async ( ) => {
2624 server . use (
27- http . get ( 'https ://example.com /weather' , function * ( ) {
25+ http . get ( 'http ://localhost /weather' , function * ( ) {
2826 let degree = 10
2927
3028 while ( degree < 13 ) {
@@ -38,18 +36,18 @@ it('supports generator function as response resolver', async () => {
3836 )
3937
4038 // Must respond with yielded responses.
41- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 11 )
42- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 12 )
43- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 13 )
39+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 11 )
40+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 12 )
41+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 13 )
4442 // Must respond with the final "done" response.
45- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 14 )
43+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 14 )
4644 // Must keep responding with the final "done" response.
47- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 14 )
45+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 14 )
4846} )
4947
5048it ( 'supports async generator function as response resolver' , async ( ) => {
5149 server . use (
52- http . get ( 'https ://example.com /weather' , async function * ( ) {
50+ http . get ( 'http ://localhost /weather' , async function * ( ) {
5351 await delay ( 20 )
5452
5553 let degree = 10
@@ -64,17 +62,17 @@ it('supports async generator function as response resolver', async () => {
6462 } ) ,
6563 )
6664
67- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 11 )
68- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 12 )
69- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 13 )
70- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 14 )
71- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 14 )
65+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 11 )
66+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 12 )
67+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 13 )
68+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 14 )
69+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 14 )
7270} )
7371
7472it ( 'supports generator function as one-time response resolver' , async ( ) => {
7573 server . use (
7674 http . get (
77- 'https ://example.com /weather' ,
75+ 'http ://localhost /weather' ,
7876 function * ( ) {
7977 let degree = 10
8078
@@ -94,16 +92,31 @@ it('supports generator function as one-time response resolver', async () => {
9492 )
9593
9694 // Must respond with the yielded incrementing responses.
97- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 11 )
98- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 12 )
99- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 13 )
95+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 11 )
96+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 12 )
97+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 13 )
10098 // Must respond with the "done" final response from the iterator.
101- await expect ( fetchJson ( 'https ://example.com /weather' ) ) . resolves . toEqual ( 14 )
99+ await expect ( fetchJson ( 'http ://localhost /weather' ) ) . resolves . toBe ( 14 )
102100 // Must respond with the other handler since the generator one is used.
103- await expect ( fetchJson ( 'https://example.com/weather' ) ) . resolves . toEqual (
104- 'fallback' ,
105- )
106- await expect ( fetchJson ( 'https://example.com/weather' ) ) . resolves . toEqual (
107- 'fallback' ,
101+ await expect ( fetchJson ( 'http://localhost/weather' ) ) . resolves . toBe ( 'fallback' )
102+ await expect ( fetchJson ( 'http://localhost/weather' ) ) . resolves . toBe ( 'fallback' )
103+ } )
104+
105+ it ( 'resets the generator state after the handlers are reset' , async ( ) => {
106+ server . use (
107+ http . get ( 'http://localhost/resource' , function * ( ) {
108+ yield HttpResponse . json ( 'Yield' )
109+ return HttpResponse . json ( 'Stable' )
110+ } ) ,
108111 )
112+
113+ await server . boundary ( async ( ) => {
114+ await expect ( fetchJson ( 'http://localhost/resource' ) ) . resolves . toBe ( 'Yield' )
115+ await expect ( fetchJson ( 'http://localhost/resource' ) ) . resolves . toBe ( 'Stable' )
116+
117+ server . resetHandlers ( )
118+
119+ await expect ( fetchJson ( 'http://localhost/resource' ) ) . resolves . toBe ( 'Yield' )
120+ await expect ( fetchJson ( 'http://localhost/resource' ) ) . resolves . toBe ( 'Stable' )
121+ } ) ( )
109122} )
0 commit comments