-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
134 lines (120 loc) · 3.67 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
131
132
133
134
/*!
* try-catch-core <https://github.com/hybridables/try-catch-core>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('mukla')
var tryCatchCore = require('./index')
var utils = require('./utils')
test('should throw TypeError if `fn` (the 1st arg) not a function', function (done) {
function fixture () {
tryCatchCore(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /try-catch-core: expect `fn` to be a function/)
done()
})
test('should throw TypeError if no function is passed to `thunk`', function (done) {
var thunk = tryCatchCore(function () {
return 'foobar'
})
function fixture () {
thunk(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /try-catch-core: expect `cb` to be a function/)
done()
})
test('should return thunk if `cb` not a function', function (done) {
var thunk = tryCatchCore(function (next) {
next(null, 123)
})
test.strictEqual(typeof thunk, 'function')
test.strictEqual(utils.isAsync(thunk), true)
thunk(function (err, num) {
test.ifError(err)
test.strictEqual(num, 123)
done()
})
})
test('should be able `fn` to return sync result and get it in `cb`', function (done) {
tryCatchCore(function () {
return 'foo bar'
}, function cb (err, res) {
test.ifError(err)
test.strictEqual(res, 'foo bar')
done()
})
})
test('should pass error to `cb` if throws in `fn`', function (done) {
tryCatchCore(function () {
qux // eslint-disable-line no-undef, no-unused-expressions
return 'foo bar'
}, function cb (err, res) {
test.ifError(!err)
test.strictEqual(err.name, 'ReferenceError')
test.strictEqual(err.message, 'qux is not defined')
test.strictEqual(res, undefined)
done()
})
})
test('should pass error from `fs.readFile` to `cb`', function (done) {
tryCatchCore(function (next) {
fs.readFile('not-existing', next)
}, function cb (err, res) {
test.ifError(!err)
test.strictEqual(err.name, 'Error')
test.ok(/no such file or directory/.test(err.message))
test.strictEqual(res, undefined)
done()
})
})
test('should pass result from `fs.readFile` to the callback', function (done) {
tryCatchCore(function (next) {
fs.readFile('package.json', 'utf-8', next)
}, function cb (err, str) {
test.ifError(err)
test.strictEqual(typeof str, 'string')
test.strictEqual(JSON.parse(str).name, 'try-catch-core')
test.strictEqual(JSON.parse(str).license, 'MIT')
done()
})
})
test('should get result of `fs.readFileSync`', function (done) {
tryCatchCore(function () {
return fs.readFileSync('./README.md', 'utf8')
}, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(res.indexOf('try-catch-core') !== -1, true)
done()
})
})
test('should be able to pass custom arguments through options', function (done) {
tryCatchCore(function (foo, bar, next) {
test.strictEqual(arguments.length, 3)
test.strictEqual(foo, 1)
test.strictEqual(bar, 2)
next(null, foo)
}, { args: [ 1, 2 ] }, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(res, 1)
done()
})
})
test('should not pass a callback to `fn` if passCallback:false', function (done) {
tryCatchCore(function () {
test.strictEqual(arguments.length, 0)
}, { passCallback: false }, function (err, res) {
test.strictEqual(err, null)
test.strictEqual(res, undefined)
done()
})
})
test('should pass custom context to `fn` through options', function (done) {
tryCatchCore(function () {
test.strictEqual(this.foo, 'bar')
}, { context: { foo: 'bar' } }, done)
})