-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from demirermustafa/master
@elastic/elasticsearch used instead of elasticsearch
- Loading branch information
Showing
8 changed files
with
4,357 additions
and
1,812 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"presets": [ | ||
"env" | ||
] | ||
"@babel/preset-env" | ||
], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
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 |
---|---|---|
|
@@ -57,3 +57,5 @@ typings/ | |
# dotenv environment variables file | ||
.env | ||
|
||
# Vs files | ||
.vs/ |
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
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); | ||
} |
Oops, something went wrong.