diff --git a/spec/compile-cache-spec.coffee b/spec/compile-cache-spec.coffee index 0ceb1c76b..8a6cc214e 100644 --- a/spec/compile-cache-spec.coffee +++ b/spec/compile-cache-spec.coffee @@ -71,13 +71,16 @@ describe 'CompileCache', -> expect(CSONParser.parse.callCount).toBe 1 describe 'overriding Error.prepareStackTrace', -> - it 'removes the override on the next tick', -> + it 'removes the override on the next tick, and always assigns the raw stack', -> Error.prepareStackTrace = -> 'a-stack-trace' error = new Error("Oops") expect(error.stack).toBe 'a-stack-trace' + expect(Array.isArray(error.getRawStack())).toBe true waits(1) runs -> error = new Error("Oops again") - expect(error.stack).not.toBe 'a-stack-trace' + console.log error.stack + expect(error.stack).toContain('compile-cache-spec.coffee') + expect(Array.isArray(error.getRawStack())).toBe true diff --git a/src/compile-cache.js b/src/compile-cache.js index 81ade6ac0..bedbd2549 100644 --- a/src/compile-cache.js +++ b/src/compile-cache.js @@ -158,29 +158,31 @@ require('source-map-support').install({ } }) -var sourceMapPrepareStackTrace = Error.prepareStackTrace +var prepareStackTraceWithSourceMapping = Error.prepareStackTrace -// Enable Grim to access the raw stack by customizing Error.prepareStackTrace -function prepareStackTraceWithRawStack (error, frames) { +let prepareStackTrace = prepareStackTraceWithSourceMapping + +function prepareStackTraceWithRawStackAssignment (error, frames) { error.rawStack = frames - return sourceMapPrepareStackTrace(error, frames) + return prepareStackTrace(error, frames) } -let prepareStackTrace = prepareStackTraceWithRawStack - -// Prevent coffee-script from reassigning Error.prepareStackTrace Object.defineProperty(Error, 'prepareStackTrace', { - get: function () { return prepareStackTrace }, + get: function () { + return prepareStackTraceWithRawStackAssignment + }, + set: function (newValue) { prepareStackTrace = newValue process.nextTick(function () { - prepareStackTrace = prepareStackTraceWithRawStack + prepareStackTrace = prepareStackTraceWithSourceMapping }) } }) Error.prototype.getRawStack = function () { // eslint-disable-line no-extend-native - // Call this.stack first to ensure rawStack is generated + // Access this.stack to ensure prepareStackTrace has been run on this error + // because it assigns this.rawStack as a side-effect this.stack return this.rawStack }