This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plug.js
212 lines (161 loc) · 4.93 KB
/
plug.js
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
import { createHarness as createZoraHarness } from 'zora'
const REPORT_HOOKS = Symbol('@zorax/plug:REPORT_HOOKS')
const noop = () => {}
const getHarnessMutators = pg => [pg.test, pg.harness, pg.init]
const getHarnessDecorators = pg => [
pg.decorate,
pg.decorateHarness,
pg.decorateInit,
]
const getProxyMutators = pg => [pg.test, pg.harness]
const getProxyDecorators = pg => [pg.decorate, pg.decorateHarness]
const getTestMutators = pg => pg.test
const getTestDecorators = pg => pg.decorate
const getHarnessHooks = plugins =>
[
...plugins.flatMap(getHarnessMutators),
...plugins.flatMap(getHarnessDecorators),
].filter(Boolean)
const getTestHooks = plugins =>
[...plugins.map(getTestMutators), ...plugins.map(getTestDecorators)].filter(
Boolean
)
const getProxyHooks = plugins =>
[
...plugins.flatMap(getProxyMutators),
...plugins.flatMap(getProxyDecorators),
].filter(Boolean)
const wrapTestMethod = (harness, proxy, withHooks, target) => {
const { test } = target
target.test = (desc, run, ...rest) => {
// just a friendly sanity check: end of pipeline respects expected signature
if (
// undefined title is actually more or less allowed
(typeof desc !== 'undefined' && typeof desc !== 'string') ||
typeof run !== 'function'
) {
throw new Error('plugins pipeline in disarray')
}
const zora_spec_fn = (t, ...rest) => run(withHooks(t), ...rest)
return test(desc, zora_spec_fn, ...rest)
}
}
// NOTE prop: {} makes it undefined
const proxySpecifiers = {
report: {},
pass: { writable: true }, // zora.pass wants to rewrite this
error: {},
length: {},
count: {},
successCount: {},
skipCount: {},
failureCount: {},
}
const augmentProxy = (harness, target, plugins, getHooks) => {
target.plugins = plugins
target.plug = (...newPlugins) => plug(harness, plugins, target, newPlugins)
target.run = fn => {
fn(target)
return target
}
const testHooks = getTestHooks(plugins)
const withTestHooks = t => {
wrapTestMethod(harness, target, withTestHooks, t)
testHooks.forEach(hook => hook(t, target, harness))
return t
}
wrapTestMethod(harness, target, withTestHooks, target)
// NOTE apply hooks _after_ replacing test!
const hooks = getHooks(plugins)
hooks.forEach(hook => hook(target, harness))
}
const createProxy = t => Object.create(t, proxySpecifiers)
const plug = (harness, parentPlugins, target, newPluginsInput) => {
const newPlugins = newPluginsInput.flat().filter(Boolean)
newPlugins.forEach(pg => {
if (pg.init || pg.decorateInit) {
throw new Error(
'Init plugin can only be added at harness creation (not plugged): ' +
pg.name
)
}
if (pg.report) {
harness[REPORT_HOOKS].push(pg.report)
}
})
const proxy = createProxy(target)
const plugins = [...parentPlugins, ...newPlugins]
augmentProxy(harness, proxy, plugins, getProxyHooks)
return proxy
}
const parseCreateHarnessFactoryArgs = args => {
if (args.length === 0) {
return args
}
let createHarness
let options
let plugins
const [a, b, c] = args
if (typeof a === 'function') {
createHarness = a
if (Array.isArray(b)) {
plugins = b
} else {
options = b
plugins = c
}
} else {
if (Array.isArray(a)) {
plugins = a
} else {
options = a
plugins = b
}
}
return [createHarness, options, plugins]
}
// createHarnessFactory(options)
// createHarnessFactory(plugins)
// createHarnessFactory(options, plugins)
// createHarnessFactory(createHarness, options)
// createHarnessFactory(createHarness, plugins)
// createHarnessFactory(createHarness, options, plugins)
export const createHarnessFactory = (...args) => {
const [
createHarness = createZoraHarness,
factoryOptions = {},
factoryPlugins = [],
] = parseCreateHarnessFactoryArgs(args)
const createZoraxHarness = (
harnessOptions = {},
harnessPlugins = [],
...args
) => {
if (Array.isArray(harnessOptions)) {
harnessPlugins = harnessOptions
harnessOptions = {}
}
const options = { ...factoryOptions, ...harnessOptions }
const harness = createHarness(options, ...args)
harness.options = options
const plugins = [...factoryPlugins, ...harnessPlugins].filter(Boolean)
harness[REPORT_HOOKS] = plugins.filter(pg => pg.report).map(pg => pg.report)
const { report } = harness
harness.report = async (...args) => {
const after = []
for (const reportHook of harness[REPORT_HOOKS]) {
const afterHook = await reportHook(harness, args)
if (afterHook) after.push(afterHook)
}
await report.apply(harness, args)
for (const afterHook of after) {
await afterHook()
}
}
augmentProxy(harness, harness, plugins, getHarnessHooks)
return harness
}
return createZoraxHarness
}
export const createHarness = createHarnessFactory(createZoraHarness)
createHarness.plug = noop