This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
base.ts
160 lines (147 loc) · 4.13 KB
/
base.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
import {isFunction} from 'lodash'
import * as Types from './types'
const context: Types.Context = {
test: it,
plugins: {},
chain: [],
}
function assignWithProps(target: any, ...sources: any[]) {
sources.forEach(source => {
if (!source) return
const descriptors = Object.keys(source).reduce((descriptors: any, key) => {
descriptors[key] = Object.getOwnPropertyDescriptor(source, key)
return descriptors
}, {})
// by default, Object.assign copies enumerable Symbols too
Object.getOwnPropertySymbols(source).forEach(sym => {
const descriptor = Object.getOwnPropertyDescriptor(source, sym) as any
if (descriptor.enumerable) {
descriptors[sym] = descriptor
}
})
Object.defineProperties(target, descriptors)
})
return target
}
// eslint-disable-next-line @typescript-eslint/ban-types
const base = <I extends Types.Context>(context: I): Types.Base<I, {}> => {
const end = (arg1: any, cb: Types.MochaCallback<I>) => {
const originalContext = context
if (isFunction(arg1)) {
cb = arg1
arg1 = undefined
}
arg1 ||= context.expectation || 'test'
async function run(this: Types.ITestCallbackContext, done?: Types.MochaDone) {
context = assignWithProps({}, originalContext)
if (context.retries) this.retries(context.retries)
if (cb) {
context.chain = [...context.chain, {
run: async (input: any) => {
await cb.call(this, input, done!)
},
}]
}
for (let i = 0; i < context.chain.length; i++) {
const handleError = async (err: Error): Promise<boolean> => {
context.error = err
i++
const handler = context.chain[i]
if (!handler || !handler.catch) return false
try {
await handler.catch(context)
delete context.error
return true
} catch (error: any) {
return handleError(error)
}
}
const next = context.chain[i]
try {
// eslint-disable-next-line no-await-in-loop
if (next.run) await next.run(context)
} catch (error: any) {
// eslint-disable-next-line no-await-in-loop
if (!await handleError(error)) break
}
}
for (const p of context.chain.reverse()) {
// eslint-disable-next-line no-await-in-loop
if (p.finally) await p.finally(context)
}
if (context.error) throw context.error
}
return context.test(arg1, (cb && cb.length === 2) ? function (done) {
if (context.timeout) this.timeout(context.timeout)
run.call(this, done).catch(done)
} : function () {
if (context.timeout) this.timeout(context.timeout)
return run.call(this)
})
}
return {
...Object.entries(context.plugins)
.reduce((plugins, [k, v]) => {
plugins[k] = (...args: any[]) => {
const plugin = v(...args)
// clone context first
const c = {...context as any}
if (plugin.init) plugin.init(c)
return base({
...c,
chain: [...c.chain, plugin],
})
}
return plugins
}, {} as any),
register(k: any, v: any) {
return base({
...context as any,
plugins: {
...context.plugins,
[k]: v,
},
})
},
do(cb) {
return base({
...context as any,
chain: [...context.chain, {run: (input: any) => cb(input)}],
})
},
finally(cb) {
return base({
...context as any,
chain: [...context.chain, {finally: (input: any) => cb(input)}],
})
},
add(key, v) {
return base({
...context as any,
chain: [...context.chain, {
async run(ctx: any) {
ctx[key] = await (isFunction(v) ? v(ctx) : v)
},
}],
})
},
end,
it: end,
}
}
export default base(context)
.register('skip', () => ({
init(ctx) {
ctx.test = it.skip
},
}))
.register('only', () => ({
init(ctx) {
ctx.test = it.only
},
}))
.register<'retries', unknown, any[]>('retries', (count: number) => ({
init(ctx) {
ctx.retries = count
},
}))