From 93452bb22fcb3a82f5b1e04865d608373aa42bc4 Mon Sep 17 00:00:00 2001 From: Maksim Ryzhikov Date: Mon, 28 Nov 2016 19:08:00 +0300 Subject: [PATCH] fix: Karma reported test duration is negative fix #143 --- src/adapter.js | 8 ++++++-- test/adapter.spec.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/adapter.js b/src/adapter.js index 04e3181..87163d2 100644 --- a/src/adapter.js +++ b/src/adapter.js @@ -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 @@ -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) { @@ -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 } diff --git a/test/adapter.spec.js b/test/adapter.spec.js index 2f4ad00..b7d192c 100644 --- a/test/adapter.spec.js +++ b/test/adapter.spec.js @@ -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 () {