-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjasmine-custom-message.js
More file actions
150 lines (130 loc) · 4.27 KB
/
jasmine-custom-message.js
File metadata and controls
150 lines (130 loc) · 4.27 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* jasmine-custom-message
* https://github.com/avrelian/jasmine-custom-message
*
* Copyright (c) 2014 Sergey Radchenko
* Licensed under the MIT license.
*/
'use strict';
(function() {
var global = Function('return this')();
if (! (global.jasmine && global.expect)) {
return;
}
var isBrowserEnv = global.window && global === global.window;
var isCommonJS = typeof module !== 'undefined' && typeof module.exports === 'object';
var isPromise = function(val) {
return val && typeof val.then === 'function';
};
var ofType = function(val) {
var types = [].slice.call(arguments, 1);
var valType = val === null ? 'null' : typeof val;
return types.indexOf(valType) > -1;
};
var formatString = function(data, message) {
message = message.replace(/#\{actual\}/g, data.actual);
message = message.replace(/#\{expected\}/g, data.expected);
return message;
};
var getMessage = function(assertion, message, data, args) {
while (! ofType(message, 'string', 'number', 'boolean')) {
switch (true) {
case ofType(message, 'undefined', 'null'):
message = 'You cannot use `' + message + '` as a custom message';
break;
case ofType(message, 'function'):
message = message.apply(assertion, args);
break;
case message && ofType(message.toString, 'function') && message.toString().indexOf('[object ') !== 0:
message = message.toString();
break;
case JSON && ofType(JSON.stringify, 'function'):
message = JSON.stringify(message);
break;
default:
message = 'N/A';
}
}
if (ofType(message, 'string')) {
return formatString(data, message);
}
return message.toString();
};
var match = function(assertion, matcher, not, expected) {
if (not) {
assertion.not[matcher](expected);
} else {
assertion[matcher](expected);
}
};
var wrapMatcher = function(matcher, promiseActual, not, customMessage) {
return function(expected) {
promiseActual.then(function(actual) {
var assertion = global.since(customMessage).expect(actual);
if (isPromise(expected)) {
expected.then(function(expectedValue) {
match(assertion, matcher, not, expectedValue);
}, function(expectedError) {
match(assertion, matcher, not, expectedError);
});
} else {
match(assertion, matcher, not, expected);
}
}, function(actualError) {
var assertion = global.since(customMessage).expect(actualError);
if (isPromise(expected)) {
expected.then(function(expectedValue) {
match(assertion, matcher, not, expectedValue);
}, function(expectedError) {
match(assertion, matcher, not, expectedError);
});
} else {
match(assertion, matcher, not, expected);
}
});
};
};
// slightly modified code from https://github.com/angular/jasminewd
var wrapMatchers = function(customMessage, promiseActual) {
var promises = {not: {}};
var env = jasmine.getEnv();
var matchersClass = env.currentSpec.matchersClass || env.matchersClass;
for (var matcher in matchersClass.prototype) {
promises[matcher] = wrapMatcher(matcher, promiseActual, false, customMessage);
promises.not[matcher] = wrapMatcher(matcher, promiseActual, true, customMessage);
}
return promises;
};
var wrapExpect = function(expect, customMessage) {
return function(actual) {
if (isPromise(actual)) {
return wrapMatchers(customMessage, actual);
}
var assertion = expect(actual);
if (! ofType(customMessage, 'undefined', 'null')) {
assertion.message = assertion.not.message = function() {
var data = {
actual: actual,
expected: arguments[0]
};
return getMessage(assertion, customMessage, data, arguments);
};
}
return assertion;
};
};
var defineSince = function() {
global.since = function(customMessage) {
return {
expect: wrapExpect(global.expect, customMessage)
};
};
};
if (isBrowserEnv) {
defineSince();
} else {
if (isCommonJS) {
module.exports = defineSince();
}
}
})();