Skip to content

Commit 0f6f62e

Browse files
committed
chore: resolve discussion
1 parent ee7c292 commit 0f6f62e

13 files changed

Lines changed: 61 additions & 487 deletions

docs/docs/api/MockAgent.md

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -573,89 +573,14 @@ mockAgent.getCallHistory()?.firstCall()
573573
// }
574574
```
575575
576-
#### Example - access call history on intercepted client
577-
578-
You can use `registerCallHistory` to register a specific MockCallHistory instance while you are intercepting request. This is useful to have an history already filtered. Note that `getCallHistory()` will still register every request configuration if you previously enable call history.
579-
580-
> using registerCallHistory with a disabled MockAgent will still register an history on the intercepted request.
581-
582-
```js
583-
import { MockAgent, setGlobalDispatcher, request } from 'undici'
584-
585-
const mockAgent = new MockAgent({ enableCallHistory: true })
586-
setGlobalDispatcher(mockAgent)
587-
588-
const client = mockAgent.get('http://example.com')
589-
590-
client.intercept({ path: '/', method: 'GET' }).reply(200, 'hi !').registerCallHistory('my-specific-history-name')
591-
592-
await request('http://example.com') // intercepted
593-
await request('http://example.com', { method: 'POST', body: JSON.stringify({ data: 'hello' }), headers: { 'content-type': 'application/json' }})
594-
595-
mockAgent.getCallHistory('my-specific-history-name')?.calls()
596-
// Returns [
597-
// MockCallHistoryLog {
598-
// body: undefined,
599-
// headers: undefined,
600-
// method: 'GET',
601-
// origin: 'http://example.com',
602-
// fullUrl: 'http://example.com/',
603-
// path: '/',
604-
// searchParams: {},
605-
// protocol: 'http:',
606-
// host: 'example.com',
607-
// port: ''
608-
// }
609-
// ]
610-
611-
mockAgent.getCallHistory()?.calls()
612-
// Returns [
613-
// MockCallHistoryLog {
614-
// body: undefined,
615-
// headers: undefined,
616-
// method: 'GET',
617-
// origin: 'http://example.com',
618-
// fullUrl: 'http://example.com/',
619-
// path: '/',
620-
// searchParams: {},
621-
// protocol: 'http:',
622-
// host: 'example.com',
623-
// port: ''
624-
// },
625-
// MockCallHistoryLog {
626-
// body: "{ "data": "hello" }",
627-
// headers: { 'content-type': 'application/json' },
628-
// method: 'POST',
629-
// origin: 'http://example.com',
630-
// fullUrl: 'http://example.com/',
631-
// path: '/',
632-
// searchParams: {},
633-
// protocol: 'http:',
634-
// host: 'example.com',
635-
// port: ''
636-
// }
637-
// ]
638-
```
639-
640576
#### Example - clear call history
641577
642-
Clear all call history registered :
643-
644578
```js
645579
const mockAgent = new MockAgent()
646580

647581
mockAgent.clearAllCallHistory()
648582
```
649583
650-
Clear only one call history :
651-
652-
```js
653-
const mockAgent = new MockAgent()
654-
655-
mockAgent.getCallHistory()?.clear()
656-
mockAgent.getCallHistory('my-history')?.clear()
657-
```
658-
659584
#### Example - call history instance class method
660585
661586
```js

docs/docs/api/MockCallHistory.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,31 @@ const mockAgent = new MockAgent()
1111
mockAgent.enableMockHistory()
1212
mockAgent.getCallHistory()
1313

