/
index.js
104 lines (78 loc) · 1.93 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
var tape = require('tape')
var isPromise = require('is-promise')
var merge = require('merge')
var figures = require('figures')
function namespace (name, setup) {
setup = setup || {}
var beforeEach = setup.beforeEach || []
var afterEach = setup.afterEach || []
var tapeFn = tape
var tester = function (name2, fn) {
var testName = name ? name + ' ' + figures.arrowRight + ' ' + name2 : name2
return tapeFn(testName, function (t) {
var end = t.end.bind(t)
// Call all beforeEach fns and set return as context
// to pass to test methods
var beforeEachReturns = beforeEach.map(function (be) {
return be()
})
t.context = merge.apply(null, beforeEachReturns)
t.end = function () {
afterEach.forEach(function (ae) {
ae({
context: t.context
})
})
end()
}
//
var ret = fn({
equal: t.equal,
deepEqual: t.deepEqual,
pass: t.pass,
fail: t.fail,
notDeepEqual: t.notDeepEqual,
notEqual: t.notEqual,
context: t.context,
plan: t.plan
})
// Promise
if (isPromise(ret)) {
ret.then(function () {
return end()
})
}
// Thunk
else if (typeof ret === 'function') {
ret(end)
}
// Call end of synchronous tests automatically
else {
end()
}
})
}
tester.beforeEach = function (fn) {
return beforeEach.push(fn)
}
tester.afterEach = function (fn) {
return afterEach.push(fn)
}
tester.namespace = function (name2) {
return namespace(name + ' -> ' + name2, {
beforeEach: beforeEach,
afterEach: afterEach,
})
}
tester.only = function () {
tapeFn = tape.only
tester.apply(null, arguments)
tapeFn = tape
}
tester.skip = function () {}
return tester
}
module.exports = {
namespace: namespace,
test: tape
}