Skip to content

Commit 6824d5b

Browse files
rossilor95ronag
authored andcommitted
feat: improve mock error breadcrumbs (#2774)
1 parent bd8f735 commit 6824d5b

3 files changed

Lines changed: 62 additions & 7 deletions

File tree

lib/mock/mock-utils.js

Lines changed: 4 additions & 3 deletions
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for meth
21622162

21632163
await t.rejects(request(`${baseUrl}/foo`, {
21642164
method: 'WRONG'
2165-
}), 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)`))
2165+
}), 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)`))
21662166
})
21672167

21682168
test('MockAgent - enableNetConnect should throw if dispatch not matched for body and the origin was not allowed by net connect', async (t) => {
@@ -2195,7 +2195,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for body
21952195
await t.rejects(request(`${baseUrl}/foo`, {
21962196
method: 'GET',
21972197
body: 'wrong'
2198-
}), 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)`))
2198+
}), 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)`))
21992199
})
22002200

22012201
test('MockAgent - enableNetConnect should throw if dispatch not matched for headers and the origin was not allowed by net connect', async (t) => {
@@ -2232,7 +2232,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for head
22322232
headers: {
22332233
'User-Agent': 'wrong'
22342234
}
2235-
}), 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)`))
2235+
}), 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)`))
22362236
})
22372237

22382238
test('MockAgent - disableNetConnect should throw if dispatch not found by net connect', async (t) => {
@@ -2303,7 +2303,7 @@ test('MockAgent - headers function interceptor', async (t) => {
23032303
headers: {
23042304
Authorization: 'Bearer foo'
23052305
}
2306-
}), new MockNotMatchedError(`Mock dispatch not matched for headers '{"Authorization":"Bearer foo"}': subsequent request to origin ${baseUrl} was not allowed (net.connect disabled)`))
2306+
}), 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)`))
23072307

23082308
{
23092309
const { statusCode } = await request(`${baseUrl}/foo`, {

test/mock-utils.js

Lines changed: 54 additions & 0 deletions
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)