From 5afe1222f45b7f8eebd7f9da8f9dce7b12728cf8 Mon Sep 17 00:00:00 2001 From: Gregg Van Hove Date: Mon, 5 Feb 2018 11:19:15 -0800 Subject: [PATCH] Add ability to report deprecation warnings from within the suite [#154746527] --- lib/jasmine-core/jasmine.js | 26 ++++++++++++++-- spec/core/SpecSpec.js | 1 + spec/core/integration/EnvSpec.js | 52 ++++++++++++++++++++++++++++++++ src/core/Env.js | 12 +++++++- src/core/Spec.js | 6 ++++ src/core/Suite.js | 8 ++++- 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 23d1441d..ea582c8f 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -505,6 +505,7 @@ getJasmineRequireObj().Spec = function(j$) { * @property {String} fullName - The full description including all ancestors of this spec. * @property {Expectation[]} failedExpectations - The list of expectations that failed during execution of this spec. * @property {Expectation[]} passedExpectations - The list of expectations that passed during execution of this spec. + * @property {Expectation[]} deprecationWarnings - The list of deprecation warnings that occurred during execution this spec. * @property {String} pendingReason - If the spec is {@link pending}, this will be the reason. * @property {String} status - Once the spec has completed, this string represents the pass/fail status of this spec. */ @@ -514,6 +515,7 @@ getJasmineRequireObj().Spec = function(j$) { fullName: this.getFullName(), failedExpectations: [], passedExpectations: [], + deprecationWarnings: [], pendingReason: '' }; } @@ -629,6 +631,10 @@ getJasmineRequireObj().Spec = function(j$) { return this.getSpecName(this); }; + Spec.prototype.addDeprecationWarning = function(msg) { + this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + }; + var extractCustomPendingMessage = function(e) { var fullMessage = e.toString(), boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage), @@ -933,6 +939,14 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; + this.deprecated = function(msg) { + var runnable = currentRunnable() || topSuite; + runnable.addDeprecationWarning(msg); + if(typeof console !== 'undefined' && typeof console.warn !== 'undefined') { + console.error('DEPRECATION: ' + msg); + } + }; + var queueRunnerFactory = function(options) { options.catchException = catchException; options.clearStack = options.clearStack || clearStack; @@ -1025,10 +1039,12 @@ getJasmineRequireObj().Env = function(j$) { * @typedef JasmineDoneInfo * @property {Order} order - Information about the ordering (random or not) of this execution of the suite. * @property {Expectation[]} failedExpectations - List of expectations that failed in an {@link afterAll} at the global level. + * @property {Expectation[]} deprecationWarnings - List of deprecation warnings that occurred at the global level. */ reporter.jasmineDone({ order: order, - failedExpectations: topSuite.result.failedExpectations + failedExpectations: topSuite.result.failedExpectations, + deprecationWarnings: topSuite.result.deprecationWarnings }); }); }; @@ -5147,13 +5163,15 @@ getJasmineRequireObj().Suite = function(j$) { * @property {String} description - The description text passed to the {@link describe} that made this suite. * @property {String} fullName - The full description including all ancestors of this suite. * @property {Expectation[]} failedExpectations - The list of expectations that failed in an {@link afterAll} for this suite. + * @property {Expectation[]} deprecationWarnings - The list of deprecation warnings that occurred on this suite. * @property {String} status - Once the suite has completed, this string represents the pass/fail status of this suite. */ this.result = { id: this.id, description: this.description, fullName: this.getFullName(), - failedExpectations: [] + failedExpectations: [], + deprecationWarnings: [] }; } @@ -5273,6 +5291,10 @@ getJasmineRequireObj().Suite = function(j$) { } }; + Suite.prototype.addDeprecationWarning = function(msg) { + this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + }; + function isAfterAll(children) { return children && children[0].result.status; } diff --git a/spec/core/SpecSpec.js b/spec/core/SpecSpec.js index 8bc538da..6118ffb0 100644 --- a/spec/core/SpecSpec.js +++ b/spec/core/SpecSpec.js @@ -222,6 +222,7 @@ describe("Spec", function() { fullName: 'a suite with a spec', failedExpectations: [], passedExpectations: [], + deprecationWarnings: [], pendingReason: '' }); }); diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index 0adbad94..25d6d69c 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2004,4 +2004,56 @@ describe("Env integration", function() { env.execute(); }); + + it('should report deprecation warnings on the correct specs and suites', function(done) { + var env = new jasmineUnderTest.Env(), + reporter = jasmine.createSpyObj('reporter', ['jasmineDone', 'suiteDone', 'specDone']); + + reporter.jasmineDone.and.callFake(function(result) { + expect(result.deprecationWarnings).toEqual([ + jasmine.objectContaining({ + message: 'top level deprecation', + stack: jasmine.any(String) + }) + ]); + + expect(reporter.suiteDone).toHaveBeenCalledWith(jasmine.objectContaining({ + fullName: 'suite', + deprecationWarnings: [ + jasmine.objectContaining({ + message: 'suite level deprecation', + stack: jasmine.any(String) + }) + ] + })); + + expect(reporter.specDone).toHaveBeenCalledWith(jasmine.objectContaining({ + fullName: 'suite spec', + deprecationWarnings: [ + jasmine.objectContaining({ + message: 'spec level deprecation', + stack: jasmine.any(String) + }) + ] + })); + + done(); + }); + + env.addReporter(reporter); + + env.deprecated('top level deprecation'); + + env.describe('suite', function() { + env.beforeAll(function() { + env.deprecated('suite level deprecation'); + }); + + env.it('spec', function() { + env.deprecated('spec level deprecation'); + }); + }); + + env.execute(); + }); }); diff --git a/src/core/Env.js b/src/core/Env.js index 9655f15d..af1ad374 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -234,6 +234,14 @@ getJasmineRequireObj().Env = function(j$) { return seed; }; + this.deprecated = function(msg) { + var runnable = currentRunnable() || topSuite; + runnable.addDeprecationWarning(msg); + if(typeof console !== 'undefined' && typeof console.warn !== 'undefined') { + console.error('DEPRECATION: ' + msg); + } + }; + var queueRunnerFactory = function(options) { options.catchException = catchException; options.clearStack = options.clearStack || clearStack; @@ -326,10 +334,12 @@ getJasmineRequireObj().Env = function(j$) { * @typedef JasmineDoneInfo * @property {Order} order - Information about the ordering (random or not) of this execution of the suite. * @property {Expectation[]} failedExpectations - List of expectations that failed in an {@link afterAll} at the global level. + * @property {Expectation[]} deprecationWarnings - List of deprecation warnings that occurred at the global level. */ reporter.jasmineDone({ order: order, - failedExpectations: topSuite.result.failedExpectations + failedExpectations: topSuite.result.failedExpectations, + deprecationWarnings: topSuite.result.deprecationWarnings }); }); }; diff --git a/src/core/Spec.js b/src/core/Spec.js index c2ee8a58..f5e50e0f 100644 --- a/src/core/Spec.js +++ b/src/core/Spec.js @@ -25,6 +25,7 @@ getJasmineRequireObj().Spec = function(j$) { * @property {String} fullName - The full description including all ancestors of this spec. * @property {Expectation[]} failedExpectations - The list of expectations that failed during execution of this spec. * @property {Expectation[]} passedExpectations - The list of expectations that passed during execution of this spec. + * @property {Expectation[]} deprecationWarnings - The list of deprecation warnings that occurred during execution this spec. * @property {String} pendingReason - If the spec is {@link pending}, this will be the reason. * @property {String} status - Once the spec has completed, this string represents the pass/fail status of this spec. */ @@ -34,6 +35,7 @@ getJasmineRequireObj().Spec = function(j$) { fullName: this.getFullName(), failedExpectations: [], passedExpectations: [], + deprecationWarnings: [], pendingReason: '' }; } @@ -149,6 +151,10 @@ getJasmineRequireObj().Spec = function(j$) { return this.getSpecName(this); }; + Spec.prototype.addDeprecationWarning = function(msg) { + this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + }; + var extractCustomPendingMessage = function(e) { var fullMessage = e.toString(), boilerplateStart = fullMessage.indexOf(Spec.pendingSpecExceptionMessage), diff --git a/src/core/Suite.js b/src/core/Suite.js index e85ab04e..8a3bef2f 100644 --- a/src/core/Suite.js +++ b/src/core/Suite.js @@ -21,13 +21,15 @@ getJasmineRequireObj().Suite = function(j$) { * @property {String} description - The description text passed to the {@link describe} that made this suite. * @property {String} fullName - The full description including all ancestors of this suite. * @property {Expectation[]} failedExpectations - The list of expectations that failed in an {@link afterAll} for this suite. + * @property {Expectation[]} deprecationWarnings - The list of deprecation warnings that occurred on this suite. * @property {String} status - Once the suite has completed, this string represents the pass/fail status of this suite. */ this.result = { id: this.id, description: this.description, fullName: this.getFullName(), - failedExpectations: [] + failedExpectations: [], + deprecationWarnings: [] }; } @@ -147,6 +149,10 @@ getJasmineRequireObj().Suite = function(j$) { } }; + Suite.prototype.addDeprecationWarning = function(msg) { + this.result.deprecationWarnings.push(this.expectationResultFactory({ message: msg })); + }; + function isAfterAll(children) { return children && children[0].result.status; }