Skip to content

Commit

Permalink
feat: improve mock error breadcrumbs (nodejs#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossilor95 authored and crysmags committed Feb 27, 2024
1 parent 94926c5 commit c4dc93e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,20 @@ function getMockDispatch (mockDispatches, key) {
// Match method
matchedMockDispatches = matchedMockDispatches.filter(({ method }) => matchValue(method, key.method))
if (matchedMockDispatches.length === 0) {
throw new MockNotMatchedError(`Mock dispatch not matched for method '${key.method}'`)
throw new MockNotMatchedError(`Mock dispatch not matched for method '${key.method}' on path '${resolvedPath}'`)
}

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

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

return matchedMockDispatches[0]
Expand Down
8 changes: 4 additions & 4 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for meth

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

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

test('MockAgent - enableNetConnect should throw if dispatch not matched for headers and the origin was not allowed by net connect', async (t) => {
Expand Down Expand Up @@ -2246,7 +2246,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for head
headers: {
'User-Agent': 'wrong'
}
}), 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)`))
}), 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)`))
})

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

{
const { statusCode } = await request(`${baseUrl}/foo`, {
Expand Down
54 changes: 54 additions & 0 deletions test/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,60 @@ describe('getMockDispatch', () => {
method: 'wrong'
}), new MockNotMatchedError('Mock dispatch not matched for path \'wrong\''))
})

test('it should throw if no dispatch matches method', (t) => {
t = tspl(t, { plan: 1 })
const dispatches = [
{
path: 'path',
method: 'method',
consumed: false
}
]

t.throws(() => getMockDispatch(dispatches, {
path: 'path',
method: 'wrong'
}), new MockNotMatchedError('Mock dispatch not matched for method \'wrong\' on path \'path\''))
})

test('it should throw if no dispatch matches body', (t) => {
t = tspl(t, { plan: 1 })
const dispatches = [
{
path: 'path',
method: 'method',
body: 'body',
consumed: false
}
]

t.throws(() => getMockDispatch(dispatches, {
path: 'path',
method: 'method',
body: 'wrong'
}), new MockNotMatchedError('Mock dispatch not matched for body \'wrong\' on path \'path\''))
})

test('it should throw if no dispatch matches headers', (t) => {
t = tspl(t, { plan: 1 })
const dispatches = [
{
path: 'path',
method: 'method',
body: 'body',
headers: { key: 'value' },
consumed: false
}
]

t.throws(() => getMockDispatch(dispatches, {
path: 'path',
method: 'method',
body: 'body',
headers: { key: 'wrong' }
}), new MockNotMatchedError('Mock dispatch not matched for headers \'{"key":"wrong"}\' on path \'path\''))
})
})

describe('getResponseData', () => {
Expand Down

0 comments on commit c4dc93e

Please sign in to comment.