@@ -59,6 +59,19 @@ module.exports = (chai, utils) => {
5959 promise . then ( ( ) => done ( ) , done ) ;
6060 }
6161
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+
6275 // These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`.
6376 function assertIfNegated ( assertion , message , extra ) {
6477 assertion . assert ( true , null , message , extra . expected , extra . actual ) ;
@@ -98,9 +111,11 @@ module.exports = (chai, utils) => {
98111 return value ;
99112 } ,
100113 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 ) ;
104119 return reason ;
105120 }
106121 ) ;
@@ -118,9 +133,12 @@ module.exports = (chai, utils) => {
118133 return value ;
119134 } ,
120135 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 ) ;
124142
125143 // Return the reason, transforming this into a fulfillment, to allow further assertions, e.g.
126144 // `promise.should.be.rejected.and.eventually.equal("reason")`.
@@ -192,34 +210,36 @@ module.exports = (chai, utils) => {
192210
193211 const reasonName = getReasonName ( reason ) ;
194212
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+ }
212241 }
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 ) ;
223243
224244 return reason ;
225245 }
0 commit comments