This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use the same 'test-counter' in reporters and in 'stats'
- Loading branch information
Showing
8 changed files
with
209 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,28 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const RunnerEvents = require('./constants/events'); | ||
const TestCounter = require('./test-counter'); | ||
|
||
const Events = require('./constants/events'); | ||
module.exports = class Stats { | ||
constructor(runner) { | ||
this._counter = new TestCounter(); | ||
|
||
/** | ||
* @constructor | ||
* @param {Runner} [runner] | ||
*/ | ||
module.exports = function(runner) { | ||
const data = {}; | ||
|
||
const add = (key) => { | ||
data[key] = data[key] || 0; | ||
data[key]++; | ||
}; | ||
|
||
if (runner) { | ||
runner | ||
.on(Events.BEGIN_STATE, _.partial(add, 'total')) | ||
.on(Events.SKIP_STATE, () => { | ||
add('total'); | ||
add('skipped'); | ||
.on(RunnerEvents.SKIP_STATE, (test) => this._counter.onSkipped(test)) | ||
.on(RunnerEvents.WARNING, (test) => this._counter.onWarned(test)) | ||
.on(RunnerEvents.ERROR, (test) => this._counter.onErrored(test)) | ||
.on(RunnerEvents.UPDATE_RESULT, (test) => { | ||
return test.updated ? this._counter.onUpdated(test) : this._counter.onPassed(test); | ||
}) | ||
.on(Events.WARNING, _.partial(add, 'warned')) | ||
.on(Events.ERROR, _.partial(add, 'errored')) | ||
|
||
.on(Events.UPDATE_RESULT, (res) => { | ||
add(res && res.updated ? 'updated' : 'passed'); | ||
.on(RunnerEvents.TEST_RESULT, (test) => { | ||
return test.equal ? this._counter.onPassed(test) : this._counter.onFailed(test); | ||
}) | ||
.on(Events.TEST_RESULT, (res) => { | ||
add(res && res.equal ? 'passed' : 'failed'); | ||
}); | ||
.on(RunnerEvents.RETRY, (test) => this._counter.onRetry(test)); | ||
} | ||
|
||
this.add = add; | ||
get(type) { | ||
const stats = this._counter.getResult(); | ||
|
||
this.get = (name) => name === undefined ? data : data[name]; | ||
return type === undefined ? stats : stats[type]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
const STATS = { | ||
total: 'total', | ||
updated: 'updated', | ||
passed: 'passed', | ||
failed: 'failed', | ||
errored: 'errored', | ||
skipped: 'skipped', | ||
warned: 'warned', | ||
retries: 'retries' | ||
}; | ||
|
||
module.exports = class TestCounter { | ||
constructor() { | ||
this._stats = {}; | ||
} | ||
|
||
onUpdated(updated) { | ||
this._addStat(STATS.updated, updated); | ||
} | ||
|
||
onPassed(passed) { | ||
this._addStat(STATS.passed, passed); | ||
} | ||
|
||
onFailed(failed) { | ||
this._addStat(STATS.failed, failed); | ||
} | ||
|
||
onErrored(errored) { | ||
this._addStat(STATS.errored, errored); | ||
} | ||
|
||
onSkipped(skipped) { | ||
this._addStat(STATS.skipped, skipped); | ||
} | ||
|
||
onWarned(warned) { | ||
this._addStat(STATS.warned, warned); | ||
} | ||
|
||
onRetry(retried) { | ||
const suiteStats = this._getSuiteStats(retried); | ||
|
||
suiteStats.retries++; | ||
} | ||
|
||
_addStat(stat, test) { | ||
const suiteStats = this._getSuiteStats(test); | ||
|
||
suiteStats.states[test.state.name] = stat; | ||
} | ||
|
||
_getSuiteStats(test) { | ||
const key = this._buildSuiteKey(test); | ||
|
||
if (!this._stats[key]) { | ||
this._stats[key] = { | ||
retries: 0, | ||
states: {} | ||
}; | ||
} | ||
|
||
return this._stats[key]; | ||
} | ||
|
||
_buildSuiteKey(test) { | ||
return test.suite.fullName + test.browserId; | ||
} | ||
|
||
getResult() { | ||
const statNames = _.keys(STATS); | ||
const result = _.zipObject(statNames, _.fill(Array(statNames.length), 0)); | ||
|
||
_.forEach(this._stats, (suiteStats) => { | ||
result.retries += suiteStats.retries; | ||
_.forEach(suiteStats.states, (stateStatus) => { | ||
result.total++; | ||
result[stateStatus]++; | ||
}); | ||
}); | ||
|
||
return result; | ||
} | ||
}; |
Oops, something went wrong.