Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
geritol committed Dec 9, 2022
1 parent fd73480 commit 666e6ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/driver/cypress/component/spec.cy.js
@@ -1,4 +1,4 @@
import sinon from 'sinon'
const { sinon } = Cypress

describe('component testing', () => {
/** @type {Cypress.Agent<sinon.SinonSpy>} */
Expand Down
18 changes: 14 additions & 4 deletions packages/driver/cypress/e2e/cypress/error_utils.cy.ts
Expand Up @@ -671,14 +671,24 @@ describe('driver/src/cypress/error_utils', () => {
}
})

it('calls Cypress.log with error type and message when error is instance of Error', () => {
it('calls Cypress.log with error name and message when error is instance of Error', () => {
$errUtils.logError(cypressMock, 'error', new Error('Some error'))
expect(cypressMock.log).to.have.been.calledWithMatch(sinon.match.has('message', `Error: Some error`))
})

it('calls Cypress.log with error type and message when error a string', () => {
$errUtils.logError(cypressMock, 'error', 'Some error')
expect(cypressMock.log).to.have.been.calledWithMatch(sinon.match.has('message', `Error: \"Some error\"`))
it('calls Cypress.log with error name and message when error a string', () => {
$errUtils.logError(cypressMock, 'error', 'Some string error')
expect(cypressMock.log).to.have.been.calledWithMatch(sinon.match.has('message', `Error: \"Some string error\"`))
})

it('calls Cypress.log with default error name and provided message message when error is an object with a message', () => {
$errUtils.logError(cypressMock, 'error', { message: 'Some object error with message' })
expect(cypressMock.log).to.have.been.calledWithMatch(sinon.match.has('message', `Error: Some object error with message`))
})

it('calls Cypress.log with error name and message when error is an object', () => {
$errUtils.logError(cypressMock, 'error', { err: 'Error details' })
expect(cypressMock.log).to.have.been.calledWithMatch(sinon.match.has('message', `Error: {"err":"Error details"}`))
})
})
})
10 changes: 6 additions & 4 deletions packages/driver/src/cypress/error_utils.ts
Expand Up @@ -553,7 +553,7 @@ const logError = (Cypress, handlerType: HandlerType, err: unknown, handled = fal
const error = toLoggableError(err)

Cypress.log({
message: `${error.name}: ${error.message}`,
message: `${error.name || 'Error'}: ${error.message}`,
name: 'uncaught exception',
type: 'parent',
// specifying the error causes the log to be red/failed
Expand All @@ -574,7 +574,9 @@ const logError = (Cypress, handlerType: HandlerType, err: unknown, handled = fal
})
}

const isLogabbleError = (error: unknown): error is Error => {
interface LoggableError { name?: string, message: string }

const isLoggabbleError = (error: unknown): error is LoggableError => {
return (
typeof error === 'object' &&
error !== null &&
Expand All @@ -583,8 +585,8 @@ const isLogabbleError = (error: unknown): error is Error => {
)
}

const toLoggableError = (maybeError: unknown): Error => {
if (isLogabbleError(maybeError)) return maybeError
const toLoggableError = (maybeError: unknown): LoggableError => {
if (isLoggabbleError(maybeError)) return maybeError

try {
return new Error(JSON.stringify(maybeError))
Expand Down

0 comments on commit 666e6ca

Please sign in to comment.