forked from allure-framework/allure-mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (65 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
var Base = require("mocha").reporters.Base;
var Allure = require("allure-js-commons");
var allureReporter = new Allure();
var Runtime = require("allure-js-commons/runtime");
global.allure = new Runtime(allureReporter);
/**
* Initialize a new `Allure` test reporter.
*
* @param {Runner} runner
* @param {Object} opts mocha options
* @api public
*/
function AllureReporter(runner, opts) {
Base.call(this, runner);
allureReporter.setOptions(opts.reporterOptions || {});
function invokeHandler(handler) {
return function() {
try {
return handler.apply(this, arguments);
} catch(error) {
console.error("Internal error in Allure:", error); // eslint-disable-line no-console
}
};
}
runner.on("suite", invokeHandler(function (suite) {
allureReporter.startSuite(suite.fullTitle());
}));
runner.on("suite end", invokeHandler(function () {
allureReporter.endSuite();
}));
runner.on("test", invokeHandler(function(test) {
if (typeof test.currentRetry !== "function" || !test.currentRetry()) {
allureReporter.startCase(test.title);
}
}));
runner.on("pending", invokeHandler(function(test) {
var currentTest = allureReporter.getCurrentTest();
if(currentTest && currentTest.name === test.title) {
allureReporter.endCase("skipped");
} else {
allureReporter.pendingCase(test.title);
}
}));
runner.on("pass", invokeHandler(function() {
allureReporter.endCase("passed");
}));
runner.on("fail", invokeHandler(function(test, err) {
if(!allureReporter.getCurrentTest()) {
allureReporter.startCase(test.title);
}
var isAssertionError = err.name === "AssertionError" || err.code === "ERR_ASSERTION";
var status = isAssertionError ? "failed" : "broken";
if(global.onError) {
global.onError(err);
}
allureReporter.endCase(status, err);
}));
runner.on("hook end", invokeHandler(function(hook) {
if(hook.title.indexOf('"after each" hook') === 0) {
allureReporter.endCase("passed");
}
}));
}
module.exports = AllureReporter;