Skip to content

Commit

Permalink
fix: Karma reported test duration is negative fix #143
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimr committed Nov 29, 2016
1 parent ecd289e commit 93452bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ function getAllSpecNames (topSuite) {
function KarmaReporter (tc, jasmineEnv) {
var currentSuite = new SuiteNode()

// Save link on native Date object
// because user can mock it
var _Date = Date

/**
* @param suite
* @returns {boolean} Return true if it is system jasmine top level suite
Expand Down Expand Up @@ -208,7 +212,7 @@ function KarmaReporter (tc, jasmineEnv) {
}

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

this.specDone = function (specResult) {
Expand All @@ -223,7 +227,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() - specResult.startTime,
executedExpectationsCount: specResult.failedExpectations.length + specResult.passedExpectations.length
}

Expand Down
17 changes: 17 additions & 0 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,23 @@ describe('jasmine adapter', function () {

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

describe('time', function () {
afterEach(function () {
jasmine.clock().uninstall()
})

it('should report correct time if user mock Date object', function () {
karma.result.and.callFake(function (result) {
expect(result.time >= 0).toBe(true)
})

reporter.specStarted(spec.result)

jasmine.clock().mockDate(new Date(0))
reporter.specDone(spec.result)
})
})
})

describe('formatFailedStep', function () {
Expand Down

0 comments on commit 93452bb

Please sign in to comment.