-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
457 lines (415 loc) · 11 KB
/
index.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import raceTimeout from 'race-timeout-anywhere'
import CompositeClass from 'composite-class'
import StateMachine from 'fsm-base'
import TestContext from './lib/test-context.js'
import { isPromise, isPlainObject, isString, isFunction } from 'typical'
/**
* @module test-object-model
*/
/**
* @param {string} [name] - The test name.
* @param {function} [testFn] - A function which will either succeed, reject or throw.
* @param {object} [options] - Test config.
* @param {number} [options.timeout] - A time limit for the test in ms.
* @param {number} [options.maxConcurrency] - The max concurrency that child tests will be able to run. For example, specifying `2` will allow child tests to run two at a time. Defaults to `10`.
* @param {boolean} [options.skip] - Skip this test.
* @param {boolean} [options.only] - Only run this test.
* @param {boolean} [options.before] - Run this test before its siblings.
* @param {boolean} [options.after] - Run this test after its siblings.
* @param {boolean} [options.todo] - Mark this test as incomplete.
* @param {boolean} [options.group] - Mark this test as a group.
* @alias module:test-object-model
*/
class Tom extends EventTarget {
/**
* Test name
* @type {string}
*/
name = 'tom'
/**
* A function which will either succeed, reject or throw.
* @type {function}
*/
testFn
/**
* Position of this test within its parents children
* @type {number}
*/
index = 1
/**
* True if the test has ended.
* @type {boolean}
*/
ended = false
/**
* If the test passed, the value returned by the test function. If it failed, the exception thrown or rejection reason.
* @type {*}
*/
result
/**
* True if one or more different tests are marked as `only`.
* @type {boolean}
*/
disabledByOnly = false
/**
* The options set when creating the test.
*/
options
/**
* Test execution stats
* @namespace
*/
stats = {
/**
* Start time.
* @type {number}
*/
start: 0,
/**
* End time.
* @type {number}
*/
end: 0,
/**
* Test execution duration.
* @type {number}
*/
duration: 0,
finish: function (end) {
this.end = end
this.duration = this.end - this.start
}
}
/**
* The text execution context.
* @type {TextContext}
*/
context
constructor (name, testFn, options) {
super()
if (name) {
if (isString(name)) {
if (isPlainObject(testFn)) {
options = testFn
testFn = undefined
}
} else if (isFunction(name)) {
options = testFn
testFn = name
name = ''
} else if (isPlainObject(name)) {
options = name
testFn = undefined
name = ''
}
} else {
if (isPlainObject(testFn)) {
options = testFn
testFn = undefined
}
}
/**
* Test state. Can be one of `pending`, `in-progress`, `skipped`, `ignored`, `todo`, `pass` or `fail`.
* @member {string} module:test-object-model#state
*/
this._initStateMachine('pending', [
{ from: 'pending', to: 'in-progress' },
{ from: 'pending', to: 'skipped' },
{ from: 'pending', to: 'ignored' },
{ from: 'pending', to: 'todo' },
{ from: 'in-progress', to: 'pass' },
{ from: 'in-progress', to: 'fail' }
])
this.name = name || 'tom'
this.testFn = testFn
options = Object.assign({}, options)
options.maxConcurrency = options.maxConcurrency || 10
options.timeout = options.timeout || 10000
this.options = options
}
/**
* Returns `test`, `group` or `todo`.
* @returns {string}
*/
get type () {
if (this.options.group) {
return 'group'
} else if (this.options.todo) {
return 'todo'
} else {
if (this.testFn && !this.children.length) {
return 'test'
} else if (!this.testFn && this.children.length) {
return 'group'
} else {
return 'todo'
}
}
}
/**
* Returns `true` if this test was marked to be skipped by usage of `skip` or `only`.
* @returns {booolean}
*/
get toSkip () {
return this.disabledByOnly || this.options.skip
}
/**
* Returns the test name.
* @returns {string}
*/
toString () {
return this.name
}
/**
* Add a test group.
* @param {string} - Test name.
* @param {objects} - Config.
* @return {module:test-object-model}
*/
group (name, options = {}) {
options.group = true
return this.test(name, options)
}
/**
* Add a test.
* @param {string} - Test name.
* @param {function} - Test function.
* @param {objects} - Config.
* @return {module:test-object-model}
*/
test (name, testFn, options = {}) {
/* validate name */
for (const child of this) {
if (child.name === name) {
throw new Error('Duplicate name: ' + name)
}
}
const test = new this.constructor(name, testFn, options)
this.add(test)
test.index = this.children.length
test._disableNonOnlyTests()
return test
}
/**
* Add a skipped test
* @return {module:test-object-model}
*/
skip (name, testFn, options = {}) {
options.skip = true
return this.test(name, testFn, options)
}
/**
* Add an only test
* @return {module:test-object-model}
*/
only (name, testFn, options = {}) {
options.only = true
return this.test(name, testFn, options)
}
/**
* Add a test which must run and complete before the others.
* @return {module:test-object-model}
*/
before (name, testFn, options = {}) {
options.before = true
return this.test(name, testFn, options)
}
/**
* Add a test but don't run it and mark as incomplete.
* @return {module:test-object-model}
*/
todo (name, testFn, options = {}) {
options.todo = true
return this.test(name, testFn, options)
}
/**
* Add a test which must run and complete after the others.
* @return {module:test-object-model}
*/
after (name, testFn, options = {}) {
options.after = true
return this.test(name, testFn, options)
}
_onlyExists () {
return Array.from(this.root()).some(t => t.options.only)
}
_disableNonOnlyTests () {
if (this._onlyExists()) {
for (const test of this.root()) {
test.disabledByOnly = !test.options.only
}
}
}
setState (state, target, data) {
if (state === 'pass' || state === 'fail') {
this.ended = true
}
// super.setState(state, target, data)
this.state = state
this.dispatchEvent(new CustomEvent(state, { detail: { target, data } }))
if (state === 'pass' || state === 'fail') {
// this.emit('end')
this.dispatchEvent(new CustomEvent('end'))
}
}
/**
* Execute the stored test function. Return a promise that either resolves with the value returned by the test function or rejects.
* @returns {Promise}
* @fulfil {*}
*/
async run () {
if (this.options.todo) {
/**
* Test todo.
* @event module:test-object-model#todo
* @param test {TestObjectModel} - The test node.
*/
this.setState('todo', this)
} else {
if (this.testFn) {
if (this.toSkip) {
/**
* Test skipped.
* @event module:test-object-model#skipped
* @param test {TestObjectModel} - The test node.
*/
this.setState('skipped', this)
} else {
return this._runTestFn()
}
} else {
/**
* Test ignored.
* @event module:test-object-model#ignored
* @param test {TestObjectModel} - The test node.
*/
this.setState('ignored', this)
}
}
}
async _runTestFn () {
this.performance = await this._getPerformance()
/**
* Test in-progress.
* @event module:test-object-model#in-progress
* @param test {TestObjectModel} - The test node.
*/
this.setState('in-progress', this)
this.stats.start = this.performance.now()
try {
this.context = new TestContext({ name: this.name, index: this.index })
const testResult = this.testFn.call(this.context)
if (isPromise(testResult)) {
try {
const result = await Promise.race([testResult, raceTimeout(this.options.timeout)])
this._testPassed(result)
return result
} catch (err) {
return Promise.reject(this._testFailed(err))
}
} else {
this._testPassed(testResult)
return testResult
}
} catch (err) {
throw this._testFailed(err)
}
}
_testPassed (result) {
this.result = result
this.stats.finish(this.performance.now())
/**
* Test pass.
* @event module:test-object-model#pass
* @param test {TestObjectModel} - The test node.
* @param result {*} - The value returned by the test.
*/
this.setState('pass', this, result)
}
_testFailed (err) {
const testFailError = new TestFailError(this.name, err)
this.result = err
this.stats.finish(this.performance.now())
/**
* Test fail.
* @event module:test-object-model#fail
* @param test {TestObjectModel} - The test node.
* @param err {Error} - The exception thrown.
*/
this.setState('fail', this, testFailError)
return testFailError
}
/**
* Reset state
*/
reset (deep) {
if (deep) {
for (const tom of this) {
tom.reset()
}
} else {
this.index = 1
this.resetState()
this.disabledByOnly = false
}
}
async _getPerformance () {
if (typeof window === 'undefined') {
const { performance } = await import('perf_hooks')
return performance
} else {
return window.performance
}
}
/**
* Used in the @test-runner/core stats.
*/
getTestCount () {
return Array.from(this).filter(t => t.testFn).length
}
/**
* If more than one TOM instances are supplied, combine them into a common root.
* @param {Array.<Tom>} tests
* @param {string} [name]
* @return {Tom}
*/
static combine (tests, name, options) {
let test
if (tests.length > 1) {
test = new this(name, options)
for (const subTom of tests) {
this.validate(subTom)
test.add(subTom)
}
} else {
test = tests[0]
this.validate(test)
}
test._disableNonOnlyTests()
return test
}
/**
* Returns true if the input is a valid test.
* @param {module:test-object-model} tom - Input to test.
* @returns {boolean}
*/
static validate (tom = {}) {
const valid = ['name', 'testFn', 'index', 'ended'].every(prop => Object.keys(tom).includes(prop))
if (!valid) {
const err = new Error('Valid TOM required')
err.invalidTom = tom
throw err
}
}
}
StateMachine.mixInto(Tom)
CompositeClass.mixInto(Tom)
class TestFailError extends Error {
constructor (name, cause) {
super(`Test failed [${name}]`)
/* Used to differentiate a test suite fail (expected) from a library bug (unexpected) */
this.isTestFail = true
this.cause = cause
}
}
export default Tom