@@ -2,7 +2,6 @@ import assert from 'node:assert';
22
33import { createTestServer } from './createTestServer/createTestServer' ;
44import { entriesToObject } from './utils/entriesToObject' ;
5- import { isWhatwgFetch } from './utils/isWhatwgFetch' ;
65import { defaults , del , get , patch , patchJSON , post , postJSON , put , putJSON } from './Http' ;
76
87const path = '/' ;
@@ -480,7 +479,7 @@ describe('body methods', () => {
480479 const response = await get ( url ) . blob ( ) ;
481480 expect ( response . size ) . toEqual ( 3 ) ;
482481 // FIXME https://github.com/jsdom/jsdom/issues/2555
483- if ( ! isWhatwgFetch ) {
482+ if ( process . env . FETCH !== 'whatwg-fetch' ) {
484483 // eslint-disable-next-line jest/no-conditional-expect
485484 expect ( await response . text ( ) ) . toEqual ( '*/*' ) ;
486485 }
@@ -598,11 +597,24 @@ describe('body methods', () => {
598597 } ) ;
599598
600599 // https://github.com/whatwg/fetch/issues/1147
601- // eslint-disable-next-line unicorn/consistent-function-scoping
602- const nodeFetchBodyAlreadyUsedError = ( url : string ) => `body used already for: ${ url } ` ;
603- const whatwgFetchBodyAlreadyUsedError = 'Already read' ;
604- const bodyAlreadyUsedError = ( url : string ) =>
605- isWhatwgFetch ? whatwgFetchBodyAlreadyUsedError : nodeFetchBodyAlreadyUsedError ( url ) ;
600+ let bodyAlreadyUsedError = '' ;
601+ switch ( process . env . FETCH ) {
602+ case 'node-fetch' : {
603+ bodyAlreadyUsedError = 'body used already for: ' ;
604+ break ;
605+ }
606+ case 'whatwg-fetch' : {
607+ bodyAlreadyUsedError = 'Already read' ;
608+ break ;
609+ }
610+ case 'undici' : {
611+ bodyAlreadyUsedError = 'Body is unusable' ;
612+ break ;
613+ }
614+ default : {
615+ assert ( false , `Unknown FETCH env '${ process . env . FETCH } '` ) ;
616+ }
617+ }
606618
607619 test ( 'multiple body calls using helpers' , async ( ) => {
608620 const server = createTestServer ( ) ;
@@ -614,7 +626,7 @@ describe('body methods', () => {
614626
615627 const response = get ( url ) ;
616628 expect ( await response . json ( ) ) . toEqual ( { accept : 'application/json' } ) ;
617- await expect ( response . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ( url ) ) ;
629+ await expect ( response . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ) ;
618630
619631 // FIXME await close() is too slow with Fastify 4.10.2
620632 server . close ( ) ;
@@ -630,7 +642,7 @@ describe('body methods', () => {
630642
631643 const response = await get ( url ) ;
632644 expect ( await response . json ( ) ) . toEqual ( { accept : '*/*' } ) ;
633- await expect ( response . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ( url ) ) ;
645+ await expect ( response . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ) ;
634646
635647 // FIXME await close() is too slow with Fastify 4.10.2
636648 server . close ( ) ;
@@ -647,7 +659,7 @@ describe('body methods', () => {
647659 const response = get ( url ) ;
648660 expect ( await response . json ( ) ) . toEqual ( { accept : 'application/json' } ) ;
649661 // eslint-disable-next-line unicorn/no-await-expression-member
650- await expect ( ( await response ) . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ( url ) ) ;
662+ await expect ( ( await response ) . text ( ) ) . rejects . toThrow ( bodyAlreadyUsedError ) ;
651663
652664 // FIXME await close() is too slow with Fastify 4.10.2
653665 server . close ( ) ;
@@ -657,11 +669,24 @@ describe('body methods', () => {
657669test ( 'cannot connect' , async ( ) => {
658670 const url = 'http://localhost/' ;
659671
660- const nodeFetchRequestFailedError = `request to ${ url } failed, reason: connect ECONNREFUSED 127.0.0.1:80` ;
661- const whatwgFetchRequestFailedError = 'Network request failed' ;
662- const requestFailedError = isWhatwgFetch
663- ? whatwgFetchRequestFailedError
664- : nodeFetchRequestFailedError ;
672+ let requestFailedError = '' ;
673+ switch ( process . env . FETCH ) {
674+ case 'node-fetch' : {
675+ requestFailedError = `request to ${ url } failed, reason: connect ECONNREFUSED 127.0.0.1:80` ;
676+ break ;
677+ }
678+ case 'whatwg-fetch' : {
679+ requestFailedError = 'Network request failed' ;
680+ break ;
681+ }
682+ case 'undici' : {
683+ requestFailedError = 'fetch failed' ;
684+ break ;
685+ }
686+ default : {
687+ assert ( false , `Unknown FETCH env '${ process . env . FETCH } '` ) ;
688+ }
689+ }
665690
666691 // Avoid console to be polluted with whatwg-fetch "Error: connect ECONNREFUSED"
667692 const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
@@ -671,12 +696,12 @@ test('cannot connect', async () => {
671696 } catch ( e ) {
672697 assert ( e instanceof Error ) ;
673698 /* eslint-disable jest/no-conditional-expect */
674- expect ( e . name ) . toEqual ( isWhatwgFetch ? 'TypeError ' : 'FetchError ' ) ;
699+ expect ( e . name ) . toEqual ( process . env . FETCH === 'node-fetch' ? 'FetchError ' : 'TypeError ' ) ;
675700 expect ( e . message ) . toEqual ( requestFailedError ) ;
676701 /* eslint-enable jest/no-conditional-expect */
677702 }
678703
679- expect ( consoleSpy ) . toHaveBeenCalledTimes ( isWhatwgFetch ? 1 : 0 ) ;
704+ expect ( consoleSpy ) . toHaveBeenCalledTimes ( process . env . FETCH === 'whatwg-fetch' ? 1 : 0 ) ;
680705
681706 consoleSpy . mockRestore ( ) ;
682707} ) ;
0 commit comments