Skip to content

Commit

Permalink
refactoring(move-to-es6): update code using ES6 standart (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispusDH authored and razvanz committed Jan 29, 2018
1 parent 26bb678 commit b9ba4b3
Show file tree
Hide file tree
Showing 13 changed files with 661 additions and 497 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
screenshots
demo
coverage/
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
'extends': [
'airbnb-base',
'eslint:recommended'
],
'rules': {
'linebreak-style': 0
},
"env": {
"jest": true
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ node_modules

# WebStorm project metadata stuff
.idea/
package-lock.json
coverage/
31 changes: 16 additions & 15 deletions demo/run.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
var miniJasmineLib = require('minijasminenode2'),
TestsReporter = require('../index').Jasmine2Reporter,
options = {
pendingSpec: true,
colors: {
pending: 'yellow',
},
symbols: {
pending: '* '.strikethrough, //strikethrough is a colors module feature
}
};
const Jasmine2Reporter = require('../src/Jasmine2Reporter');
const miniJasmineLib = require('minijasminenode2');

miniJasmineLib.addSpecs('demo/specs.js');
miniJasmineLib.addReporter(new TestsReporter(options));
const options = {
pendingSpec: true,
colors: {
pending: 'yellow',
},
symbols: {
pending: '* '.strikethrough, // strikethrough is a colors module feature
},
};

miniJasmineLib.addSpecs('specs.js');
miniJasmineLib.addReporter(new Jasmine2Reporter(options));
miniJasmineLib.executeSpecs({
silent: true
})
silent: true,
});
26 changes: 13 additions & 13 deletions demo/specs.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
describe('demo suite', function() {
xit('spec skip', function() {});
describe('demo suite', () => {
xit('spec skip', () => {});

it('spec success', function() {
it('spec success', () => {
expect(true).toEqual(true);
});

it('spec fail', function() {
it('spec fail', () => {
expect(true).toEqual(false);
});

xdescribe('skipped suite', function() {
xit('spec skip', function() {});
xdescribe('skipped suite', () => {
xit('spec skip', () => {});

it('spec success', function() {
it('spec success', () => {
expect(true).toEqual(true);
});

it('spec fail', function() {
it('spec fail', () => {
expect(true).toEqual(false);
});
});

describe('inner suite', function() {
it('spec skip', function() {
pending()
describe('inner suite', () => {
it('spec skip', () => {
pending();
});

it('spec success', function() {
it('spec success', () => {
expect(true).toEqual(true);
});

it('spec fail', function() {
it('spec fail', () => {
expect(true).toEqual(false);
});
});
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
module.exports.Jasmine2Reporter = require('./src/Reporter.js').Jasmine2Reporter;
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "Jasmine 2.0 console reporter.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"demo": "node demo/run.js"
"test": "jest",
"demo": "node demo/run.js",
"lint": "eslint ."
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,6 +34,10 @@
"colors": "^1.1.2"
},
"devDependencies": {
"eslint": "^4.13.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"jest": "^22.0.4",
"minijasminenode2": "github:razvanz/minijasminenode#master"
}
}
176 changes: 82 additions & 94 deletions src/Calculator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function (global) {
const colors = require('colors/safe');

'use strict';

var Calculator = function () {
class Calculator {
constructor() {
this.startTime = null;
this.specStartTime = null;
this.runTime = null;
Expand All @@ -15,108 +14,97 @@
this.totalSuites = 0;
this.totalSpecs = 0;
this.suites = {};
};
}

start(specNo) {
this.startTime = (new Date()).getTime();
this.totalSpecs = specNo;
}

stop() {
this.runTime = this.duration((new Date()).getTime() - this.startTime);
colors.strikethrough(this.runTime);
this.executedSpecs = this.failedSpecs + this.passedSpecs;
this.pendingSpecs = this.totalSpecs - this.executedSpecs;
}

Calculator.prototype = {
start: function (specNo) {
this.startTime = (new Date())
.getTime();
this.totalSpecs = specNo;
},
stop: function () {
this.runTime = this.formatDuration((new Date())
.getTime() - this.startTime);
this.executedSpecs = this.failedSpecs + this.passedSpecs;
this.pendingSpecs = this.totalSpecs - this.executedSpecs;
},
startSuite: function (suite) {
startSuite(suite) {
this.suites[suite.id] = {
status: 'exec',
startTime: (new Date()).getTime(),
};
}

stopSuite(suite) {
this.totalSuites++;
if (this.suites[suite.id]) {
this.suites[suite.id].duration = this.duration((new Date()).getTime() - this.suites[suite.id].startTime);
colors.strikethrough(this.suites[suite.id].duration);
this.executedSuites++;
} else {
this.suites[suite.id] = {
status: 'exec',
startTime: (new Date())
.getTime()
status: 'skip',
startTime: (new Date()).getTime(),
duration: this.duration(0),
};
},
stopSuite: function (suite) {
this.totalSuites++;
if (this.suites[suite.id]) {
this.suites[suite.id].duration = this.formatDuration((new Date())
.getTime() - this.suites[suite.id].startTime);
this.executedSuites++;
} else {
this.suites[suite.id] = {
status: 'skip',
startTime: (new Date())
.getTime(),
duration: this.formatDuration(0)
};
}
},
startSpec: function () {
this.specStartTime = (new Date())
.getTime();
},
stopSpec: function (spec) {
this.specTime = this.formatDuration((new Date())
.getTime() - this.specStartTime);
this.countSpecs(spec.status);
},
countSpecs: function (status) {
switch (status) {
}
}

startSpec() {
this.specStartTime = (new Date()).getTime();
}

stopSpec(spec) {
this.specTime = this.duration((new Date()).getTime() - this.specStartTime);
colors.strikethrough(this.specTime);
this.countSpecs(spec.status);
}

countSpecs(status) {
switch (status) {
case 'passed':
this.passedSpecs++;
break;
case 'failed':
this.failedSpecs++;
break;
}
},
formatDuration: function (durationInMs) {
var duration = '',
durationInSecs, durationInMins, durationInHrs;
durationInSecs = durationInMs / 1000;
if (durationInSecs < 1) {
return (durationInSecs + ' s')
.strikethrough;
}
durationInSecs = Math.round(durationInSecs);
if (durationInSecs < 60) {
return (durationInSecs + ' s')
.strikethrough;
}
durationInMins = Math.floor(durationInSecs / 60);
durationInSecs = durationInSecs % 60;
if (durationInSecs) {
duration = ' ' + durationInSecs + ' s';
}
if (durationInMins < 60) {
return (durationInMins + ' min' + duration)
.strikethrough;
}
durationInHrs = Math.floor(durationInMins / 60);
durationInMins = durationInMins % 60;
if (durationInMins) {
duration = ' ' + durationInMins + ' min' + duration;
}
return (durationInHrs + ' hours' + duration)
.strikethrough;
},
formatProcentage: function (unit, total) {
if(total !== 0){
return ' ' + (parseInt(unit * 10000 / total) / 100) + '% ';
} else return '0 %';
}
};
}

function expose() {
if (typeof module !== 'undefined' && module.exports) {
return exports;
} else {
global.reporterHelpers = global.reporterHelpers || {};
return global.reporterHelpers;
duration(durationInMs) {
let duration = '';
let durationInSecs;
let durationInMins;
let durationInHrs;
durationInSecs = durationInMs / 1000;
if (durationInSecs < 1) {
return `${durationInSecs} s`;
}
durationInSecs = Math.round(durationInSecs);
if (durationInSecs < 60) {
return `${durationInSecs} s`;
}
durationInMins = Math.floor(durationInSecs / 60);
durationInSecs %= 60;
if (durationInSecs) {
duration = ` ${durationInSecs} s`;
}
if (durationInMins < 60) {
return `${durationInMins} min${duration}`;
}
durationInHrs = Math.floor(durationInMins / 60);
durationInMins %= 60;
if (durationInMins) {
duration = ` ${durationInMins} min${duration}`;
}
return `${durationInHrs} hours${duration}`;
}

expose()
.Calculator = Calculator;
formatPercentage(unit, total) {
if (total !== 0) {
return ` ${parseInt(unit * 10000 / total) / 100}% `;
} return '0 %';
}
}

})(this);
module.exports = Calculator;
50 changes: 50 additions & 0 deletions src/Jasmine2Reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const Calculator = require('./Calculator');
const TerminalLogger = require('./TerminalLogger');

class Jasmine2Reporter {
constructor(options, cb) {
this.started = false;
this.finished = false;
this.loggerNo = 1;
this.calc = new Calculator();
this.logger = new TerminalLogger(options);
this.jasmineCallback = cb;
}

jasmineStarted(runner) {
this.started = true;
this.calc.start(runner.totalSpecsDefined);
this.logger.start(runner, this.calc);
}

jasmineDone(summary) {
this.calc.stop();
this.logger.stop(this.calc);
this.finished = true;
if (this.jasmineCallback) {
this.jasmineCallback(summary);
}
}

suiteStarted(suite) {
this.calc.startSuite(suite);
this.logger.startSuite(suite, this.calc);
}

suiteDone(suite) {
this.calc.stopSuite(suite);
this.logger.stopSuite(suite, this.calc);
}

specStarted(spec) {
this.calc.startSpec(spec);
this.logger.startSpec(spec);
}

specDone(spec) {
this.calc.stopSpec(spec);
this.logger.stopSpec(spec, this.calc);
}
}

module.exports = Jasmine2Reporter;
Loading

0 comments on commit b9ba4b3

Please sign in to comment.