File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 48
48
"lint:fix" : " standard --fix | snazzy" ,
49
49
"test" : " npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:jest && tsd" ,
50
50
"test:node-fetch" : " node scripts/verifyVersion.js 16 || mocha test/node-fetch" ,
51
- "test:fetch" : " node scripts/verifyVersion.js 16 || tap test/fetch/*.js" ,
51
+ "test:fetch" : " node scripts/verifyVersion.js 16 || npm run build:node && tap test/fetch/*.js" ,
52
52
"test:jest" : " jest" ,
53
53
"test:tap" : " tap test/*.js test/diagnostics-channel/*.js" ,
54
54
"test:tdd" : " tap test/*.js test/diagnostics-channel/*.js -w" ,
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const { test } = require ( 'tap' )
4
+
5
+ const undici = require ( '../..' )
6
+ const { fetch : theoreticalGlobalFetch } = require ( '../../undici-fetch' )
7
+
8
+ test ( 'Mocking works with both fetches' , async ( t ) => {
9
+ const mockAgent = new undici . MockAgent ( )
10
+ const body = JSON . stringify ( { foo : 'bar' } )
11
+
12
+ mockAgent . disableNetConnect ( )
13
+ undici . setGlobalDispatcher ( mockAgent )
14
+ const pool = mockAgent . get ( 'https://example.com' )
15
+
16
+ pool . intercept ( {
17
+ path : '/path' ,
18
+ method : 'POST' ,
19
+ body ( bodyString ) {
20
+ t . equal ( bodyString , body )
21
+ return true
22
+ }
23
+ } ) . reply ( 200 , { ok : 1 } ) . times ( 2 )
24
+
25
+ const url = new URL ( 'https://example.com/path' ) . href
26
+
27
+ // undici fetch from node_modules
28
+ await undici . fetch ( url , {
29
+ method : 'POST' ,
30
+ body
31
+ } )
32
+
33
+ // the global fetch bundled with esbuild
34
+ await theoreticalGlobalFetch ( url , {
35
+ method : 'POST' ,
36
+ body
37
+ } )
38
+
39
+ t . end ( )
40
+ } )
You can’t perform that action at this time.
0 commit comments