-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtspl.js
68 lines (54 loc) · 1.08 KB
/
tspl.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
'use strict'
const assert = require('node:assert')
function tspl (t, opts = {}) {
if (t) {
t.after(autoEnd)
}
let ended = false
const { plan } = opts
let actual = 0
let _resolve
const completed = new Promise((resolve) => {
_resolve = resolve
})
async function autoEnd () {
if (ended) {
return
}
if (plan) {
assert.strictEqual(actual, plan, 'The plan was not completed')
} else {
assert.fail('The plan was not completed')
}
}
function end () {
if (ended) {
return
}
ended = true
if (plan) {
assert.strictEqual(actual, plan, 'The plan was not completed')
_resolve()
}
}
const res = {
completed,
end
}
for (const method of Object.keys(assert)) {
if (method.match(/^[a-z]/)) {
res[method] = (...args) => {
actual++
const res = assert[method](...args)
if (actual === plan) {
_resolve()
}
return res
}
}
}
return res
}
module.exports = tspl
module.exports.default = tspl
module.exports.tspl = tspl