Skip to content

Commit 0dfe00e

Browse files
iamdoroncjihrig
authored andcommitted
Add the correct thrownAt() location when using expect().to.reject()
1 parent 40e5271 commit 0dfe00e

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.expect = function (value, prefix) {
2929
const location = at.filename + ':' + at.line + '.' + at.column;
3030
internals.locations[location] = true;
3131
++internals.count;
32-
return new internals.Assertion(value, prefix, location);
32+
return new internals.Assertion(value, prefix, location, at);
3333
};
3434

3535

@@ -52,11 +52,12 @@ exports.count = function () {
5252
};
5353

5454

55-
internals.Assertion = function (ref, prefix, location) {
55+
internals.Assertion = function (ref, prefix, location, at) {
5656

5757
this._ref = ref;
5858
this._prefix = prefix || '';
5959
this._location = location;
60+
this._at = at;
6061
this._flags = {};
6162
};
6263

@@ -112,7 +113,7 @@ internals.Assertion.prototype.assert = function (result, verb, actual, expected)
112113
Error.captureStackTrace(error, this.assert);
113114
error.actual = actual;
114115
error.expected = expected;
115-
error.at = exports.thrownAt(error);
116+
error.at = exports.thrownAt(error) || this._at;
116117
throw error;
117118
};
118119

@@ -522,7 +523,7 @@ exports.thrownAt = function (error) {
522523
error = error || new Error();
523524
const stack = typeof error.stack === 'string' ? error.stack : '';
524525
const frame = stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0] || '';
525-
const at = frame.match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/);
526+
const at = frame.match(/^\s*at [^(]*\(?(.+)\:(\d+)\:(\d+)\)?$/);
526527
return Array.isArray(at) ? {
527528
filename: at[1],
528529
line: at[2],

test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ describe('expect()', () => {
12181218

12191219
Hoek.assert(exception.message === 'some message', exception);
12201220
Hoek.assert(exception.at.line !== Code.thrownAt(failed).line, 'Reports the wrong line number');
1221+
Hoek.assert(exception.at.filename === __filename, `expected ${exception.at.filename} to equal ${__filename}`);
12211222
});
12221223
});
12231224

@@ -2194,13 +2195,16 @@ describe('expect()', () => {
21942195
const Custom = function () { };
21952196

21962197
try {
2198+
var expectedLineNumber = Number(new Error().stack.match(/:(\d+)/)[1]) + 1;
21972199
await Code.expect(Promise.reject(new Custom())).to.reject('kaboom');
21982200
}
21992201
catch (err) {
22002202
var exception = err;
22012203
}
22022204

22032205
Hoek.assert(exception.message === 'Expected [Promise] to reject with an error with specified message', exception);
2206+
Hoek.assert(Number(exception.at.line) === expectedLineNumber, `expected ${expectedLineNumber}, got ${exception.at.line}`);
2207+
Hoek.assert(exception.at.filename === __filename, `expected ${exception.at.filename} to equal ${__filename}`);
22042208
});
22052209

22062210
it('invalidates rejection (message)', async () => {

0 commit comments

Comments
 (0)