-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
junit.test.ts
122 lines (101 loc) · 3.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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: {},
projectName: '',
}
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'])
const xml = stabilizeReport(stdout)
expect(xml).toContain('<testsuite name="with-syntax-error.test.js" timestamp="..." hostname="..." tests="1" failures="1" errors="0" skipped="0" time="...">')
expect(xml).toContain('<failure')
})
test('emits <failure> when beforeAll/afterAll failed', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/suite-hook-failure' })
const xml = stabilizeReport(stdout)
expect(xml).toMatchSnapshot()
})
test('format error', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root }, ['error.test.ts'])
expect(stabilizeReport(stdout)).toMatchSnapshot()
})
test('write testsuite name relative to root config', async () => {
const { stdout } = await runVitest({ reporters: 'junit', root: './fixtures/better-testsuite-name' })
const xml = stabilizeReport(stdout)
expect(xml).toContain('<testsuite name="space-1/test/base.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">')
expect(xml).toContain('<testsuite name="space-2/test/base.test.ts" timestamp="..." hostname="..." tests="1" failures="0" errors="0" skipped="0" time="...">')
})
test('options.classname changes classname property', async () => {
const { stdout } = await runVitest({
reporters: [['junit', { classname: 'some-custom-classname' }]],
root: './fixtures/default',
include: ['a.test.ts'],
})
const xml = stabilizeReport(stdout)
// All classname attributes should have the custom value
expect(xml.match(/<testcase classname="a\.test\.ts"/g)).toBeNull()
expect(xml.match(/<testcase classname="/g)).toHaveLength(13)
expect(xml.match(/<testcase classname="some-custom-classname"/g)).toHaveLength(13)
})
test('options.suiteName changes name property', async () => {
const { stdout } = await runVitest({
reporters: [['junit', { suiteName: 'some-custom-suiteName' }]],
root: './fixtures/default',
include: ['a.test.ts'],
})
const xml = stabilizeReport(stdout)
expect(xml).not.toContain('<testsuites name="vitest tests"')
expect(xml).toContain('<testsuites name="some-custom-suiteName"')
})
function stabilizeReport(report: string) {
return report.replaceAll(/(timestamp|hostname|time)=".*?"/g, '$1="..."')
}
test.each([true, false])('includeConsoleOutput %s', async (t) => {
const { stdout } = await runVitest({
reporters: [['junit', { includeConsoleOutput: t }]],
root,
include: ['console-simple.test.ts'],
})
expect(stabilizeReport(stdout)).matchSnapshot()
})
test.each([true, false])('addFileAttribute %s', async (t) => {
const { stdout } = await runVitest({
reporters: [['junit', { addFileAttribute: t }]],
root,
include: ['ok.test.ts'],
})
expect(stabilizeReport(stdout)).matchSnapshot()
})