14-
// or
15-
const mockAgent = new MockAgent()
16-
const mockClient = mockAgent.get('http://localhost:3000')
17-
mockClient
18-
.intercept({ path: '/' })
19-
.reply(200, 'hello')
20-
.registerCallHistory('my-custom-history')
21-
mockAgent.getCallHistory('my-custom-history')
2214
```
2315

2416
a MockCallHistory instance implements a **Symbol.iterator** letting you iterate on registered logs :
2517

2618
```ts
27-
for (const log of mockAgent.getCallHistory('my-custom-history')) {
19+
for (const log of mockAgent.getCallHistory()) {
2820
//...
2921
}
3022

31-
const array: Array<MockCallHistoryLog> = [...mockAgent.getCallHistory('my-custom-history')]
32-
const set: Set<MockCallHistoryLog> = new Set(mockAgent.getCallHistory('my-custom-history'))
23+
const array: Array<MockCallHistoryLog> = [...mockAgent.getCallHistory()]
24+
const set: Set<MockCallHistoryLog> = new Set(mockAgent.getCallHistory())
3325
```
3426

3527
## class methods
3628

3729
### clear
3830

39-
Clear all MockCallHistoryLog registered
31+
Clear all MockCallHistoryLog registered. This is automatically done when calling `mockAgent.close()`
4032

4133
```js
42-
mockAgent.getCallHistory()?.clear() // clear only mockAgent history
43-
mockAgent.getCallHistory('my-custom-history')?.clear() // clear only 'my-custom-history' history
34+
mockAgent.clearCallHistory()
35+
// same as
36+
mockAgent.getCallHistory()?.clear()
4437
```
4538
46-
> to clear all registered MockCallHistory, use `mockAgent.clearAllCallHistory()`. This is automatically done when calling `mockAgent.close()`
47-
4839
### calls
4940
5041
Get all MockCallHistoryLog registered as an array

docs/docs/best-practices/mocking-request.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ assert.deepEqual(badRequest, { message: 'bank account not found' })
7575

7676
Explore other MockAgent functionality [here](/docs/docs/api/MockAgent.md)
7777

78-
## Access agent history
78+
## Access agent call history
7979

8080
Using a MockAgent also allows you to make assertions on the configuration used to make your request in your application.
8181

@@ -115,30 +115,10 @@ assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.method, 'POST')
115115
assert.strictEqual(mockAgent.getCallHistory()?.firstCall()?.path, '/endpoint')
116116
assert.deepStrictEqual(mockAgent.getCallHistory()?.firstCall()?.headers, { 'content-type': 'application/json' })
117117

118-
// register a specific call history for a given interceptor (useful to filter call within a particular interceptor)
119-
const mockPool = mockAgent.get('http://localhost:3000');
120-
121-
// we intercept a call and we register a specific MockCallHistory
122-
mockPool.intercept({
123-
path: '/second-endpoint',
124-
})
125-
.reply(200, 'hello')
126-
.registerCallHistory('second-endpoint-history')
127-
128-
assert.ok(mockAgent.getCallHistory()?.calls().length === 2) // MockAgent call history has registered the call too
129-
assert.ok(mockAgent.getCallHistory('second-endpoint-history')?.calls().length === 1)
130-
assert.strictEqual(mockAgent.getCallHistory('second-endpoint-history')?.firstCall()?.path, '/second-endpoint')
131-
assert.strictEqual(mockAgent.getCallHistory('second-endpoint-history')?.firstCall()?.method, 'GET')
132-
133-
// clearing all call history
134-
mockAgent.clearAllCallHistory()
118+
// clear all call history logs
119+
mockAgent.clearCallHistory()
135120

136121
assert.ok(mockAgent.getCallHistory()?.calls().length === 0)
137-
assert.ok(mockAgent.getCallHistory('second-endpoint-history')?.calls().length === 0)
138-
139-
// clearing a particular history
140-
mockAgent.getCallHistory()?.clear() // second-endpoint-history will not be cleared
141-
mockAgent.getCallHistory('second-endpoint-history').clear() // second-endpoint-history is now cleared
142122
```
143123
144124
Calling `mockAgent.close()` will automatically clear and delete every call history for you.

lib/mock/mock-agent.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ const {
1212
kGetNetConnect,
1313
kOptions,
1414
kFactory,
15-
kMockAgentMockCallHistoryName,
1615
kMockAgentRegisterCallHistory,
1716
kMockAgentIsCallHistoryEnabled,
1817
kMockAgentAddCallHistoryLog,
19-
kMockCallHistoryGetByName,
20-
kMockCallHistoryClearAll,
21-
kMockCallHistoryDeleteAll,
22-
kMockCallHistoryCreate,
18+
kMockAgentMockCallHistoryInstance,
2319
kMockCallHistoryAddLog
2420
} = require('./mock-symbols')
2521
const MockClient = require('./mock-client')
@@ -75,7 +71,7 @@ class MockAgent extends Dispatcher {
7571
}
7672

7773
async close () {
78-
MockCallHistory[kMockCallHistoryDeleteAll]()
74+
this.clearCallHistory()
7975
await this[kAgent].close()
8076
this[kClients].clear()
8177
}
@@ -118,16 +114,14 @@ class MockAgent extends Dispatcher {
118114
return this
119115
}
120116

121-
getCallHistory (name) {
122-
if (name == null) {
123-
return MockCallHistory[kMockCallHistoryGetByName](kMockAgentMockCallHistoryName)
124-
}
125-
126-
return MockCallHistory[kMockCallHistoryGetByName](name)
117+
getCallHistory () {
118+
return this[kMockAgentMockCallHistoryInstance]
127119
}
128120

129-
clearAllCallHistory () {
130-
MockCallHistory[kMockCallHistoryClearAll]()
121+
clearCallHistory () {
122+
if (this[kMockAgentMockCallHistoryInstance] !== undefined) {
123+
this[kMockAgentMockCallHistoryInstance].clear()
124+
}
131125
}
132126

133127
// This is required to bypass issues caused by using global symbols - see:
@@ -137,19 +131,18 @@ class MockAgent extends Dispatcher {
137131
}
138132

139133
[kMockAgentRegisterCallHistory] () {
140-
MockCallHistory[kMockCallHistoryCreate](kMockAgentMockCallHistoryName)
134+
if (this[kMockAgentMockCallHistoryInstance] === undefined) {
135+
this[kMockAgentMockCallHistoryInstance] = new MockCallHistory()
136+
}
141137
}
142138

143139
[kMockAgentAddCallHistoryLog] (opts) {
144140
if (this[kMockAgentIsCallHistoryEnabled]) {
145-
// guard if mockAgent.close() (which delete all history) was called before a dispatch by inadvertency
146-
const maybeMockAgentCallHistory = this.getCallHistory()
147-
if (maybeMockAgentCallHistory === undefined) {
148-
this[kMockAgentRegisterCallHistory]()
149-
}
141+
// additional setup when enableCallHistory class method is used after mockAgent instantiation
142+
this[kMockAgentRegisterCallHistory]()
150143

151144
// add call history log on every call (intercepted or not)
152-
this.getCallHistory()[kMockCallHistoryAddLog](opts)
145+
this[kMockAgentMockCallHistoryInstance][kMockCallHistoryAddLog](opts)
153146
}
154147
}
155148

lib/mock/mock-call-history.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
'use strict'
22

3-
const {
4-
kMockCallHistoryAddLog,
5-
kMockCallHistoryGetByName,
6-
kMockCallHistoryClearAll,
7-
kMockCallHistoryDeleteAll,
8-
kMockCallHistoryCreate,
9-
kMockCallHistoryAllMockCallHistoryInstances
10-
} = require('./mock-symbols')
3+
const { kMockCallHistoryAddLog } = require('./mock-symbols')
114
const { InvalidArgumentError } = require('../core/errors')
125

136
function handleFilterCallsWithOptions (criteria, options, handler, store) {
@@ -132,38 +125,8 @@ class MockCallHistoryLog {
132125
}
133126

134127
class MockCallHistory {
135-
static [kMockCallHistoryAllMockCallHistoryInstances] = new Map()
136-
137128
logs = []
138129

139-
constructor (name) {
140-
this.name = name
141-
142-
MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].set(this.name, this)
143-
}
144-
145-
static [kMockCallHistoryCreate] (name) {
146-
if (MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].has(name)) {
147-
return MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].get(name)
148-
}
149-
150-
return new MockCallHistory(name)
151-
}
152-
153-
static [kMockCallHistoryGetByName] (name) {
154-
return MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].get(name)
155-
}
156-
157-
static [kMockCallHistoryClearAll] () {
158-
for (const callHistory of MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].values()) {
159-
callHistory.clear()
160-
}
161-
}
162-
163-
static [kMockCallHistoryDeleteAll] () {
164-
MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].clear()
165-
}
166-
167130
calls () {
168131
return this.logs
169132
}

