Skip to content

Commit efae35f

Browse files
KhafraDevronag
authored andcommitted
fix: add test
1 parent cf34f3c commit efae35f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"lint:fix": "standard --fix | snazzy",
4949
"test": "npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:jest && tsd",
5050
"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",
5252
"test:jest": "jest",
5353
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
5454
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",

test/fetch/issue-1447.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
})

0 commit comments

Comments
 (0)