Skip to content

Commit

Permalink
fix(time): report correct time since Jasmine v2.9.0
Browse files Browse the repository at this point in the history
Time reporting broke since jasmine-core v2.9 because the reporter API
now receives copies of spec results instead of the real deal.
We cannot rely on the same object being passed around.
  • Loading branch information
nicojs committed Mar 9, 2018
1 parent f48a731 commit 3713801
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/adapter.js
Expand Up @@ -161,6 +161,7 @@ function KarmaReporter (tc, jasmineEnv) {
// Save link on native Date object
// because user can mock it
var _Date = Date
var startTimeCurrentSpec = new _Date().getTime();

/**
* @param suite
Expand Down Expand Up @@ -234,7 +235,7 @@ function KarmaReporter (tc, jasmineEnv) {
}

this.specStarted = function (specResult) {
specResult.startTime = new _Date().getTime()
startTimeCurrentSpec = new _Date().getTime()
}

this.specDone = function (specResult) {
Expand All @@ -249,7 +250,7 @@ function KarmaReporter (tc, jasmineEnv) {
pending: specResult.status === 'pending',
success: specResult.failedExpectations.length === 0,
suite: [],
time: skipped ? 0 : new _Date().getTime() - specResult.startTime,
time: skipped ? 0 : new _Date().getTime() - startTimeCurrentSpec,
executedExpectationsCount: specResult.failedExpectations.length + specResult.passedExpectations.length
}

Expand Down
12 changes: 10 additions & 2 deletions test/adapter.spec.js
Expand Up @@ -253,6 +253,14 @@ describe('jasmine adapter', function () {
it('should report time for every spec', function () {
var counter = 3

function copySpecResult() {
var copy = {}
for(var i in spec.result) {
copy[i] = spec.result[i]
}
return copy
}

spyOn(Date.prototype, 'getTime').and.callFake(function () {
counter += 1
return counter
Expand All @@ -262,8 +270,8 @@ describe('jasmine adapter', function () {
expect(result.time).toBe(1) // 4 - 3
})

reporter.specStarted(spec.result)
reporter.specDone(spec.result)
reporter.specStarted(copySpecResult())
reporter.specDone(copySpecResult())

expect(karma.result).toHaveBeenCalled()
})
Expand Down

0 comments on commit 3713801

Please sign in to comment.