Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
feat(stats): move part of the logic to the gemini-core
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Feb 4, 2018
1 parent 707abee commit 99e7e0c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 54 deletions.
72 changes: 20 additions & 52 deletions lib/stats.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,40 @@
'use strict';

const _ = require('lodash');
const {BaseStats} = require('gemini-core');
const RunnerEvents = require('./constants/events');

const STATS = {
total: 'total',
updated: 'updated',
passed: 'passed',
failed: 'failed',
skipped: 'skipped',
retries: 'retries'
const statNames = {
TOTAL: 'total',
UPDATED: 'updated',
PASSED: 'passed',
FAILED: 'failed',
SKIPPED: 'skipped',
RETRIES: 'retries'
};

module.exports = class Stats {
static create() {
return new Stats();
module.exports = class Stats extends BaseStats {
constructor() {
super(statNames);
}

constructor() {
this._stats = {};
addUpdated(test) {
return this._addStat(this._statNames.UPDATED, test);
}

attachRunner(runner) {
runner
.on(RunnerEvents.SKIP_STATE, (test) => this._addStat(STATS.skipped, test))
.on(RunnerEvents.ERROR, (test) => this._addStat(STATS.failed, test))
.on(RunnerEvents.UPDATE_RESULT, (test) => {
return test.updated ? this._addStat(STATS.updated, test) : this._addStat(STATS.passed, test);
})
.on(RunnerEvents.TEST_RESULT, (test) => {
return test.equal ? this._addStat(STATS.passed, test) : this._addStat(STATS.failed, test);
})
.on(RunnerEvents.RETRY, (test) => this._getSuiteStats(test).retries++);
.on(RunnerEvents.SKIP_STATE, (test) => this.addSkipped(test))
.on(RunnerEvents.ERROR, (test) => this.addFailed(test))
.on(RunnerEvents.UPDATE_RESULT, (test) => test.updated ? this.addUpdated(test) : this.addPassed(test))
.on(RunnerEvents.TEST_RESULT, (test) => test.equal ? this.addPassed(test) : this.addFailed(test))
.on(RunnerEvents.RETRY, () => this.addRetries());
}

_addStat(stat, test) {
this._getSuiteStats(test).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];
_buildStateKey(test) {
return test.state.name;
}

_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;
}
};
4 changes: 2 additions & 2 deletions test/unit/stats.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const EventEmitter = require('events').EventEmitter;
const {EventEmitter} = require('events');
const RunnerEvents = require('../../lib/constants/events');
const Stats = require('../../lib/stats');
const makeStateResult = require('../util').makeStateResult;
const {makeStateResult} = require('../util');

describe('Stats', () => {
let stats;
Expand Down

0 comments on commit 99e7e0c

Please sign in to comment.