@@ -29,6 +29,12 @@ class AssertionError extends Error {
29
29
}
30
30
exports . AssertionError = AssertionError ;
31
31
32
+ function getStack ( ) {
33
+ const obj = { } ;
34
+ Error . captureStackTrace ( obj , getStack ) ;
35
+ return obj . stack ;
36
+ }
37
+
32
38
function wrapAssertions ( callbacks ) {
33
39
const pass = callbacks . pass ;
34
40
const pending = callbacks . pending ;
@@ -138,7 +144,7 @@ function wrapAssertions(callbacks) {
138
144
coreAssertThrowsErrorArg = err ;
139
145
}
140
146
141
- const test = fn => {
147
+ const test = ( fn , stack ) => {
142
148
let actual ;
143
149
let threw = false ;
144
150
try {
@@ -160,13 +166,16 @@ function wrapAssertions(callbacks) {
160
166
throw new AssertionError ( {
161
167
assertion : 'throws' ,
162
168
message,
169
+ stack,
163
170
values
164
171
} ) ;
165
172
}
166
173
} ;
167
174
168
175
if ( promise ) {
169
- const intermediate = promise . then ( makeNoop , makeRethrow ) . then ( test ) ;
176
+ // Record stack before it gets lost in the promise chain.
177
+ const stack = getStack ( ) ;
178
+ const intermediate = promise . then ( makeNoop , makeRethrow ) . then ( fn => test ( fn , stack ) ) ;
170
179
pending ( this , intermediate ) ;
171
180
// Don't reject the returned promise, even if the assertion fails.
172
181
return intermediate . catch ( noop ) ;
@@ -196,20 +205,23 @@ function wrapAssertions(callbacks) {
196
205
return ;
197
206
}
198
207
199
- const test = fn => {
208
+ const test = ( fn , stack ) => {
200
209
try {
201
210
coreAssert . doesNotThrow ( fn ) ;
202
211
} catch ( err ) {
203
212
throw new AssertionError ( {
204
213
assertion : 'notThrows' ,
205
214
message,
215
+ stack,
206
216
values : [ formatAssertError . formatWithLabel ( 'Threw:' , err . actual ) ]
207
217
} ) ;
208
218
}
209
219
} ;
210
220
211
221
if ( promise ) {
212
- const intermediate = promise . then ( noop , reason => test ( makeRethrow ( reason ) ) ) ;
222
+ // Record stack before it gets lost in the promise chain.
223
+ const stack = getStack ( ) ;
224
+ const intermediate = promise . then ( noop , reason => test ( makeRethrow ( reason ) , stack ) ) ;
213
225
pending ( this , intermediate ) ;
214
226
// Don't reject the returned promise, even if the assertion fails.
215
227
return intermediate . catch ( noop ) ;
0 commit comments