/
junit.test.ts
53 lines (47 loc) · 1.58 KB
/
junit.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { Suite, Task, TaskResult } from 'vitest'
import { expect, test } from 'vitest'
import { resolve } from 'pathe'
import { runVitest } from '../../test-utils'
import { getDuration } from '../../../packages/vitest/src/node/reporters/junit'
const root = resolve(__dirname, '../fixtures')
test('calc the duration used by junit', () => {
const result: TaskResult = { state: 'pass', duration: 0 }
const suite: Suite = {
id: '1',
type: 'suite',
name: 'suite',
mode: 'run',
tasks: [],
meta: {},
}
const task: Task = {
id: '1',
type: 'test',
name: 'timeout',
mode: 'run',
result,
context: null as any,
suite,
meta: {},
}
expect(getDuration(task)).toBe('0')
result.duration = 0.12
expect(getDuration(task)).toBe('0.00012')
result.duration = 12
expect(getDuration(task)).toBe('0.012')
result.duration = 12.01
expect(getDuration(task)).toBe('0.01201')
result.duration = 12000
expect(getDuration(task)).toBe('12')
result.duration = 12001
expect(getDuration(task)).toBe('12.001')
})
test('emits <failure> if a test has a syntax error', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root }, ['with-syntax-error'])
let xml = stdout
// clear timestamp and hostname
xml = xml.replace(/timestamp="[^"]+"/, 'timestamp="TIMESTAMP"')
xml = xml.replace(/hostname="[^"]+"/, 'hostname="HOSTNAME"')
expect(xml).toContain('<testsuite name="with-syntax-error.test.js" timestamp="TIMESTAMP" hostname="HOSTNAME" tests="1" failures="1" errors="0" skipped="0" time="0">')
expect(xml).toContain('<failure')
})