Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve mock error breadcrumbs #2774

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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