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

fix: correctly render ansi diff #3810

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 30 additions & 2 deletions packages/ui/client/components/views/ViewReport.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,22 @@ function makeTextStack() {
// 5 Stacks
const textStacks = Array.from(new Array(5)).map(makeTextStack)

const diff = `
\x1B[32m- Expected\x1B[39m
\x1B[31m+ Received\x1B[39m

\x1B[2m Object {\x1B[22m
\x1B[2m "a": 1,\x1B[22m
\x1B[32m- "b": 2,\x1B[39m
\x1B[31m+ "b": 3,\x1B[39m
\x1B[2m }\x1B[22m
`

const error = {
name: 'Do some test',
stacks: textStacks,
message: 'Error: Transform failed with 1 error:',
diff,
}

const fileWithTextStacks = {
Expand All @@ -34,6 +46,7 @@ const fileWithTextStacks = {
type: 'suite',
mode: 'run',
filepath: 'test/plain-stack-trace.ts',
meta: {},
result: {
state: 'fail',
error,
Expand Down Expand Up @@ -73,12 +86,14 @@ describe('ViewReport', () => {
type: 'suite',
mode: 'run',
filepath: 'test/plain-stack-trace.ts',
meta: {},
result: {
state: 'fail',
errors: [{
name: 'Do some test',
stack: '\x1B[33mtest/plain-stack-trace.ts\x1B[0m',
message: 'Error: Transform failed with 1 error:',
diff,
}],
},
tasks: [],
Expand Down Expand Up @@ -110,18 +125,20 @@ describe('ViewReport', () => {
type: 'suite',
mode: 'run',
filepath: 'test/plain-stack-trace.ts',
meta: {},
result: {
state: 'fail',
errors: [{
name: 'Do some test',
stack: '\x1B[33mtest/plain-stack-trace.ts\x1B[0m',
message: '\x1B[44mError: Transform failed with 1 error:\x1B[0m',
diff,
}],
},
tasks: [],
}
const container = cy.mount(<ViewReport file={file} />)
.get(taskErrorSelector)
const component = cy.mount(<ViewReport file={file} />)
const container = component.get(taskErrorSelector)
container.should('exist')
container.children('pre').then((c) => {
expect(c.text(), 'error has the correct plain text').equals('Do some test: Error: Transform failed with 1 error:test/plain-stack-trace.ts')
Expand All @@ -145,4 +162,15 @@ describe('ViewReport', () => {
})
})
})

it('test diff display', () => {
const component = cy.mount(<ViewReport file={fileWithTextStacks as File} />)

const diffElement = component.get('[data-testid="diff"]')
diffElement.should('exist')
diffElement
.should('contain.text', 'Expected')
.and('contain.text', 'Received')
.and('not.contain.text', '\x1B')
})
})
14 changes: 7 additions & 7 deletions packages/ui/client/components/views/ViewReportError.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ function relative(p: string) {
return p
}

const filter = computed(() => createAnsiToHtmlFilter(isDark.value))

const isDiffShowable = computed(() => {
return props.error?.expected && props.error?.actual
return !!props.error?.diff
})

function diff() {
return props.error.diff
}
const diff = computed(() => props.error.diff ? filter.value.toHtml(props.error.diff) : undefined)
</script>

<template>
Expand All @@ -37,9 +37,9 @@ function diff() {
@click.passive="openInEditor(stack.file, stack.line, stack.column)"
/>
</div>
<pre v-if="isDiffShowable">
{{ `\n${diff()}` }}
</pre>
<template v-if="isDiffShowable">
<pre data-testid="diff" v-html="diff" />
</template>
</div>
</template>

Expand Down
Loading