Skip to content

Commit

Permalink
Fixed so that timing will always use native Date instance instead of …
Browse files Browse the repository at this point in the history
…mock even for badly designed tests (#240)

The user might mock the Date. Because of this KarmaReporter saved a reference to the native Date and used that reference for measuring duration. The problem was that it only worked when users followed best practises when writing tests. 
If a user wrote a "bad" test where the Date is mocked immediately in the body of a decribe instead of inside an it or beforeEach, the Date was already mocked by the time KarmaReporter was called. By saving the reference at the time adapter.js is executed instead of when KarmaReporter is run, this problem is circumvented.
  • Loading branch information
strille authored and johnjbarton committed Jan 3, 2020
1 parent 9480804 commit 28aa72f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/adapter.js
Expand Up @@ -2,6 +2,10 @@

'use strict'

// Save link to native Date object
// before it might be mocked by the user
var _Date = Date

/**
* Decision maker for whether a stack entry is considered external to jasmine and karma.
* @param {String} entry Error stack entry.
Expand Down Expand Up @@ -142,9 +146,6 @@ 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
var startTimeCurrentSpec = new _Date().getTime()

function handleGlobalErrors (result) {
Expand Down

0 comments on commit 28aa72f

Please sign in to comment.