From 1a7901bfa21fd48bd5bbecd3c2c540169af4ae1b Mon Sep 17 00:00:00 2001 From: Lorenzo Rossi <65499789+rossilor95@users.noreply.github.com> Date: Sat, 17 Feb 2024 08:03:39 +0100 Subject: [PATCH] feat: improve mock error breadcrumbs (#2774) --- lib/mock/mock-utils.js | 7 +++--- test/mock-agent.js | 8 +++---- test/mock-utils.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index 96282f0ec9d..036d39680ff 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -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] diff --git a/test/mock-agent.js b/test/mock-agent.js index 80b04e658e8..cd0cf55f587 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -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) => { @@ -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) => { @@ -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) => { @@ -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`, { diff --git a/test/mock-utils.js b/test/mock-utils.js index baf0933ba57..744011c70b8 100644 --- a/test/mock-utils.js +++ b/test/mock-utils.js @@ -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', () => {