forked from vitest-dev/vitest
/
suite.ts
215 lines (178 loc) · 5.43 KB
/
suite.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import util from 'util'
import type { File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, Test, TestAPI, TestFunction } from '../types'
import { getWorkerState, isObject, noop } from '../utils'
import { createChainable } from './chain'
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
import { getHooks, setFn, setHooks } from './map'
// apis
export const suite = createSuite()
export const test = createTest(
function (name: string, fn?: TestFunction, timeout?: number) {
// @ts-expect-error untyped internal prop
getCurrentSuite().test.fn.call(this, name, fn, timeout)
},
)
function formatTitle(template: string, items: any[], idx: number) {
if (template.includes('%#')) {
// '%#' match index of the test case
template = template
.replace(/%%/g, '__vitest_escaped_%__')
.replace(/%#/g, `${idx}`)
.replace(/__vitest_escaped_%__/g, '%%')
}
const count = template.split('%').length - 1
let formatted = util.format(template, ...items.slice(0, count))
if (isObject(items[0])) {
formatted = formatted.replace(/\$([$\w_]+)/g, (_, key) => {
return items[0][key]
})
}
return formatted
}
// alias
export const describe = suite
export const it = test
const workerState = getWorkerState()
// implementations
export const defaultSuite = workerState.config.sequence.shuffle
? suite.shuffle('')
: suite('')
export function clearCollectorContext() {
collectorContext.tasks.length = 0
defaultSuite.clear()
collectorContext.currentSuite = defaultSuite
}
export function getCurrentSuite() {
return collectorContext.currentSuite || defaultSuite
}
export function createSuiteHooks() {
return {
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: [],
}
}
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean) {
const tasks: (Test | Suite | SuiteCollector)[] = []
const factoryQueue: (Test | Suite | SuiteCollector)[] = []
let suite: Suite
initSuite()
const test = createTest(function (name: string, fn = noop, timeout?: number) {
const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
const test: Test = {
id: '',
type: 'test',
name,
mode,
suite: undefined!,
fails: this.fails,
} as Omit<Test, 'context'> as Test
if (this.concurrent || concurrent)
test.concurrent = true
if (shuffle)
test.shuffle = true
const context = createTestContext(test)
// create test context
Object.defineProperty(test, 'context', {
value: context,
enumerable: false,
})
setFn(test, withTimeout(
() => fn(context),
timeout,
))
tasks.push(test)
})
const collector: SuiteCollector = {
type: 'collector',
name,
mode,
test,
tasks,
collect,
clear,
on: addHook,
}
function addHook<T extends keyof SuiteHooks>(name: T, ...fn: SuiteHooks[T]) {
getHooks(suite)[name].push(...fn as any)
}
function initSuite() {
suite = {
id: '',
type: 'suite',
name,
mode,
shuffle,
tasks: [],
}
setHooks(suite, createSuiteHooks())
}
function clear() {
tasks.length = 0
factoryQueue.length = 0
initSuite()
}
async function collect(file?: File) {
factoryQueue.length = 0
if (factory)
await runWithSuite(collector, () => factory(test))
const allChildren: Task[] = []
for (const i of [...factoryQueue, ...tasks])
allChildren.push(i.type === 'collector' ? await i.collect(file) : i)
suite.file = file
suite.tasks = allChildren
allChildren.forEach((task) => {
task.suite = suite
if (file)
task.file = file
})
return suite
}
collectTask(collector)
return collector
}
function createSuite() {
const suite = createChainable(
['concurrent', 'shuffle', 'skip', 'only', 'todo'],
function (name: string, factory?: SuiteFactory) {
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle)
},
) as SuiteAPI
suite.each = <T>(cases: ReadonlyArray<T>) => {
return (name: string, fn: (...args: T[]) => void) => {
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
suite(formatTitle(name, items, idx), () => fn(...items))
})
}
}
suite.skipIf = (condition: any) => (condition ? suite.skip : suite) as SuiteAPI
suite.runIf = (condition: any) => (condition ? suite : suite.skip) as SuiteAPI
return suite
}
function createTest(fn: (
(
this: Record<'concurrent' | 'skip' | 'only' | 'todo' | 'fails', boolean | undefined>,
title: string,
fn?: TestFunction,
timeout?: number
) => void
)) {
const test = createChainable(
['concurrent', 'skip', 'only', 'todo', 'fails'],
fn,
) as TestAPI
test.each = <T>(cases: ReadonlyArray<T>) => {
return (name: string, fn: (...args: T[]) => void, timeout?: number) => {
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
test(formatTitle(name, items, idx), () => fn(...items), timeout)
})
}
}
test.skipIf = (condition: any) => (condition ? test.skip : test) as TestAPI
test.runIf = (condition: any) => (condition ? test : test.skip) as TestAPI
return test as TestAPI
}