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
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: improve mock error breadcrums
rossilor95 committed Feb 16, 2024
commit 8d74fed105e905b3080d0774ef1a0e43d255d959
7 changes: 4 additions & 3 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
@@ -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]
54 changes: 54 additions & 0 deletions test/mock-utils.js
Original file line number Diff line number Diff line change
@@ -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', () => {