Skip to content

Commit ab60bf8

Browse files
authoredMar 4, 2024··
fix(ui): escape html in error diff (#5325)
1 parent 14ee16d commit ab60bf8

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed
 

‎packages/ui/client/components/views/ViewReportError.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import type { ErrorWithDiff } from 'vitest'
33
import { openInEditor, shouldOpenInEditor } from '~/composables/error'
4+
import { escapeHtml } from '~/utils/escape';
45
56
const props = defineProps<{
67
root: string
@@ -20,7 +21,7 @@ const isDiffShowable = computed(() => {
2021
return !!props.error?.diff
2122
})
2223
23-
const diff = computed(() => props.error.diff ? filter.value.toHtml(props.error.diff) : undefined)
24+
const diff = computed(() => props.error.diff ? filter.value.toHtml(escapeHtml(props.error.diff)) : undefined)
2425
</script>
2526

2627
<template>

‎test/ui/fixtures/error.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, it } from "vitest"
2+
3+
// https://github.com/vitest-dev/vitest/issues/5321
4+
it('escape html in error diff', () => {
5+
expect('<style>* {border: 2px solid green};</style>').toBe("")
6+
})

‎test/ui/test/html-report.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test.describe('html report', () => {
3232
await page.goto(pageUrl)
3333

3434
// dashbaord
35-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 0 Fail 5 Total')
35+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total')
3636

3737
// unhandled errors
3838
await expect(page.getByTestId('unhandled-errors')).toContainText(
@@ -64,4 +64,10 @@ test.describe('html report', () => {
6464
await page.getByLabel('Show coverage').click()
6565
await page.frameLocator('#vitest-ui-coverage').getByRole('heading', { name: 'All files' }).click()
6666
})
67+
68+
test('error', async ({ page }) => {
69+
await page.goto(pageUrl)
70+
await page.getByText('fixtures/error.test.ts').click()
71+
await expect(page.getByTestId('diff')).toContainText('- Expected + Received + <style>* {border: 2px solid green};</style>')
72+
})
6773
})

‎test/ui/test/ui.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test.describe('ui', () => {
2323
await page.goto(pageUrl)
2424

2525
// dashbaord
26-
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 0 Fail 5 Total')
26+
await expect(page.locator('[aria-labelledby=tests]')).toContainText('5 Pass 1 Fail 6 Total')
2727

2828
// unhandled errors
2929
await expect(page.getByTestId('unhandled-errors')).toContainText(
@@ -63,6 +63,12 @@ test.describe('ui', () => {
6363
await page.getByText('/(?<char>\\w)/').click()
6464
})
6565

66+
test('error', async ({ page }) => {
67+
await page.goto(pageUrl)
68+
await page.getByText('fixtures/error.test.ts').click()
69+
await expect(page.getByTestId('diff')).toContainText('- Expected + Received + <style>* {border: 2px solid green};</style>')
70+
})
71+
6672
test('file-filter', async ({ page }) => {
6773
await page.goto(pageUrl)
6874

‎test/ui/vitest.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ export default defineConfig({
44
test: {
55
dir: './fixtures',
66
environment: 'happy-dom',
7+
coverage: {
8+
reportOnFailure: true,
9+
},
710
},
811
})

0 commit comments

Comments
 (0)
Please sign in to comment.