-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
collect.ts
177 lines (154 loc) · 4.91 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { File, ResolvedConfig, Suite, TaskBase } from '../types'
import { getWorkerState, isBrowser, relativePath } from '../utils'
import { clearCollectorContext, defaultSuite } from './suite'
import { getHooks, setHooks } from './map'
import { processError } from './error'
import { collectorContext } from './context'
import { runSetupFiles } from './setup'
const now = Date.now
function hash(str: string): string {
let hash = 0
if (str.length === 0)
return `${hash}`
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i)
hash = (hash << 5) - hash + char
hash = hash & hash // Convert to 32bit integer
}
return `${hash}`
}
export async function collectTests(paths: string[], config: ResolvedConfig): Promise<File[]> {
const files: File[] = []
const browserHashMap = getWorkerState().browserHashMap!
async function importFromBrowser(filepath: string) {
const match = filepath.match(/^(\w:\/)/)
const hash = browserHashMap.get(filepath)
if (match)
return await import(`/@fs/${filepath.slice(match[1].length)}?v=${hash}`)
else
return await import(`${filepath}?v=${hash}`)
}
for (const filepath of paths) {
const path = relativePath(config.root, filepath)
const file: File = {
id: hash(path),
name: path,
type: 'suite',
mode: 'run',
filepath,
tasks: [],
}
clearCollectorContext()
try {
const setupStart = now()
await runSetupFiles(config)
const collectStart = now()
file.setupDuration = collectStart - setupStart
if (config.browser && isBrowser)
await importFromBrowser(filepath)
else
await import(filepath)
const defaultTasks = await defaultSuite.collect(file)
setHooks(file, getHooks(defaultTasks))
for (const c of [...defaultTasks.tasks, ...collectorContext.tasks]) {
if (c.type === 'test') {
file.tasks.push(c)
}
else if (c.type === 'benchmark') {
file.tasks.push(c)
}
else if (c.type === 'suite') {
file.tasks.push(c)
}
else if (c.type === 'collector') {
const suite = await c.collect(file)
if (suite.name || suite.tasks.length)
file.tasks.push(suite)
}
}
file.collectDuration = now() - collectStart
}
catch (e) {
file.result = {
state: 'fail',
error: processError(e),
}
if (config.browser)
console.error(e)
}
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`.
*/
export 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 && !getTaskFullName(t).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.length && suite.tasks.every(i => i.mode !== 'run'))
suite.mode = 'skip'
}
}
function getTaskFullName(task: TaskBase): string {
return `${task.suite ? `${getTaskFullName(task.suite)} ` : ''}${task.name}`
}
export 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('[Vitest] Unexpected .only modifier. Remove it or pass --allowOnly argument to bypass this error')),
}
}
function calculateHash(parent: Suite) {
parent.tasks.forEach((t, idx) => {
t.id = `${parent.id}_${idx}`
if (t.type === 'suite')
calculateHash(t)
})
}