-
Notifications
You must be signed in to change notification settings - Fork 21
/
exception.js
85 lines (68 loc) · 2.7 KB
/
exception.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
"use strict";
var samsam = require("@sinonjs/samsam");
var hasOwnProperty = require("@sinonjs/commons").prototypes.object
.hasOwnProperty;
var captureException = require("../capture-exception");
var typeNoExceptionMessage =
"${customMessage}Expected ${expected} but no exception was thrown";
var typeFailMessage =
"${customMessage}Expected ${expected} but threw ${actualExceptionType} (${actualExceptionMessage})\n${actualExceptionStack}";
var matchFailMessage =
"${customMessage}Expected thrown ${actualExceptionType} (${actualExceptionMessage}) to pass matcher function";
module.exports = function(referee) {
referee.captureException = captureException;
referee.add("exception", {
assert: function(callback) {
var matcher = arguments[1];
var customMessage = arguments[2];
if (typeof matcher === "string") {
customMessage = matcher;
matcher = undefined;
}
this.expected = matcher;
this.customMessage = customMessage;
var err = captureException(callback);
if (err) {
this.actualExceptionType = err.name;
this.actualExceptionMessage = err.message;
this.actualExceptionStack = err.stack;
}
if (!err) {
if (typeof matcher === "object") {
return this.fail(typeNoExceptionMessage);
}
return this.fail("message");
}
if (typeof matcher === "object" && !samsam.match(err, matcher)) {
var prop;
for (prop in matcher) {
if (
hasOwnProperty(matcher, prop) &&
!samsam.match(err[prop], matcher[prop])
) {
return this.fail(typeFailMessage);
}
}
return true;
}
if (typeof matcher === "function" && matcher(err) !== true) {
return this.fail(matchFailMessage);
}
return true;
},
refute: function(callback) {
var err = captureException(callback);
if (err) {
this.customMessage = arguments[1];
this.actualExceptionType = err.name;
this.actualExceptionMessage = err.message;
return false;
}
return true;
},
expectation: "toThrow",
assertMessage: "${customMessage}Expected exception",
refuteMessage:
"${customMessage}Expected not to throw but threw ${actualExceptionType} (${actualExceptionMessage})"
});
};