From a8a5b839abf524fb6f55e7d875b5009d9385f962 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 7 Feb 2018 21:10:53 -0600 Subject: [PATCH 1/3] allow adding a deprecation object --- src/core/Env.js | 8 ++++---- src/core/ExpectationResult.js | 12 ++++++++---- src/core/Spec.js | 7 +++++-- src/core/Suite.js | 7 +++++-- 4 files changed, 22 insertions(+), 12 deletions(-) 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) { From c859128537e9e5a8c48f5b5634b446f7a8f2b937 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 7 Feb 2018 23:39:22 -0600 Subject: [PATCH 2/3] add tests --- spec/core/integration/EnvSpec.js | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 58109f8c..c03e3132 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,67 @@ 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 = new Error('top level deprecation'), + suiteLevelError = new Error('suite level deprecation'), + specLevelError = new Error('spec level deprecation'); + + + + // 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); + }); + }); + + console.log(env.topSuite()); + + env.execute(); + }); }); From 6193bc113b613f83dfeb48e958f5a46c9eb75585 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 8 Feb 2018 15:12:14 -0600 Subject: [PATCH 3/3] throw errors --- spec/core/integration/EnvSpec.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index c03e3132..93baf7ed 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2349,11 +2349,11 @@ describe("Env integration", function() { var env = new jasmineUnderTest.Env(), exceptionFormatter = new jasmineUnderTest.ExceptionFormatter(), reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']), - topLevelError = new Error('top level deprecation'), - suiteLevelError = new Error('suite level deprecation'), - specLevelError = new Error('spec level deprecation'); - + 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"); @@ -2403,8 +2403,6 @@ describe("Env integration", function() { }); }); - console.log(env.topSuite()); - env.execute(); }); });