diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 50cb7c17..63e30a66 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -641,8 +641,11 @@ getJasmineRequireObj().Spec = function(j$) { return this.getSpecName(this); }; - Spec.prototype.addDeprecationWarning = function(msg) { - this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + Spec.prototype.addDeprecationWarning = function(deprecation) { + if (typeof deprecation === 'string') { + deprecation = { message: deprecation }; + } + this.result.deprecationWarnings.push(this.expectationResultFactory(deprecation)); }; var extractCustomPendingMessage = function(e) { @@ -915,11 +918,11 @@ getJasmineRequireObj().Env = function(j$) { handlingLoadErrors = false; }; - this.deprecated = function(msg) { + this.deprecated = function(deprecation) { var runnable = currentRunnable() || topSuite; - runnable.addDeprecationWarning(msg); - if(typeof console !== 'undefined' && typeof console.warn !== 'undefined') { - console.error('DEPRECATION: ' + msg); + runnable.addDeprecationWarning(deprecation); + if(typeof console !== 'undefined' && typeof console.error === 'function') { + console.error('DEPRECATION:', deprecation); } }; @@ -2530,10 +2533,14 @@ getJasmineRequireObj().buildExpectationResult = function() { var error = options.error; if (!error) { - try { - throw new Error(message()); - } catch (e) { - error = e; + if (options.stack) { + error = options; + } else { + try { + throw new Error(message()); + } catch (e) { + error = e; + } } } return stackFormatter(error); @@ -5749,8 +5756,11 @@ getJasmineRequireObj().Suite = function(j$) { } }; - Suite.prototype.addDeprecationWarning = function(msg) { - this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + Suite.prototype.addDeprecationWarning = function(deprecation) { + if (typeof deprecation === 'string') { + deprecation = { message: deprecation }; + } + this.result.deprecationWarnings.push(this.expectationResultFactory(deprecation)); }; function isFailure(args) { diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 58109f8c..93baf7ed 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2303,6 +2303,9 @@ describe("Env integration", function() { var env = new jasmineUnderTest.Env(), reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); + // prevent deprecation from being desplayed + spyOn(console, "error"); + reporter.jasmineDone.and.callFake(function(result) { expect(result.deprecationWarnings).toEqual([ jasmine.objectContaining({ message: 'top level deprecation' }) @@ -2341,4 +2344,65 @@ describe("Env integration", function() { env.execute(); }); + + it('should report deprecation stack with an error object', function(done) { + var env = new jasmineUnderTest.Env(), + exceptionFormatter = new jasmineUnderTest.ExceptionFormatter(), + reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']), + topLevelError, suiteLevelError, specLevelError; + + try { throw new Error('top level deprecation') } catch (err) { topLevelError = err; } + try { throw new Error('suite level deprecation') } catch (err) { suiteLevelError = err; } + try { throw new Error('spec level deprecation') } catch (err) { specLevelError = err; } + + // prevent deprecation from being desplayed + spyOn(console, "error"); + + reporter.jasmineDone.and.callFake(function(result) { + expect(result.deprecationWarnings).toEqual([ + jasmine.objectContaining({ + message: topLevelError.message, + stack: exceptionFormatter.stack(topLevelError) + }) + ]); + + expect(reporter.suiteDone).toHaveBeenCalledWith(jasmine.objectContaining({ + fullName: 'suite', + deprecationWarnings: [ + jasmine.objectContaining({ + message: suiteLevelError.message, + stack: exceptionFormatter.stack(suiteLevelError) + }) + ] + })); + + expect(reporter.specDone).toHaveBeenCalledWith(jasmine.objectContaining({ + fullName: 'suite spec', + deprecationWarnings: [ + jasmine.objectContaining({ + message: specLevelError.message, + stack: exceptionFormatter.stack(specLevelError) + }) + ] + })); + + done(); + }); + + env.addReporter(reporter); + + env.deprecated(topLevelError); + + env.describe('suite', function() { + env.beforeAll(function() { + env.deprecated(suiteLevelError); + }); + + env.it('spec', function() { + env.deprecated(specLevelError); + }); + }); + + env.execute(); + }); }); diff --git a/src/core/Env.js b/src/core/Env.js index f9fa181e..ccddcdfe 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -200,11 +200,11 @@ getJasmineRequireObj().Env = function(j$) { handlingLoadErrors = false; }; - this.deprecated = function(msg) { + this.deprecated = function(deprecation) { var runnable = currentRunnable() || topSuite; - runnable.addDeprecationWarning(msg); - if(typeof console !== 'undefined' && typeof console.warn !== 'undefined') { - console.error('DEPRECATION: ' + msg); + runnable.addDeprecationWarning(deprecation); + if(typeof console !== 'undefined' && typeof console.error === 'function') { + console.error('DEPRECATION:', deprecation); } }; diff --git a/src/core/ExpectationResult.js b/src/core/ExpectationResult.js index c794ec77..b4a43e4f 100644 --- a/src/core/ExpectationResult.js +++ b/src/core/ExpectationResult.js @@ -45,10 +45,14 @@ getJasmineRequireObj().buildExpectationResult = function() { var error = options.error; if (!error) { - try { - throw new Error(message()); - } catch (e) { - error = e; + if (options.stack) { + error = options; + } else { + try { + throw new Error(message()); + } catch (e) { + error = e; + } } } return stackFormatter(error); diff --git a/src/core/Spec.js b/src/core/Spec.js index ef5f0d02..a2fe3910 100644 --- a/src/core/Spec.js +++ b/src/core/Spec.js @@ -152,8 +152,11 @@ getJasmineRequireObj().Spec = function(j$) { return this.getSpecName(this); }; - Spec.prototype.addDeprecationWarning = function(msg) { - this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + Spec.prototype.addDeprecationWarning = function(deprecation) { + if (typeof deprecation === 'string') { + deprecation = { message: deprecation }; + } + this.result.deprecationWarnings.push(this.expectationResultFactory(deprecation)); }; var extractCustomPendingMessage = function(e) { diff --git a/src/core/Suite.js b/src/core/Suite.js index b64d9489..f4aa633b 100644 --- a/src/core/Suite.js +++ b/src/core/Suite.js @@ -148,8 +148,11 @@ getJasmineRequireObj().Suite = function(j$) { } }; - Suite.prototype.addDeprecationWarning = function(msg) { - this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + Suite.prototype.addDeprecationWarning = function(deprecation) { + if (typeof deprecation === 'string') { + deprecation = { message: deprecation }; + } + this.result.deprecationWarnings.push(this.expectationResultFactory(deprecation)); }; function isFailure(args) {