Skip to content

Commit

Permalink
Merge branch 'johnjbarton-duration'
Browse files Browse the repository at this point in the history
- Merges #1660 from @johnjbarton
- Fixes #1646
  • Loading branch information
Gregg Van Hove committed Mar 15, 2019
2 parents 7c0f013 + a8c2399 commit f5663a9
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
28 changes: 23 additions & 5 deletions lib/jasmine-core/jasmine.js
Expand Up @@ -557,6 +557,7 @@ getJasmineRequireObj().Spec = function(j$) {
this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
this.catchingExceptions = attrs.catchingExceptions || function() { return true; };
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
this.timer = attrs.timer || j$.noopTimer;

if (!this.queueableFn.fn) {
this.pend();
Expand All @@ -572,6 +573,7 @@ getJasmineRequireObj().Spec = function(j$) {
* @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.
* @property {number} duration - The time in ms used by the spec execution, including any before/afterEach.
*/
this.result = {
id: this.id,
Expand All @@ -580,7 +582,8 @@ getJasmineRequireObj().Spec = function(j$) {
failedExpectations: [],
passedExpectations: [],
deprecationWarnings: [],
pendingReason: ''
pendingReason: '',
duration: null,
};
}

Expand Down Expand Up @@ -610,6 +613,7 @@ getJasmineRequireObj().Spec = function(j$) {

var onStart = {
fn: function(done) {
self.timer.start();
self.onStart(self, done);
}
};
Expand All @@ -633,6 +637,7 @@ getJasmineRequireObj().Spec = function(j$) {
self.onException.apply(self, arguments);
},
onComplete: function() {
self.result.duration = self.timer.elapsed();
onComplete(self.result.status === 'failed' && new j$.StopExecutionError('spec failed'));
},
userContext: this.userContext()
Expand Down Expand Up @@ -1290,6 +1295,7 @@ getJasmineRequireObj().Env = function(j$) {
currentlyExecutingSuites.push(suite);
defaultResourcesForRunnable(suite.id, suite.parentSuite.id);
reporter.suiteStarted(suite.result, next);
suite.startTimer();
},
nodeComplete: function(suite, result, next) {
if (suite !== currentSuite()) {
Expand All @@ -1302,7 +1308,7 @@ getJasmineRequireObj().Env = function(j$) {
if (result.status === 'failed') {
hasFailures = true;
}

suite.endTimer();
reporter.suiteDone(result, next);
},
orderChildren: function(node) {
Expand Down Expand Up @@ -1582,9 +1588,9 @@ getJasmineRequireObj().Env = function(j$) {
fn: fn,
timeout: timeout || 0
},
throwOnExpectationFailure: config.oneFailurePerSpec
throwOnExpectationFailure: config.oneFailurePerSpec,
timer: new j$.Timer(),
});

return spec;

function specResultCallback(result, next) {
Expand Down Expand Up @@ -6582,6 +6588,8 @@ getJasmineRequireObj().Suite = function(j$) {
this.beforeAllFns = [];
this.afterAllFns = [];

this.timer = attrs.timer || j$.noopTimer;

this.children = [];

/**
Expand All @@ -6592,13 +6600,15 @@ getJasmineRequireObj().Suite = function(j$) {
* @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.
* @property {number} duration - The time in ms for Suite execution, including any before/afterAll, before/afterEach.
*/
this.result = {
id: this.id,
description: this.description,
fullName: this.getFullName(),
failedExpectations: [],
deprecationWarnings: []
deprecationWarnings: [],
duration: null,
};
}

Expand Down Expand Up @@ -6640,6 +6650,14 @@ getJasmineRequireObj().Suite = function(j$) {
this.afterAllFns.unshift(fn);
};

Suite.prototype.startTimer = function() {
this.timer.start();
};

Suite.prototype.endTimer = function() {
this.result.duration = this.timer.elapsed();
};

function removeFns(queueableFns) {
for(var i = 0; i < queueableFns.length; i++) {
queueableFns[i].fn = null;
Expand Down
19 changes: 18 additions & 1 deletion spec/core/SpecSpec.js
Expand Up @@ -207,7 +207,8 @@ describe("Spec", function() {
failedExpectations: [],
passedExpectations: [],
deprecationWarnings: [],
pendingReason: ''
pendingReason: '',
duration: null,
}, 'things');
});

Expand Down Expand Up @@ -242,6 +243,22 @@ describe("Spec", function() {
expect(done).toHaveBeenCalledWith(jasmine.any(jasmineUnderTest.StopExecutionError));
});

it("should report the duration of the test", function() {
var done = jasmine.createSpy('done callback'),
timer = jasmine.createSpyObj('timer', {'start': null, elapsed: 77000}),
spec = new jasmineUnderTest.Spec({
queueableFn: { fn: jasmine.createSpy("spec body")},
catchExceptions: function() { return false; },
resultCallback: function() {},
queueRunnerFactory: function(attrs) {
attrs.onComplete();
},
timer: timer,
});
spec.execute(done);
expect(spec.result.duration).toBe(77000);
});

it("#status returns passing by default", function() {
var spec = new jasmineUnderTest.Spec({queueableFn: { fn: jasmine.createSpy("spec body")} });
expect(spec.status()).toBe('passed');
Expand Down
13 changes: 13 additions & 0 deletions spec/core/SuiteSpec.js
Expand Up @@ -111,6 +111,19 @@ describe("Suite", function() {
expect(suite.getResult().failedExpectations).toEqual([]);
});

it("calls timer to compute duration", function(){
var env = new jasmineUnderTest.Env(),
suite = new jasmineUnderTest.Suite({
env: env,
id: 456,
description: "I am a suite",
timer: jasmine.createSpyObj('timer', {'start': null, elapsed: 77000}),
});
suite.startTimer();
suite.endTimer();
expect(suite.getResult().duration).toEqual(77000);
});

describe('#sharedUserContext', function() {
beforeEach(function() {
this.suite = new jasmineUnderTest.Suite({});
Expand Down
3 changes: 3 additions & 0 deletions spec/core/integration/EnvSpec.js
Expand Up @@ -1436,6 +1436,9 @@ describe("Env integration", function() {
status: 'pending'
}));

var suiteDone = reporter.suiteDone.calls.argsFor(0)[0];
expect(typeof suiteDone.duration).toBe('number');

var suiteResult = reporter.suiteStarted.calls.argsFor(0)[0];
expect(suiteResult.description).toEqual("A Suite");

Expand Down
7 changes: 4 additions & 3 deletions src/core/Env.js
Expand Up @@ -512,6 +512,7 @@ getJasmineRequireObj().Env = function(j$) {
currentlyExecutingSuites.push(suite);
defaultResourcesForRunnable(suite.id, suite.parentSuite.id);
reporter.suiteStarted(suite.result, next);
suite.startTimer();
},
nodeComplete: function(suite, result, next) {
if (suite !== currentSuite()) {
Expand All @@ -524,7 +525,7 @@ getJasmineRequireObj().Env = function(j$) {
if (result.status === 'failed') {
hasFailures = true;
}

suite.endTimer();
reporter.suiteDone(result, next);
},
orderChildren: function(node) {
Expand Down Expand Up @@ -804,9 +805,9 @@ getJasmineRequireObj().Env = function(j$) {
fn: fn,
timeout: timeout || 0
},
throwOnExpectationFailure: config.oneFailurePerSpec
throwOnExpectationFailure: config.oneFailurePerSpec,
timer: new j$.Timer(),
});

return spec;

function specResultCallback(result, next) {
Expand Down
7 changes: 6 additions & 1 deletion src/core/Spec.js
Expand Up @@ -14,6 +14,7 @@ getJasmineRequireObj().Spec = function(j$) {
this.queueRunnerFactory = attrs.queueRunnerFactory || function() {};
this.catchingExceptions = attrs.catchingExceptions || function() { return true; };
this.throwOnExpectationFailure = !!attrs.throwOnExpectationFailure;
this.timer = attrs.timer || j$.noopTimer;

if (!this.queueableFn.fn) {
this.pend();
Expand All @@ -29,6 +30,7 @@ getJasmineRequireObj().Spec = function(j$) {
* @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.
* @property {number} duration - The time in ms used by the spec execution, including any before/afterEach.
*/
this.result = {
id: this.id,
Expand All @@ -37,7 +39,8 @@ getJasmineRequireObj().Spec = function(j$) {
failedExpectations: [],
passedExpectations: [],
deprecationWarnings: [],
pendingReason: ''
pendingReason: '',
duration: null,
};
}

Expand Down Expand Up @@ -67,6 +70,7 @@ getJasmineRequireObj().Spec = function(j$) {

var onStart = {
fn: function(done) {
self.timer.start();
self.onStart(self, done);
}
};
Expand All @@ -90,6 +94,7 @@ getJasmineRequireObj().Spec = function(j$) {
self.onException.apply(self, arguments);
},
onComplete: function() {
self.result.duration = self.timer.elapsed();
onComplete(self.result.status === 'failed' && new j$.StopExecutionError('spec failed'));
},
userContext: this.userContext()
Expand Down
14 changes: 13 additions & 1 deletion src/core/Suite.js
Expand Up @@ -14,6 +14,8 @@ getJasmineRequireObj().Suite = function(j$) {
this.beforeAllFns = [];
this.afterAllFns = [];

this.timer = attrs.timer || j$.noopTimer;

this.children = [];

/**
Expand All @@ -24,13 +26,15 @@ getJasmineRequireObj().Suite = function(j$) {
* @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.
* @property {number} duration - The time in ms for Suite execution, including any before/afterAll, before/afterEach.
*/
this.result = {
id: this.id,
description: this.description,
fullName: this.getFullName(),
failedExpectations: [],
deprecationWarnings: []
deprecationWarnings: [],
duration: null,
};
}

Expand Down Expand Up @@ -72,6 +76,14 @@ getJasmineRequireObj().Suite = function(j$) {
this.afterAllFns.unshift(fn);
};

Suite.prototype.startTimer = function() {
this.timer.start();
};

Suite.prototype.endTimer = function() {
this.result.duration = this.timer.elapsed();
};

function removeFns(queueableFns) {
for(var i = 0; i < queueableFns.length; i++) {
queueableFns[i].fn = null;
Expand Down

0 comments on commit f5663a9

Please sign in to comment.