forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.ts
146 lines (128 loc) · 3.85 KB
/
collect.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { createHash } from 'crypto'
import { relative } from 'pathe'
import type { File, ResolvedConfig, Suite, TaskBase } from '../types'
import { clearContext, defaultSuite } from './suite'
import { getHooks, setHooks } from './map'
import { processError } from './error'
import { context } from './context'
import { runSetupFiles } from './setup'
function hash(str: string, length = 10) {
return createHash('md5')
.update(str)
.digest('hex')
.slice(0, length)
}
export async function collectTests(paths: string[], config: ResolvedConfig) {
const files: File[] = []
for (const filepath of paths) {
const path = relative(config.root, filepath)
const file: File = {
id: hash(path),
name: path,
type: 'suite',
mode: 'run',
filepath,
tasks: [],
}
clearContext()
try {
await runSetupFiles(config)
await import(filepath)
const defaultTasks = await defaultSuite.collect(file)
setHooks(file, getHooks(defaultTasks))
for (const c of [...defaultTasks.tasks, ...context.tasks]) {
if (c.type === 'test') {
file.tasks.push(c)
}
else if (c.type === 'suite') {
file.tasks.push(c)
}
else {
const start = performance.now()
const suite = await c.collect(file)
file.collectDuration = performance.now() - start
if (suite.name || suite.tasks.length)
file.tasks.push(suite)
}
}
}
catch (e) {
file.result = {
state: 'fail',
error: processError(e),
}
// not sure thy, this this line is needed to trigger the error
process.stdout.write('\0')
}
calculateHash(file)
const hasOnlyTasks = someTasksAreOnly(file)
interpretTaskModes(file, config.testNamePattern, hasOnlyTasks, false, config.allowOnly)
files.push(file)
}
return files
}
/**
* If any tasks been marked as `only`, mark all other tasks as `skip`.
*/
function interpretTaskModes(suite: Suite, namePattern?: string | RegExp, onlyMode?: boolean, parentIsOnly?: boolean, allowOnly?: boolean) {
const suiteIsOnly = parentIsOnly || suite.mode === 'only'
suite.tasks.forEach((t) => {
// Check if either the parent suite or the task itself are marked as included
const includeTask = suiteIsOnly || t.mode === 'only'
if (onlyMode) {
if (t.type === 'suite' && (includeTask || someTasksAreOnly(t))) {
// Don't skip this suite
if (t.mode === 'only') {
checkAllowOnly(t, allowOnly)
t.mode = 'run'
}
}
else if (t.mode === 'run' && !includeTask) { t.mode = 'skip' }
else if (t.mode === 'only') {
checkAllowOnly(t, allowOnly)
t.mode = 'run'
}
}
if (t.type === 'test') {
if (namePattern && !t.name.match(namePattern))
t.mode = 'skip'
}
else if (t.type === 'suite') {
if (t.mode === 'skip')
skipAllTasks(t)
else
interpretTaskModes(t, namePattern, onlyMode, includeTask, allowOnly)
}
})
// if all subtasks are skipped, mark as skip
if (suite.mode === 'run') {
if (suite.tasks.every(i => i.mode !== 'run'))
suite.mode = 'skip'
}
}
function someTasksAreOnly(suite: Suite): boolean {
return suite.tasks.some(t => t.mode === 'only' || (t.type === 'suite' && someTasksAreOnly(t)))
}
function skipAllTasks(suite: Suite) {
suite.tasks.forEach((t) => {
if (t.mode === 'run') {
t.mode = 'skip'
if (t.type === 'suite')
skipAllTasks(t)
}
})
}
function checkAllowOnly(task: TaskBase, allowOnly?: boolean) {
if (allowOnly) return
task.result = {
state: 'fail',
error: processError(new Error('only is not allowed')),
}
}
function calculateHash(parent: Suite) {
parent.tasks.forEach((t, idx) => {
t.id = `${parent.id}_${idx}`
if (t.type === 'suite')
calculateHash(t)
})
}