-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
suite.ts
176 lines (145 loc) · 4.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
import { format } from 'util'
import type { File, MutableArray, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Task, Test, TestAPI, TestFunction } from '../types'
import { isObject, noop, toArray } from '../utils'
import { createChainable } from './chain'
import { collectTask, context, normalizeTest, runWithSuite } 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[]) {
const count = template.split('%').length - 1
let formatted = 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
// implementations
export const defaultSuite = suite('')
export function clearContext() {
context.tasks.length = 0
defaultSuite.clear()
context.currentSuite = defaultSuite
}
export function getCurrentSuite() {
return context.currentSuite || defaultSuite
}
export function createSuiteHooks() {
return {
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: [],
}
}
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean) {
const tasks: (Test | Suite | SuiteCollector)[] = []
const factoryQueue: (Test | Suite | SuiteCollector)[] = []
let suite: Suite
initSuite()
const test = createTest(function(name: string, fn?: TestFunction, 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,
}
if (this.concurrent || concurrent)
test.concurrent = true
setFn(test, normalizeTest(fn || noop, 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,
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', 'skip', 'only', 'todo'],
function(name: string, factory?: SuiteFactory) {
const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
return createSuiteCollector(name, factory, mode, this.concurrent)
},
) as SuiteAPI
suite.each = <T>(cases: T[] | readonly T[]) => {
return (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => {
cases.forEach((i) => {
const items = toArray(i) as any
suite(formatTitle(name, items), () => fn(...items))
})
}
}
return suite as SuiteAPI
}
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: T[] | readonly T[]) => {
return (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => {
cases.forEach((i) => {
const items = toArray(i) as any
test(formatTitle(name, items), () => fn(...items))
})
}
}
return test as TestAPI
}