-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.2 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
const assert = require('assert')
const isRegexp = require('is-regexp')
function expectedException (actual, expected) {
if (!actual) return false
if (!expected) return true
if (isRegexp(expected)) return expected.test(actual)
try {
if (actual instanceof expected) return true
} catch (_) {
// Ignore. The instanceof check doesn't work for arrow functions.
}
if (Error.isPrototypeOf(expected)) return false
return expected.call({}, actual) === true
}
module.exports = function assertRejects (promise, error, message) {
if (typeof promise.then !== 'function') {
throw new TypeError('"promise" argument must be a promise')
}
if (typeof error === 'string') {
message = error
error = null
}
message = (error && error.name ? ' (' + error.name + ')' : '') + (message ? ': ' + message : '.')
function onFullfilled () {
throw new assert.AssertionError({
expected: error,
message: 'Missing expected rejection' + message,
operator: 'rejects',
stackStartFn: assertRejects,
stackStartFunction: assertRejects
})
}
function onRejected (err) {
if (!expectedException(err, error)) throw err
}
return promise.then(onFullfilled, onRejected)
}