Skip to content

Instantly share code, notes, and snippets.

@saikrishna321
Created May 30, 2022 05:28
Show Gist options
  • Save saikrishna321/dfd845f2982a833282e055e07ce25210 to your computer and use it in GitHub Desktop.
Save saikrishna321/dfd845f2982a833282e055e07ce25210 to your computer and use it in GitHub Desktop.
'use strict';
const Mocha = require('mocha');
const shelljs = require('shelljs');
const chalk = require('chalk');
const {
EVENT_TEST_BEGIN,
EVENT_RUN_END,
EVENT_TEST_FAIL,
EVENT_TEST_PASS,
EVENT_SUITE_BEGIN,
EVENT_SUITE_END,
EVENT_HOOK_END,
} = Mocha.Runner.constants;
// this reporter outputs test results, indenting two spaces per suite
var passes = 0;
var failures = 0;
const fs = require('fs');
const path = require('path');
const util = require('util');
const { info } = require('@reportportal/agent-js-mocha/lib/publicReportingAPI');
const pass = `✅`;
const error = `❌`;
const running = `🏃‍♂️`;
const screenShot = path.join(__dirname, '../screenshot');
class MyReporter {
constructor(runner) {
this._indents = 0;
const stats = runner.stats;
runner
.once(EVENT_TEST_BEGIN, (test) => {
console.log(`${this.indent()} ${chalk.yellow.bold(`${running} `)} ${test.title}`);
})
.on(EVENT_SUITE_BEGIN, (suite) => {
this.increaseIndent();
console.log(`${this.indent()} ${chalk.cyanBright.bold.italic(suite.title)}`);
})
.on(EVENT_SUITE_END, () => {
this.decreaseIndent();
})
.on(EVENT_HOOK_END, (test) => {
if (test.originalTitle === '"after each" hook: afterEach') {
console.log('In Event Hook End');
info('Test In Event Hook End');
// const currentTestTitle = test.ctx.currentTest.title;
// if (true) {
// console.log(path.resolve(__dirname, `../screenshot/${currentTestTitle.replace(/ /g, '_')}.png`))
// const data = fs.readFileSync(path.resolve(__dirname, `../screenshot/${currentTestTitle.replace(/ /g, '_')}.png`));
// const attachment = {
// name: `${currentTestTitle.replace(/ /g, '_')}.png`,
// type: 'image/png',
// content: data.toString('base64'),
// };
// info('Info log message for test "test" with attachment');
// }
}
})
.on(EVENT_TEST_PASS, (test) => {
if (test.type !== 'hook') {
passes++;
console.log(`${this.indent()} ${chalk.green.bold(`${pass} `)} ${test.fullTitle()}`);
}
})
.on(EVENT_TEST_FAIL, (test, err) => {
if (test.type !== 'hook') {
failures++;
info('Test Failed');
console.log(
`${this.indent()} ${chalk.red.bold(`${error} `)} ${test.fullTitle()} ${chalk.red.bold(err.stack)} \n`,
);
} else {
failures++;
console.log(`${this.indent()} ${chalk.red.bold(`${error} `)} ${test.title} ${chalk.red.bold(err.stack)} \n`);
console.log(chalk.red.bold(err.stack));
}
})
.once(EVENT_RUN_END, () => {
const pushNotificationPath = path.join(__dirname, '../../../');
let summary = [];
summary.push(`⏳ Total Duration in seconds: ${stats.duration}`);
summary.push(`📦 Total Number of Tests: ${passes + failures + stats.pending}`);
summary.push(`✅ Passed: ${passes}`);
summary.push(`❌ Failed: ${failures}`);
summary.push(`👷 Skipped: ${stats.pending}`);
if (stats.failures > 0) {
shelljs.exec(
`${pushNotificationPath}push_notifications.sh --test_summary="${summary.toString().replace(/,/g, '\n')}"`,
);
}
console.log(summary);
});
}
indent() {
return Array(this._indents).join(' ');
}
increaseIndent() {
this._indents++;
}
decreaseIndent() {
this._indents--;
}
}
module.exports = MyReporter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment