Skip to content

Commit d3128c1

Browse files
authored
feat: improve mock error breadcrumbs (#2774)
1 parent eab6a0d commit d3128c1

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

lib/mock/mock-utils.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,20 @@ function getMockDispatch (mockDispatches, key) {
138138
// Match method
139139
matchedMockDispatches = matchedMockDispatches.filter(({ method }) => matchValue(method, key.method))
140140
if (matchedMockDispatches.length === 0) {
141-
throw new MockNotMatchedError(`Mock dispatch not matched for method '${key.method}'`)
141+
throw new MockNotMatchedError(`Mock dispatch not matched for method '${key.method}' on path '${resolvedPath}'`)
142142
}
143143

144144
// Match body
145145
matchedMockDispatches = matchedMockDispatches.filter(({ body }) => typeof body !== 'undefined' ? matchValue(body, key.body) : true)
146146
if (matchedMockDispatches.length === 0) {
147-
throw new MockNotMatchedError(`Mock dispatch not matched for body '${key.body}'`)
147+
throw new MockNotMatchedError(`Mock dispatch not matched for body '${key.body}' on path '${resolvedPath}'`)
148148
}
149149

150150
// Match headers
151151
matchedMockDispatches = matchedMockDispatches.filter((mockDispatch) => matchHeaders(mockDispatch, key.headers))
152152
if (matchedMockDispatches.length === 0) {
153-
throw new MockNotMatchedError(`Mock dispatch not matched for headers '${typeof key.headers === 'object' ? JSON.stringify(key.headers) : key.headers}'`)
153+
const headers = typeof key.headers === 'object' ? JSON.stringify(key.headers) : key.headers
154+
throw new MockNotMatchedError(`Mock dispatch not matched for headers '${headers}' on path '${resolvedPath}'`)
154155
}
155156

156157
return matchedMockDispatches[0]

test/mock-agent.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for meth
21762176

21772177
await t.rejects(request(`${baseUrl}/foo`, {
21782178
method: 'WRONG'
2179-
}), new MockNotMatchedError(`Mock dispatch not matched for method 'WRONG': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
2179+
}), new MockNotMatchedError(`Mock dispatch not matched for method 'WRONG' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
21802180
})
21812181

21822182
test('MockAgent - enableNetConnect should throw if dispatch not matched for body and the origin was not allowed by net connect', async (t) => {
@@ -2209,7 +2209,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for body
22092209
await t.rejects(request(`${baseUrl}/foo`, {
22102210
method: 'GET',
22112211
body: 'wrong'
2212-
}), new MockNotMatchedError(`Mock dispatch not matched for body 'wrong': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
2212+
}), new MockNotMatchedError(`Mock dispatch not matched for body 'wrong' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
22132213
})
22142214

22152215
test('MockAgent - enableNetConnect should throw if dispatch not matched for headers and the origin was not allowed by net connect', async (t) => {
@@ -2246,7 +2246,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for head
22462246
headers: {
22472247
'User-Agent': 'wrong'
22482248
}
2249-
}), new MockNotMatchedError(`Mock dispatch not matched for headers '{"User-Agent":"wrong"}': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
2249+
}), new MockNotMatchedError(`Mock dispatch not matched for headers '{"User-Agent":"wrong"}' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect is not enabled for this origin)`))
22502250
})
22512251

22522252
test('MockAgent - disableNetConnect should throw if dispatch not found by net connect', async (t) => {
@@ -2317,7 +2317,7 @@ test('MockAgent - headers function interceptor', async (t) => {
23172317
headers: {
23182318
Authorization: 'Bearer foo'
23192319
}
2320-
}), new MockNotMatchedError(`Mock dispatch not matched for headers '{"Authorization":"Bearer foo"}': subsequent request to origin ${baseUrl} was not allowed (net.connect disabled)`))
2320+
}), new MockNotMatchedError(`Mock dispatch not matched for headers '{"Authorization":"Bearer foo"}' on path '/foo': subsequent request to origin ${baseUrl} was not allowed (net.connect disabled)`))
23212321

23222322
{
23232323
const { statusCode } = await request(`${baseUrl}/foo`, {

test/mock-utils.js

+54
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,60 @@ describe('getMockDispatch', () => {
8787
method: 'wrong'
8888
}), new MockNotMatchedError('Mock dispatch not matched for path \'wrong\''))
8989
})
90+
91+
test('it should throw if no dispatch matches method', (t) => {
92+
t = tspl(t, { plan: 1 })
93+
const dispatches = [
94+
{
95+
path: 'path',
96+
method: 'method',
97+
consumed: false
98+
}
99+
]
100+
101+
t.throws(() => getMockDispatch(dispatches, {
102+
path: 'path',
103+
method: 'wrong'
104+
}), new MockNotMatchedError('Mock dispatch not matched for method \'wrong\' on path \'path\''))
105+
})
106+
107+
test('it should throw if no dispatch matches body', (t) => {
108+
t = tspl(t, { plan: 1 })
109+
const dispatches = [
110+
{
111+
path: 'path',
112+
method: 'method',
113+
body: 'body',
114+
consumed: false
115+
}
116+
]
117+
118+
t.throws(() => getMockDispatch(dispatches, {
119+
path: 'path',
120+
method: 'method',
121+
body: 'wrong'
122+
}), new MockNotMatchedError('Mock dispatch not matched for body \'wrong\' on path \'path\''))
123+
})
124+
125+
test('it should throw if no dispatch matches headers', (t) => {
126+
t = tspl(t, { plan: 1 })
127+
const dispatches = [
128+
{
129+
path: 'path',
130+
method: 'method',
131+
body: 'body',
132+
headers: { key: 'value' },
133+
consumed: false
134+
}
135+
]
136+
137+
t.throws(() => getMockDispatch(dispatches, {
138+
path: 'path',
139+
method: 'method',
140+
body: 'body',
141+
headers: { key: 'wrong' }
142+
}), new MockNotMatchedError('Mock dispatch not matched for headers \'{"key":"wrong"}\' on path \'path\''))
143+
})
90144
})
91145

92146
describe('getResponseData', () => {

0 commit comments

Comments
 (0)