Skip to content

Commit

Permalink
fix tests, clean up error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Aug 27, 2020
1 parent 75fe8f9 commit cc357f9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ describe('network stubbing', function () {
it('fails test if network error occurs retrieving response and response is intercepted', function (done) {
cy.on('fail', (err) => {
expect(err.message)
.to.contain('req.reply() was provided a callback to intercept the upstream response, but a network error occurred while making the request:')
.to.contain('\`req.reply()\` was provided a callback to intercept the upstream response, but a network error occurred while making the request:')
.and.contain('Error: connect ECONNREFUSED 127.0.0.1:3333')

done()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get } from 'lodash'
import { NetEventFrames } from '@packages/net-stubbing/lib/types'
import { errByPath, makeErrFromObj } from '../../../cypress/error_utils'
import { HandlerFn } from './'
Expand All @@ -16,7 +17,7 @@ export const onRequestComplete: HandlerFn<NetEventFrames.HttpRequestComplete> =
const err = errByPath(`net_stubbing.request_error.${errorName}`, {
innerErr: makeErrFromObj(frame.error),
req: request.request,
route: getRoute(frame.routeHandlerId),
route: get(getRoute(frame.routeHandlerId), 'options'),
})

request.state = 'Errored'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
...req,
reply (responseHandler, maybeBody?, maybeHeaders?) {
if (resolved) {
return $errUtils.throwErrByPath('net_stubbing.request_handling.reply_called_after_resolved', { args: { route: route.options, req } })
return $errUtils.throwErrByPath('net_stubbing.request_handling.reply_called_after_resolved')
}

if (replyCalled) {
return $errUtils.throwErrByPath('net_stubbing.request_handling.multiple_reply_calls', { args: { route: route.options, req } })
return $errUtils.throwErrByPath('net_stubbing.request_handling.multiple_reply_calls')
}

replyCalled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ export const onResponseReceived: HandlerFn<NetEventFrames.HttpResponseReceived>
},
})
})
.finally(() => {
resolved = true
})
.then(() => {
if (!sendCalled) {
// user did not call send, send response
userRes.send()
}
})
.finally(() => {
resolved = true
})
}
54 changes: 20 additions & 34 deletions packages/driver/src/cypress/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,10 @@ module.exports = {

net_stubbing: {
invalid_static_response: ({ cmd, message, staticResponse }) => {
return stripIndent`\
return cyStripIndent(`\
An invalid StaticResponse was supplied to \`${cmd}()\`. ${message}
You passed: ${format(staticResponse)}`
You passed: ${format(staticResponse)}`, 8)
},
route2: {
needs_experimental: stripIndent`\
Expand All @@ -972,67 +972,53 @@ module.exports = {
},
request_handling: {
cb_failed: ({ err, req, route }) => {
return stripIndent`\
return cyStripIndent(`\
A request callback passed to ${cmd('route2')} threw an error while intercepting a request:
${err.message}
Route: ${format(route)}
Intercepted request: ${format(req)}`
Intercepted request: ${format(req)}`, 10)
},
cb_timeout: ({ timeout, req, route }) => {
return stripIndent`\
return cyStripIndent(`\
A request callback passed to ${cmd('route2')} timed out after returning a Promise that took more than the \`defaultCommandTimeout\` of \`${timeout}ms\` to resolve.
If the request callback is expected to take longer than \`${timeout}ms\`, increase the configured \`defaultCommandTimeout\` value.
Route: ${format(route)}
Intercepted request: ${format(req)}`
},
multiple_reply_calls: ({ route, req }) => {
return stripIndent`\
\`req.reply()\` was called multiple times in a request handler, but a request can only be replied to once.
Route: ${format(route)}
Intercepted request: ${format(req)}`
},
reply_called_after_resolved: ({ route, req }) => {
return stripIndent`\
\`req.reply()\` was called after the request handler finished executing, but \`req.reply()\` can not be called after the request has been passed on.
Route: ${format(route)}
Intercepted request: ${format(req)}`
Intercepted request: ${format(req)}`, 10)
},
multiple_reply_calls: `\`req.reply()\` was called multiple times in a request handler, but a request can only be replied to once.`,
reply_called_after_resolved: `\`req.reply()\` was called after the request handler finished executing, but \`req.reply()\` can not be called after the request has been passed on.`,
},
request_error: {
network_error: ({ innerErr, req, route }) => {
return stripIndent`\
return cyStripIndent(`\
\`req.reply()\` was provided a callback to intercept the upstream response, but a network error occurred while making the request:
${normalizedStack(innerErr)}
Route: ${format(route)}
Intercepted request: ${format(req)}`
Intercepted request: ${format(req)}`, 10)
},
timeout: ({ innerErr, req, route }) => {
return stripIndent`\
return cyStripIndent(`\
\`req.reply()\` was provided a callback to intercept the upstream response, but the request timed out after the \`responseTimeout\` of \`${req.responseTimeout}ms\`.
${normalizedStack(innerErr)}
Route: ${format(route)}
Intercepted request: ${format(req)}`
Intercepted request: ${format(req)}`, 10)
},
},
response_handling: {
cb_failed: ({ err, req, res, route }) => {
return stripIndent`\
return cyStripIndent(`\
A response callback passed to \`req.reply()\` threw an error while intercepting a response:
${err.message}
Expand All @@ -1041,10 +1027,10 @@ module.exports = {
Intercepted request: ${format(req)}
Intercepted response: ${format(res)}`
Intercepted response: ${format(res)}`, 10)
},
cb_timeout: ({ timeout, req, res, route }) => {
return stripIndent`\
return cyStripIndent(`\
A response callback passed to \`req.reply()\` timed out after returning a Promise that took more than the \`defaultCommandTimeout\` of \`${timeout}ms\` to resolve.
If the response callback is expected to take longer than \`${timeout}ms\`, increase the configured \`defaultCommandTimeout\` value.
Expand All @@ -1053,19 +1039,19 @@ module.exports = {
Intercepted request: ${format(req)}
Intercepted response: ${format(res)}`
Intercepted response: ${format(res)}`, 10)
},
multiple_send_calls: ({ res }) => {
return stripIndent`\
return cyStripIndent(`\
\`res.send()\` was called multiple times in a response handler, but the response can only be sent once.
Response: ${format(res)}`
Response: ${format(res)}`, 10)
},
send_called_after_resolved: ({ res }) => {
return stripIndent`\
return cyStripIndent(`\
\`res.send()\` was called after the response handler finished executing, but \`res.send()\` can not be called after the response has been passed on.
Intercepted response: ${format(res)}`
Intercepted response: ${format(res)}`, 10)
},
},
},
Expand Down

0 comments on commit cc357f9

Please sign in to comment.