-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
130 lines (112 loc) · 3.25 KB
/
test.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
/*!
* try-catch-callback <https://github.com/hybridables/try-catch-callback>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var test = require('mukla')
var tryCatch = require('./index')
test('should throw TypeError if `fn` not a function', function (done) {
function fixture () {
tryCatch(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `fn` to be a function/)
done()
})
test('should throw TypeError if no function passed to the thunk', function (done) {
var thunk = tryCatch(function () {
return 'qux'
})
function fixture () {
thunk(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `cb` to be a function/)
done()
})
test('should get error in `cb` if `fn` throws', function (done) {
tryCatch(function () {
throw new Error('foo qux')
}, function (err) {
test.ifError(!err)
test.strictEqual(err.name, 'Error')
test.strictEqual(err.message, 'foo qux')
done()
})
})
test('should get result in `cb` if `fn` returns', function (done) {
tryCatch(function () {
return 'foo bar'
}, function (err, res) {
test.ifError(err)
test.strictEqual(res, 'foo bar')
done()
})
})
test('should return thunk if no `cb` passed', function (done) {
var thunk = tryCatch(function () {
return 123
})
thunk(function (err, res) {
test.ifError(err)
test.strictEqual(res, 123)
done()
})
})
test('should pass the `cb` to `fn` if 3rd arg is strictly `true`', function (done) {
tryCatch(function (cb) {
test.strictEqual(typeof cb, 'function')
}, { passCallback: true }, done)
})
test('should `fn` not have arguments if 3rd arg is not `true`', function (done) {
tryCatch(function () {
test.strictEqual(arguments.length, 0)
}, done)
})
test('should be able to pass custom arguments to `fn` through options', function (done) {
tryCatch(function (foo, bar, qux) {
test.strictEqual(arguments.length, 3)
test.strictEqual(foo, 1)
test.strictEqual(bar, true)
test.strictEqual(qux, 'foo')
}, { args: [1, true, 'foo'] }, done)
})
test('should be able to pass custom `fn` context through options', function (done) {
tryCatch(function (aa) {
test.strictEqual(aa, 'bb')
test.strictEqual(this.foo, 'bar')
}, {
context: { foo: 'bar' },
args: 'bb'
}, done)
})
test('should pass custom context using `tryCatch.call`', function (done) {
tryCatch.call({ abc: 'def', qux: true }, function () {
test.strictEqual(this.abc, 'def')
test.strictEqual(this.qux, true)
}, done)
})
test('should allow passing options when want thunk', function (done) {
var thunk = tryCatch(function () {
test.strictEqual(this.a, 'b')
}, { context: { a: 'b' } })
test.strictEqual(typeof thunk, 'function')
thunk(done)
})
test('should return error if `opts.return:true`', function (done) {
var err = tryCatch(function () {
throw new TypeError('foo baq xx')
}, { return: true })
test.strictEqual(err.name, 'TypeError')
test.strictEqual(err.message, 'foo baq xx')
done()
})
test('should return value if `opts.return:true`', function (done) {
var val = tryCatch(function () {
return 123
}, { return: true })
test.strictEqual(val, 123)
done()
})