Skip to content

Commit

Permalink
fix: format special characters correctly (#22537)
Browse files Browse the repository at this point in the history
* Update packages/reporter/cypress/e2e/unit/formatted_message.cy.ts

Co-authored-by: Rachel <rachel@cypress.io>

* Update packages/reporter/cypress/e2e/unit/formatted_message.cy.ts

Co-authored-by: Rachel <rachel@cypress.io>

* Remove extra spaces

Co-authored-by: gitstart <gitstart@users.noreply.github.com>
Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
Co-authored-by: Rachel <rachel@cypress.io>
Co-authored-by: Tyler Biethman <tbiethman@users.noreply.github.com>
Co-authored-by: Benjamin Mayanja <vibenjamin6@gmail.com>
  • Loading branch information
6 people committed Jul 14, 2022
1 parent 1c44eae commit 90fe502
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
30 changes: 30 additions & 0 deletions packages/reporter/cypress/e2e/unit/formatted_message.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { formattedMessage } from '../../../src/commands/command'

describe('formattedMessage', () => {
it('returns empty string when message is falsy', () => {
const result = formattedMessage('')

expect(result).to.equal('')
})

it('maintains special characters when using "to match"', () => {
const specialMessage = 'expected **__*abcdef*__** to match /__.*abcdef.*__/'
const result = formattedMessage(specialMessage)

expect(result).to.equal('expected <strong><strong><em>abcdef</em></strong></strong> to match <strong>/__.*abcdef.*__/</strong>')
})

it('maintains special characters when using "to contain"', () => {
const specialMessage = 'expected ***abcdef*** to equal ***abcdef***'
const result = formattedMessage(specialMessage)

expect(result).to.equal('expected <em><strong>abcdef</strong></em> to equal <strong>***abcdef***</strong>')
})

it('does NOT maintain special characters when "to equal" or "to match" are not in assertion', () => {
const specialMessage = 'expected ***abcdef*** to contain ***abcdef***'
const result = formattedMessage(specialMessage)

expect(result).to.equal('expected <em><strong>abcdef</strong></em> to contain <em><strong>abcdef</strong></em>')
})
})
16 changes: 15 additions & 1 deletion packages/reporter/src/commands/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ const md = new Markdown()

const displayName = (model: CommandModel) => model.displayName || model.name
const nameClassName = (name: string) => name.replace(/(\s+)/g, '-')
const formattedMessage = (message: string) => message ? md.renderInline(message) : ''

export const formattedMessage = (message: string) => {
if (!message) return ''

const searchText = ['to match', 'to equal']
const regex = new RegExp(searchText.join('|'))
const split = message.split(regex)
const matchingText = searchText.find((text) => message.includes(text))
const textToConvert = [split[0].trim(), ...(matchingText ? [matchingText] : [])].join(' ')
const converted = md.renderInline(textToConvert)
const assertion = (split[1] && [`<strong>${split[1].trim()}</strong>`]) || []

return [converted, ...assertion].join(' ')
}

const invisibleMessage = (model: CommandModel) => {
return model.numElements > 1 ?
'One or more matched elements are not visible.' :
Expand Down
5 changes: 1 addition & 4 deletions packages/reporter/src/errors/test-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FlashOnClick from '../lib/flash-on-click'
import { onEnterOrSpace } from '../lib/util'
import Attempt from '../attempts/attempt-model'
import Command from '../commands/command-model'
import { formattedMessage } from '../commands/command'

import WarningIcon from '-!react-svg-loader!@packages/frontend-shared/src/assets/icons/warning_x8.svg'
import TerminalIcon from '-!react-svg-loader!@packages/frontend-shared/src/assets/icons/technology-terminal_x16.svg'
Expand Down Expand Up @@ -54,10 +55,6 @@ const TestError = observer((props: TestErrorProps) => {
onPrint()
}

const formattedMessage = (message?: string) => {
return message ? md.renderInline(message) : ''
}

const { err } = props.model
const { codeFrame } = err

Expand Down

4 comments on commit 90fe502

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 90fe502 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.1/linux-x64/develop-90fe5027cd1b040a3c5ee9c881f92c437f90f2af/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 90fe502 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.1/linux-arm64/develop-90fe5027cd1b040a3c5ee9c881f92c437f90f2af/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 90fe502 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.1/darwin-arm64/develop-90fe5027cd1b040a3c5ee9c881f92c437f90f2af/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 90fe502 Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.1/darwin-x64/develop-90fe5027cd1b040a3c5ee9c881f92c437f90f2af/cypress.tgz

Please sign in to comment.