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(ui): escape html in error diff #5325

Merged
merged 3 commits into from
Mar 4, 2024
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
3 changes: 2 additions & 1 deletion packages/ui/client/components/views/ViewReportError.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import type { ErrorWithDiff } from 'vitest'
import { openInEditor, shouldOpenInEditor } from '~/composables/error'
import { escapeHtml } from '~/utils/escape';

const props = defineProps<{
root: string
Expand All @@ -20,7 +21,7 @@ const isDiffShowable = computed(() => {
return !!props.error?.diff
})

const diff = computed(() => props.error.diff ? filter.value.toHtml(props.error.diff) : undefined)
const diff = computed(() => props.error.diff ? filter.value.toHtml(escapeHtml(props.error.diff)) : undefined)
</script>

<template>
Expand Down
6 changes: 6 additions & 0 deletions test/ui/fixtures/error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { expect, it } from "vitest"

// https://github.com/vitest-dev/vitest/issues/5321
it('escape html in error diff', () => {
expect('<style>* {border: 2px solid green};</style>').toBe("")
})
8 changes: 7 additions & 1 deletion test/ui/test/html-report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test.describe('html report', () => {
await page.goto(pageUrl)

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

// unhandled errors
await expect(page.getByTestId('unhandled-errors')).toContainText(
Expand Down Expand Up @@ -64,4 +64,10 @@ test.describe('html report', () => {
await page.getByLabel('Show coverage').click()
await page.frameLocator('#vitest-ui-coverage').getByRole('heading', { name: 'All files' }).click()
})

test('error', async ({ page }) => {
await page.goto(pageUrl)
await page.getByText('fixtures/error.test.ts').click()
await expect(page.getByTestId('diff')).toContainText('- Expected + Received + <style>* {border: 2px solid green};</style>')
})
})
8 changes: 7 additions & 1 deletion test/ui/test/ui.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.describe('ui', () => {
await page.goto(pageUrl)

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

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

test('error', async ({ page }) => {
await page.goto(pageUrl)
await page.getByText('fixtures/error.test.ts').click()
await expect(page.getByTestId('diff')).toContainText('- Expected + Received + <style>* {border: 2px solid green};</style>')
})

test('file-filter', async ({ page }) => {
await page.goto(pageUrl)

Expand Down
3 changes: 3 additions & 0 deletions test/ui/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ export default defineConfig({
test: {
dir: './fixtures',
environment: 'happy-dom',
coverage: {
reportOnFailure: true,
},
},
})
Loading