forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.ts
60 lines (54 loc) · 2.41 KB
/
summary.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
import { hasFailedSnapshot } from '@vitest/ws-client'
import type { Benchmark, Task, Test } from 'vitest/src'
import { files, testRunState } from '~/composables/client'
type Nullable<T> = T | null | undefined
type Arrayable<T> = T | Array<T>
// files
export const filesFailed = computed(() => files.value.filter(f => f.result?.state === 'fail'))
export const filesSuccess = computed(() => files.value.filter(f => f.result?.state === 'pass'))
export const filesIgnore = computed(() => files.value.filter(f => f.mode === 'skip' || f.mode === 'todo'))
export const filesRunning = computed(() => files.value.filter(f =>
!filesFailed.value.includes(f)
&& !filesSuccess.value.includes(f)
&& !filesIgnore.value.includes(f),
))
export const filesSkipped = computed(() => filesIgnore.value.filter(f => f.mode === 'skip'))
export const filesSnapshotFailed = computed(() => files.value.filter(hasFailedSnapshot))
export const filesTodo = computed(() => filesIgnore.value.filter(f => f.mode === 'todo'))
export const finished = computed(() => testRunState.value === 'idle')
// tests
export const tests = computed(() => {
return getTests(files.value)
})
export const testsFailed = computed(() => {
return tests.value.filter(f => f.result?.state === 'fail')
})
export const testsSuccess = computed(() => {
return tests.value.filter(f => f.result?.state === 'pass')
})
export const testsIgnore = computed(() => tests.value.filter(f => f.mode === 'skip' || f.mode === 'todo'))
export const testsSkipped = computed(() => testsIgnore.value.filter(f => f.mode === 'skip'))
export const testsTodo = computed(() => testsIgnore.value.filter(f => f.mode === 'todo'))
export const totalTests = computed(() => testsFailed.value.length + testsSuccess.value.length)
export const time = computed(() => {
const t = getTests(tests.value).reduce((acc, t) => {
if (t.result?.duration)
acc += t.result.duration
return acc
}, 0)
if (t > 1000)
return `${(t / 1000).toFixed(2)}s`
return `${Math.round(t)}ms`
})
function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
array = array || []
if (Array.isArray(array))
return array
return [array]
}
function isAtomTest(s: Task): s is Test | Benchmark {
return (s.type === 'test' || s.type === 'benchmark')
}
function getTests(suite: Arrayable<Task>): (Test | Benchmark)[] {
return toArray(suite).flatMap(s => isAtomTest(s) ? [s] : s.tasks.flatMap(c => isAtomTest(c) ? [c] : getTests(c)))
}