-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchWithDebug.spec.ts
More file actions
65 lines (55 loc) · 1.49 KB
/
fetchWithDebug.spec.ts
File metadata and controls
65 lines (55 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import nock from 'nock'
import assert from 'node:assert'
import { describe, it, mock } from 'node:test'
import { fetchWithDebug } from './fetchWithDebug.ts'
void describe('fetchWithDebug', (): void => {
void it('should debug a request', async () => {
const scope = nock('https://example.com').post('/foo').reply(
200,
{ foo: 'bar' },
{
'Content-Type': 'application/json',
},
)
const mockLog = mock.fn()
const mockError = mock.fn()
const mockBody = mock.fn()
const res = await fetchWithDebug(
mockLog,
mockError,
mockBody,
)(new URL('https://example.com/foo'), {
method: 'POST',
body: JSON.stringify({ bar: 'baz' }),
headers: {
'Content-Type': 'application/json',
},
})
assert.equal(res.status, 200)
assert.deepEqual(await res.json(), { foo: 'bar' })
const requestLog = mockLog.mock.calls.find(
(call) => call.arguments[0] === 'request',
)
const responseLog = mockLog.mock.calls.find(
(call) => call.arguments[0] === 'response',
)
assert.ok(requestLog)
assert.ok(responseLog)
assert.deepEqual(requestLog?.arguments[1], {
url: 'https://example.com/foo',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ bar: 'baz' }),
})
assert.partialDeepStrictEqual(responseLog?.arguments[1], {
url: 'https://example.com/foo',
status: 200,
ok: true,
headers: {},
})
assert.deepEqual(mockBody.mock.calls[0]?.arguments[0], { foo: 'bar' })
assert.ok(scope.isDone())
})
})