lib/mock/mock-interceptor.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ const {
88
kDefaultTrailers,
99
kContentLength,
1010
kMockDispatch,
11-
kIgnoreTrailingSlash,
12-
kMockCallHistoryCreate,
13-
kMockDispatchCallHistoryName
11+
kIgnoreTrailingSlash
1412
} = require('./mock-symbols')
1513
const { InvalidArgumentError } = require('../core/errors')
1614
const { serializePathWithQuery } = require('../core/util')
17-
const { MockCallHistory } = require('./mock-call-history')
1815

1916
/**
2017
* Defines the scope API for an interceptor reply
@@ -55,21 +52,6 @@ class MockScope {
5552
this[kMockDispatch].times = repeatTimes
5653
return this
5754
}
58-
59-
/**
60-
* Register call history to be able to make assertion on
61-
*/
62-
registerCallHistory (name) {
63-
if (typeof name !== 'string' || name.length === 0) {
64-
throw new InvalidArgumentError('name must be a populated string')
65-
}
66-
67-
MockCallHistory[kMockCallHistoryCreate](name)
68-
69-
this[kMockDispatch][kMockDispatchCallHistoryName] = name
70-
71-
return this
72-
}
7355
}
7456

7557
/**

lib/mock/mock-symbols.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module.exports = {
1313
kMockAgentSet: Symbol('mock agent set'),
1414
kMockAgentGet: Symbol('mock agent get'),
1515
kMockDispatch: Symbol('mock dispatch'),
16-
kMockDispatchCallHistoryName: Symbol('mock dispatch call history name'),
1716
kClose: Symbol('close'),
1817
kOriginalClose: Symbol('original agent close'),
1918
kOriginalDispatch: Symbol('original dispatch'),
@@ -23,14 +22,9 @@ module.exports = {
2322
kGetNetConnect: Symbol('get net connect'),
2423
kConnected: Symbol('connected'),
2524
kIgnoreTrailingSlash: Symbol('ignore trailing slash'),
26-
kMockAgentMockCallHistoryName: Symbol('mock agent mock call history name'),
25+
kMockAgentMockCallHistoryInstance: Symbol('mock agent mock call history name'),
2726
kMockAgentRegisterCallHistory: Symbol('mock agent register mock call history'),
2827
kMockAgentAddCallHistoryLog: Symbol('mock agent add call history log'),
2928
kMockAgentIsCallHistoryEnabled: Symbol('mock agent is call history enabled'),
30-
kMockCallHistoryAddLog: Symbol('mock call history add log'),
31-
kMockCallHistoryAllMockCallHistoryInstances: Symbol('mock call history all instances'),
32-
kMockCallHistoryCreate: Symbol('mock call history create'),
33-
kMockCallHistoryGetByName: Symbol('mock call history get by name'),
34-
kMockCallHistoryClearAll: Symbol('mock call history clear all'),
35-
kMockCallHistoryDeleteAll: Symbol('mock call history delete all')
29+
kMockCallHistoryAddLog: Symbol('mock call history add log')
3630
}

0 commit comments

Comments
 (0)