@@ -59,6 +59,19 @@ module.exports = (chai, utils) => {
59
59
promise . then ( ( ) => done ( ) , done ) ;
60
60
}
61
61
62
+ function replaceExceptionStack ( f , originalError ) {
63
+ try {
64
+ f ( ) ;
65
+ } catch ( e ) {
66
+ if ( originalError )
67
+ {
68
+ const message_lines = ( originalError . message . match ( / \n / g) || [ ] ) . length + 1 ;
69
+ e . stack = e . message + '\n' + originalError . stack . split ( '\n' ) . slice ( message_lines ) . join ( '\n' ) ;
70
+ }
71
+ throw e ;
72
+ }
73
+ }
74
+
62
75
// These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`.
63
76
function assertIfNegated ( assertion , message , extra ) {
64
77
assertion . assert ( true , null , message , extra . expected , extra . actual ) ;
@@ -98,9 +111,11 @@ module.exports = (chai, utils) => {
98
111
return value ;
99
112
} ,
100
113
reason => {
101
- assertIfNotNegated ( this ,
102
- "expected promise to be fulfilled but it was rejected with #{act}" ,
103
- { actual : getReasonName ( reason ) } ) ;
114
+ replaceExceptionStack ( ( ) =>
115
+ assertIfNotNegated ( this ,
116
+ "expected promise to be fulfilled but it was rejected with #{act}" ,
117
+ { actual : getReasonName ( reason ) } ) ,
118
+ reason ) ;
104
119
return reason ;
105
120
}
106
121
) ;
@@ -118,9 +133,12 @@ module.exports = (chai, utils) => {
118
133
return value ;
119
134
} ,
120
135
reason => {
121
- assertIfNegated ( this ,
122
- "expected promise not to be rejected but it was rejected with #{act}" ,
123
- { actual : getReasonName ( reason ) } ) ;
136
+ replaceExceptionStack ( ( ) =>
137
+ assertIfNegated ( this ,
138
+ "expected promise not to be rejected but it was rejected with #{act}" ,
139
+ { actual : getReasonName ( reason ) } ,
140
+ reason ) ,
141
+ reason ) ;
124
142
125
143
// Return the reason, transforming this into a fulfillment, to allow further assertions, e.g.
126
144
// `promise.should.be.rejected.and.eventually.equal("reason")`.
@@ -192,34 +210,36 @@ module.exports = (chai, utils) => {
192
210
193
211
const reasonName = getReasonName ( reason ) ;
194
212
195
- if ( negate && everyArgIsDefined ) {
196
- if ( errorLikeCompatible && errMsgMatcherCompatible ) {
197
- this . assert ( true ,
198
- null ,
199
- "expected promise not to be rejected with #{exp} but it was rejected " +
200
- "with #{act}" ,
201
- errorLikeName ,
202
- reasonName ) ;
203
- }
204
- } else {
205
- if ( errorLike ) {
206
- this . assert ( errorLikeCompatible ,
207
- "expected promise to be rejected with #{exp} but it was rejected with #{act}" ,
208
- "expected promise not to be rejected with #{exp} but it was rejected " +
209
- "with #{act}" ,
210
- errorLikeName ,
211
- reasonName ) ;
213
+ replaceExceptionStack ( ( ) => {
214
+ if ( negate && everyArgIsDefined ) {
215
+ if ( errorLikeCompatible && errMsgMatcherCompatible ) {
216
+ this . assert ( true ,
217
+ null ,
218
+ "expected promise not to be rejected with #{exp} but it was rejected " +
219
+ "with #{act}" ,
220
+ errorLikeName ,
221
+ reasonName ) ;
222
+ }
223
+ } else {
224
+ if ( errorLike ) {
225
+ this . assert ( errorLikeCompatible ,
226
+ "expected promise to be rejected with #{exp} but it was rejected with #{act}" ,
227
+ "expected promise not to be rejected with #{exp} but it was rejected " +
228
+ "with #{act}" ,
229
+ errorLikeName ,
230
+ reasonName ) ;
231
+ }
232
+
233
+ if ( errMsgMatcher ) {
234
+ this . assert ( errMsgMatcherCompatible ,
235
+ `expected promise to be rejected with an error ${ matcherRelation } #{exp} but got ` +
236
+ `#{act}` ,
237
+ `expected promise not to be rejected with an error ${ matcherRelation } #{exp}` ,
238
+ errMsgMatcher ,
239
+ checkError . getMessage ( reason ) ) ;
240
+ }
212
241
}
213
-
214
- if ( errMsgMatcher ) {
215
- this . assert ( errMsgMatcherCompatible ,
216
- `expected promise to be rejected with an error ${ matcherRelation } #{exp} but got ` +
217
- `#{act}` ,
218
- `expected promise not to be rejected with an error ${ matcherRelation } #{exp}` ,
219
- errMsgMatcher ,
220
- checkError . getMessage ( reason ) ) ;
221
- }
222
- }
242
+ } , reason ) ;
223
243
224
244
return reason ;
225
245
}
0 commit comments