Skip to content

Commit

Permalink
Merge pull request #3 from demirermustafa/master
Browse files Browse the repository at this point in the history
@elastic/elasticsearch used instead of elasticsearch
  • Loading branch information
ocinbat committed Jan 28, 2020
2 parents 7addb8e + c5b49c1 commit 811f1d6
Show file tree
Hide file tree
Showing 8 changed files with 4,357 additions and 1,812 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"presets": [
"env"
]
"@babel/preset-env"
],
"plugins": ["@babel/plugin-transform-runtime"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# Vs files
.vs/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Ensure that your elasticsearch installation is up and running.
Run mocha with `mochalastic`:

```shell
$ mocha test --reporter mochalastic --reporter-options host=localhost,username=elastic,password=changeme,port=9200,project=some_project,suite=some_suite
$ mocha test --reporter mochalastic --reporter-options nodeUris=https://localhost:9200,username=elastic,password=changeme,project=some_project,suite=some_suite, indexPrefix=test-results
```

or use a mocha.options file
```shell
mocha --opts mochalastic.opts build/test
--recursive
--reporter mochalastic
--reporter-options host=localhost,username=elastic,password=changeme,port=9200,project=some_project,suite=some_suite
--reporter-options nodeUris=https://localhost:9200,username=elastic,password=changeme,project=some_project,suite=some_suite, indexPrefix=test-results
--no-exit
```

Expand Down
286 changes: 165 additions & 121 deletions lib/mochalastic.js
Original file line number Diff line number Diff line change
@@ -1,128 +1,172 @@
'use strict';

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var mocha = require('mocha');
var elasticsearch = require('elasticsearch');

var _require = require('@elastic/elasticsearch'),
Client = _require.Client;

var dateFormat = require('date-format');

module.exports = mochalastic;

function mochalastic(runner, options) {
mocha.reporters.Base.call(this, runner);
var reporterOptions = options.reporterOptions;
var currentDate = new Date().toISOString();

validate(reporterOptions, 'host');
validate(reporterOptions, 'port');
validate(reporterOptions, 'protocol');
validate(reporterOptions, 'project');
validate(reporterOptions, 'suite');

var self = this;
var tests = [];

var elasticConfig = {
host: [{
host: reporterOptions.host,
protocol: reporterOptions.protocol,
port: reporterOptions.port
}],
log: 'error'
};

if (reporterOptions.username && reporterOptions.password) {
elasticConfig.host[0].auth = reporterOptions.username + ':' + reporterOptions.password;
}

var client = new elasticsearch.Client(elasticConfig);

var log = function log(testData) {
client.create({
index: 'test-data-index', // Get from config
type: 'mocha-test-result',
id: testData.id,
timestamp: testData.time,
body: testData
}, function (error, response) {
console.log(error);
});
};

runner.on('test end', function (test) {
tests.push(test);
});

runner.on('end', function () {
tests.map(clean).forEach(function (test) {
log(test);
}, this);
});

/**
* Transform `error` into a JSON object.
*
* @api private
* @param {Error} err
* @return {Object}
*/
function errorJSON(err) {
var res = {};
Object.getOwnPropertyNames(err).forEach(function (key) {
res[key] = err[key];
}, err);
return res;
}

/**
* Return a plain-object representation of `test`
* free of cyclic properties etc.
*
* @api private
* @param {Object} test
* @return {Object}
*/
function clean(test) {
return {
id: guid(),
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
currentRetry: test.currentRetry(),
project: reporterOptions.project,
suite: reporterOptions.suite,
time: currentDate,
err: errorJSON(test.err || {})
};
}

/**
* Checks if (obj) is empty.
*
* @api private
* @param {Object} obj
* @return {Object}
*/
function isEmpty(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) return false;
}

return true;
}

function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}

function validate(options, name) {
if (options == null) {
throw new Error("Missing --reporter-options in mocha.opts");
}

if (options[name] == null) {
throw new Error('Missing ' + name + ' value. Please update --reporter-options in mocha.opts');
}
}
function mochalastic(_x, _x2) {
return _mochalastic.apply(this, arguments);
}

function _mochalastic() {
_mochalastic = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee3(runner, options) {
var reporterOptions, currentDate, testResults, client, logResultsToIndex, _logResultsToIndex, errorJSON, clean, isEmpty, guid, s4, validate;

return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
validate = function _ref9(options, name) {
if (options == null) {
throw new Error("Missing --reporter-options in mocha.opts");
}

if (options[name] == null) {
throw new Error("Missing ".concat(name, " value. Please update --reporter-options in mocha.opts"));
}
};

s4 = function _ref8() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
};

guid = function _ref7() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};

isEmpty = function _ref6(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) return false;
}

return true;
};

clean = function _ref5(test) {
return {
id: guid(),
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
currentRetry: test.currentRetry(),
project: reporterOptions.project,
suite: reporterOptions.suite,
time: currentDate,
err: errorJSON(test.err || {})
};
};

errorJSON = function _ref4(err) {
var res = {};
Object.getOwnPropertyNames(err).forEach(function (key) {
res[key] = err[key];
}, err);
return res;
};

_logResultsToIndex = function _ref3() {
_logResultsToIndex = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee2(testResult) {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return client.index({
index: reporterOptions.indexPrefix + '-' + dateFormat('yyyy.MM.dd', new Date()),
body: testResult
})["catch"](console.log);

case 2:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _logResultsToIndex.apply(this, arguments);
};

logResultsToIndex = function _ref2(_x3) {
return _logResultsToIndex.apply(this, arguments);
};

mocha.reporters.Base.call(this, runner);
reporterOptions = options.reporterOptions;
currentDate = new Date().toISOString();
validate(reporterOptions, 'nodeUris');
validate(reporterOptions, 'username');
validate(reporterOptions, 'password');
validate(reporterOptions, 'indexPrefix');
validate(reporterOptions, 'project');
validate(reporterOptions, 'suite');
testResults = [];
client = new Client({
node: reporterOptions.nodeUris,
auth: {
username: reporterOptions.username,
password: reporterOptions.password
}
});
;
runner.on('test end', function (test) {
testResults.push(test);
});
runner.on('end', function () {
testResults.map(clean).forEach(
/*#__PURE__*/
function () {
var _ref = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee(test) {
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return logResultsToIndex(test)["catch"](console.log);

case 2:
case "end":
return _context.stop();
}
}
}, _callee);
}));

return function (_x4) {
return _ref.apply(this, arguments);
};
}(), this);
});
/**
* Transform `error` into a JSON object.
*
* @api private
* @param {Error} err
* @return {Object}
*/

case 22:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
return _mochalastic.apply(this, arguments);
}
Loading

0 comments on commit 811f1d6

Please sign in to comment.