-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
toBeRejectedWithError.js
97 lines (82 loc) · 2.98 KB
/
toBeRejectedWithError.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
getJasmineRequireObj().toBeRejectedWithError = function(j$) {
/**
* Expect a promise to be rejected with a value matched to the expected
* @function
* @async
* @name async-matchers#toBeRejectedWithError
* @since 3.5.0
* @param {Error} [expected] - `Error` constructor the object that was thrown needs to be an instance of. If not provided, `Error` will be used.
* @param {RegExp|String} [message] - The message that should be set on the thrown `Error`
* @example
* await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, 'Error message');
* await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, /Error message/);
* await expectAsync(aPromise).toBeRejectedWithError(MyCustomError);
* await expectAsync(aPromise).toBeRejectedWithError('Error message');
* return expectAsync(aPromise).toBeRejectedWithError(/Error message/);
*/
return function toBeRejectedWithError() {
return {
compare: function(actualPromise, arg1, arg2) {
if (!j$.isPromiseLike(actualPromise)) {
throw new Error('Expected toBeRejectedWithError to be called on a promise.');
}
var expected = getExpectedFromArgs(arg1, arg2);
return actualPromise.then(
function() {
return {
pass: false,
message: 'Expected a promise to be rejected but it was resolved.'
};
},
function(actualValue) { return matchError(actualValue, expected); }
);
}
};
};
function matchError(actual, expected) {
if (!j$.isError_(actual)) {
return fail(expected, 'rejected with ' + j$.pp(actual));
}
if (!(actual instanceof expected.error)) {
return fail(expected, 'rejected with type ' + j$.fnNameFor(actual.constructor));
}
var actualMessage = actual.message;
if (actualMessage === expected.message || typeof expected.message === 'undefined') {
return pass(expected);
}
if (expected.message instanceof RegExp && expected.message.test(actualMessage)) {
return pass(expected);
}
return fail(expected, 'rejected with ' + j$.pp(actual));
}
function pass(expected) {
return {
pass: true,
message: 'Expected a promise not to be rejected with ' + expected.printValue + ', but it was.'
};
}
function fail(expected, message) {
return {
pass: false,
message: 'Expected a promise to be rejected with ' + expected.printValue + ' but it was ' + message + '.'
};
}
function getExpectedFromArgs(arg1, arg2) {
var error, message;
if (isErrorConstructor(arg1)) {
error = arg1;
message = arg2;
} else {
error = Error;
message = arg1;
}
return {
error: error,
message: message,
printValue: j$.fnNameFor(error) + (typeof message === 'undefined' ? '' : ': ' + j$.pp(message))
};
}
function isErrorConstructor(value) {
return typeof value === 'function' && (value === Error || j$.isError_(value.prototype));
}
};