Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the help option the exit code #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
node_modules
cypress/screenshots
cypress/videos
allure-report
allure-results
.env
#Ignore everything
*

/node_modules/

!/src
!/src/**
!/.npmignore
!/.gitignore
!/.LICENSE
!/package.json
!/package-lock.json
!/README.md
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-discord-reporter",
"version": "1.1.0",
"version": "1.1.1",
"description": "A Discord reporter for allure reports generated by cypress",
"main": "src/index.js",
"bin": {
Expand Down
56 changes: 28 additions & 28 deletions src/compileResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,71 @@ const { Status } = require('./status.js')
const { Color } = require('./colors.js')

async function compileResults(results){
let appName = ''
let appVersion = ''
let title = ''
let color = Color.BLACK
let image
let text = ''
let totalTests = 0
let passedTests = []
,failedTests = []
,brokenTests = []
,skippedTests = [];
let testStats={
totalTests : 0,
passedTests : [],
failedTests : [],
brokenTests : [],
skippedTests : []
}

if (!results) {
console.warn('Report is empty')
title = 'Test report is empty'
color = Color.BLACK
image = 'https://raw.githubusercontent.com/piopi/cypress-discord-reporter/main/assets/images/error.png'
text = 'An error has occured and the test report is empty.'

return { appName, appVersion, color, title, image, text
}
return { testStats, color, title, image, text }
}

results['runs'].forEach(element => {

const testsResults= element.tests;
totalTests += testsResults.length
passedTests=passedTests.concat(testsResults.filter(
testStats.totalTests += testsResults.length
testStats.passedTests=testStats.passedTests.concat(testsResults.filter(
(testRes) => testRes.state === Status.PASSED
));
failedTests=failedTests.concat(testsResults.filter(
testStats.failedTests=testStats.failedTests.concat(testsResults.filter(
(testRes) => testRes.state === Status.FAILED
));
brokenTests=brokenTests.concat(testsResults.filter(
testStats.brokenTests=testStats.brokenTests.concat(testsResults.filter(
(testRes) => testRes.state === Status.BROKEN
));
skippedTests=skippedTests.concat(testsResults.filter(
testStats.skippedTests=testStats.skippedTests.concat(testsResults.filter(
(testRes) => testRes.state === Status.SKIPPED
))

})

if (failedTests.length > 0) {
title = `${failedTests.length} test case(s) failed`
if (testStats.failedTests.length > 0) {
title = `${testStats.failedTests.length} test case(s) failed`
color = Color.RED
image = 'https://raw.githubusercontent.com/maritome/cypress-msteams-reporter/main/assets/images/failed.png'
text = `**Failed test case(s):** \n ${failedTests
text = `**Failed test case(s):** \n ${testStats.failedTests
.map((failedTest) => failedTest.title)
.join('\n')} \n\n **Number of test cases that passed: **${passedTests.length}\n`
} else if (brokenTests.length > 0) {
.join('\n')} \n\n **Number of test cases that passed: **${testStats.passedTests.length}\n`
} else if (testStats.brokenTests.length > 0) {
title =
brokenTests.length === 1
? `${brokenTests.length} test case is broken`
: `${brokenTests.length} test cases are broken`
testStats.brokenTests.length === 1
? `${testStats.brokenTests.length} test case is broken`
: `${testStats.brokenTests.length} test cases are broken`
color = Color.YELLOW
image = 'https://raw.githubusercontent.com/piopi/cypress-discord-reporter/main/assets/images/failed.png'
text = `Broken test case(s): \n ${brokenTests
text = `Broken test case(s): \n ${testStats.brokenTests
.map((brokenTest) => brokenTest.title)
.join('\n')} \n\n **Number of test cases that passed: **${passedTests.length}\n`
.join('\n')} \n\n **Number of test cases that passed: **${testStats.passedTests.length}\n`
} else if (
passedTests.length > 0 &&
passedTests.length === totalTests - skippedTests.length
testStats.passedTests.length > 0 &&
testStats.passedTests.length === testStats.totalTests - testStats.skippedTests.length
) {
title = 'All test cases passed'
color = Color.GREEN
text = `**Number of test cases that passed: **${passedTests.length}\n`
text = `**Number of test cases that passed: **${testStats.passedTests.length}\n`
image = 'https://raw.githubusercontent.com/piopi/cypress-discord-reporter/main/assets/images/passed.png'
} else {
title = 'Unknown Status'
Expand All @@ -76,7 +76,7 @@ async function compileResults(results){
'Some of the tests have unknown status or the test results are missing.'
}

return { appName, appVersion, color, title, image, text }
return { testStats, color, title, image, text, }

}
module.exports = { compileResults }
11 changes: 9 additions & 2 deletions src/discordWebhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ async function sendWebhook(args) {
}
else{

throw new Error('Failed to send Discord message. Please enter the the Discord webhook url either with the argument --discordHook or in the .env file');
throw new Error('Failed to send Discord message. Please enter the Discord webhook url either with the argument --discordHook or in the .env file');

}
let appName="";
if (args.appName){
appName=args.appName;
}else if(process.env.APP_NAME){
appName=process.env.APP_NAME;
}
const embed = new MessageBuilder();

await embed.setTitle(args.title)
Expand All @@ -24,13 +30,14 @@ async function sendWebhook(args) {
if(args.url){
await embed.addField('Report URL:', args.url);
}
if(args.appName){
if(appName){
await embed.setDescription(`**Application:** ${args.appName} \n\n`+args.text);
}else{
await embed.setDescription(args.text);
}


hook.send(embed);
process.exitCode = args.testStats.failedTests.length;
}
module.exports = { sendWebhook }
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ const cypress = require('cypress')
const { compileResults } = require('./compileResults')
const { sendWebhook } = require('./discordWebhook')
let url,appName,webHook;
let sendOnFail=false;
let cypressArg=processCliArg(process.argv.slice(2));

function processCliArg(argv){
let filteredArg=argv;
if(argv.indexOf("--reportUrl")>-1 || argv.indexOf("--appName")>-1 || argv.indexOf("--discordHook")>-1){
if(argv.indexOf("--help")>-1 || argv.indexOf("-h")>-1 || argv.indexOf("-help")>-1){
console.log("Usage: index.js [options] \n\nOptions:\n --reportUrl provide the link for the Report (default: '')\n --appName define the App Name for the report(default: '')\n --discordHook provide the link for the discord hook\n --onFail send report only when it contains failures\n -h,--help display help for command");
process.exit(0);
}
if(argv.indexOf("--reportUrl")>-1 || argv.indexOf("--appName")>-1 || argv.indexOf("--discordHook")>-1 || argv.indexOf("--onFail")>-1){
url=argv.indexOf("--reportUrl")>-1 ? argv[argv.indexOf("--reportUrl")+1]:"";
appName= argv.indexOf("--appName")>-1 ? argv[argv.indexOf("--appName")+1]:"";
webHook= argv.indexOf("--discordHook")>-1 ? argv[argv.indexOf("--discordHook")+1]:"";
sendOnFail= argv.indexOf("--onFail")>-1;
filteredArg= argv.filter( value => {
return value != url && value != '--reportUrl' && value != appName && value != webHook && value !="--discordHook" && value != "--appName";
return value != url && value != '--reportUrl' && value != appName && value != webHook && value !="--discordHook" && value != "--appName" && value !="--onFail";
});
}

Expand All @@ -40,8 +47,14 @@ function processCliArg(argv){
)
}
try {
await sendWebhook({ ...summary, url, appName,webHook})
console.log('Discord message was sent successfully.')
if(!sendOnFail || sendOnFail && summary.testStats.failedTests.length !== 0){
await sendWebhook({ ...summary, url, appName,webHook});
console.log('Discord message was sent successfully.');
}else{
console.log('All tests passed but the report was not sent because of the option --onFail');
}


} catch (error) {
error.name
? console.error(
Expand Down