-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (93 loc) · 2.53 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
import { ref, initialized } from './lib/runner.js'
import test from './lib/test.js'
import TestSuite from './lib/suite.js'
ref.set('running', false)
setTimeout(() => {
if (!initialized && ref.get('autostart')) {
ref.set('initialized', true)
run()
}
}, 300)
ref.set('testid', 0)
function runner (fn, name = null, directive = null) {
const TestRunner = ref.get('runner') || TestSuite
return new Promise((resolve, reject) => {
const suite = new TestRunner(name, resolve, reject, ref.get('testid'), directive, ref.get('logger') || console)
try {
fn(suite)
} catch (e) {
suite.fail(e.stack.split('\n')[0].trim(), {
stack: '| \n ' + e.stack
})
suite.end()
// reject(new Error(e))
}
})
}
async function run () {
// Prevent running more than once
if (ref.get('running')) {
return
}
ref.set('running', true)
const tests = ref.get('tests') || []
const logger = ref.get('logger') || console
const TestRunner = ref.get('runner') || TestSuite
const before = ref.get('before')
const after = ref.get('after')
const beforeEach = ref.get('beforeEach')
const afterEach = ref.get('afterEach')
// Signal start of test execution
ref.get('emitter').emit('start')
// Identify TAP version
logger.log('TAP version 13')
// Short circuit if there are no tests
if (tests.length === 0) {
logger.log('Bail out! no tests')
ref.set('running', false)
return
}
// Execute pre-suite tests
if (typeof before === 'function') {
await runner(before)
}
// Run tests sequentially
for (const test of tests) {
// Execute pre-tests
if (typeof beforeEach === 'function') {
await runner(beforeEach)
}
await
runner(test[1], test[0], test.length === 3 ? test[2] : null)
.then(count => { ref.set('testid', ref.get('testid') + count) })
.catch(abort)
// Execute pre-tests
if (typeof afterEach === 'function') {
await runner(afterEach)
}
}
// Execute post-suite tests
if (typeof after === 'function') {
await runner(after)
}
// Output the final plan on "next tick"
logger.log(`1..${ref.get('testid')}`)
// Signal end of test execution
ref.set('running', false)
ref.get('emitter').emit('end')
}
function abort (e) {
(ref.get('logger') || console).log(`Bail out! ${e.message}`)
ref.set('running', false)
try {
process.exit(1)
} catch (e) {
try {
Deno.exit(1)
} catch (e) {
throw ''
}
}
}
ref.set("start", run)
export { test as default, test, TestSuite as Runner }