diff --git a/lib/index.js b/lib/index.js index 2e675b2..8c98ea6 100755 --- a/lib/index.js +++ b/lib/index.js @@ -80,7 +80,11 @@ internals.Assertion.prototype.assert = function (result, verb, actual, expected) this._ref instanceof Error) { const original = this._ref; - original.at = internals.at(this._ref); + const at = internals.at(this._ref); + + if (at !== undefined) { + original.at = at; + } throw original; } @@ -432,12 +436,14 @@ internals.type = function (value) { internals.at = function (error) { error = error || new Error(); - const at = error.stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0].match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/); - return { + const stack = typeof error.stack === 'string' ? error.stack : ''; + const frame = stack.replace(error.toString(), '').split('\n').slice(1).filter(internals.filterLocal)[0] || ''; + const at = frame.match(/^\s*at \(?(.+)\:(\d+)\:(\d+)\)?$/); + return Array.isArray(at) ? { filename: at[1], line: at[2], column: at[3] - }; + } : undefined; }; diff --git a/test/index.js b/test/index.js index 8bd7ca3..4624e4f 100755 --- a/test/index.js +++ b/test/index.js @@ -1039,6 +1039,23 @@ describe('expect()', () => { Hoek.assert(exception.message === 'some message', exception); done(); }); + + it('validates assertion (error with incomplete stack)', (done) => { + + let exception = false; + try { + const err = new Error('foo'); + err.stack = undefined; + Code.expect(err).to.not.exist(); + } + catch (err) { + exception = err; + } + + Hoek.assert(exception.message === 'foo', exception); + Hoek.assert(exception.at === undefined, exception); + done(); + }); }); describe('empty()', () => {