-
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 #29 from ASSERT-KTH/28-add-custom-reporter-for-tes…
…t-results Add custom mocha reporter
- Loading branch information
Showing
5 changed files
with
739 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const Mocha = require('mocha'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { fail } = require('assert'); | ||
|
||
// Extend the Spec reporter | ||
const Spec = Mocha.reporters.Spec; | ||
const Base = Mocha.reporters.Base; // For styling and symbols | ||
|
||
class CustomReporter extends Spec { | ||
constructor(runner, options) { | ||
// Call the parent constructor (Spec reporter) | ||
super(runner); | ||
|
||
// Initialize variables to track the current file and passing file count | ||
let passingFiles = 0; | ||
let currentFile = null; | ||
let allTestsPassed = true; | ||
let allFiles = 0; | ||
const testResults = []; | ||
|
||
const exportOptions = options.reporterOptions || {}; | ||
const exportToJson = exportOptions.json || false; | ||
|
||
// When a new suite (test file) starts | ||
runner.on('suite', (suite) => { | ||
if (suite.file) { | ||
if (currentFile !== suite.file) { | ||
// New test file started | ||
currentFile = suite.file; | ||
allTestsPassed = true; // Assume all tests will pass initially | ||
allFiles += 1; | ||
} | ||
} | ||
}); | ||
|
||
// If any test fails | ||
runner.on('fail', () => { | ||
// Mark the current test file as having failed tests | ||
allTestsPassed = false; | ||
}); | ||
|
||
// When a test ends, store its result | ||
runner.on('test end', (test) => { | ||
// only get the string after 'test' in the title | ||
// filename = currentFile.split('/'); | ||
const fileName = currentFile.split('/test/')[1]; | ||
const contractFile = fileName.replace('_test.js', '.sol'); | ||
// console.log(contract_file); | ||
testResults.push({ | ||
title: test.title, | ||
file: fileName, | ||
contractFile: contractFile, | ||
state: test.state, | ||
}); | ||
}); | ||
|
||
// When the suite (test file) ends | ||
runner.on('suite end', (suite) => { | ||
if (suite.file && currentFile === suite.file && allTestsPassed) { | ||
passingFiles += 1; | ||
} | ||
}); | ||
|
||
// At the end of all tests, log the number of passing test files in the same style as passing tests | ||
runner.on('end', () => { | ||
const { tests, passes, failures, pending, duration } = runner.stats; | ||
|
||
const failedFiles = allFiles - passingFiles; | ||
|
||
const formattedMessage = Base.color('green', `Total passing test files: ${passingFiles}/${allFiles}`); | ||
const formattedMessage2 = Base.color('fail', `Total failed files: ${failedFiles}/${allFiles}`); | ||
// // Log the formatted message | ||
console.log(`${formattedMessage}`); | ||
console.log(`${formattedMessage2}`); | ||
|
||
if (exportToJson) { | ||
// Prepare the data to be exported to JSON | ||
const results = { | ||
totalTests: tests, | ||
passingTests: passes, | ||
failingTests: failures, | ||
totalFiles: allFiles, | ||
passingFiles: passingFiles, | ||
failingFiles: failedFiles, | ||
testResults: testResults, | ||
}; | ||
|
||
// Write to JSON file | ||
const jsonPath = path.join(__dirname, 'test-results.json'); | ||
fs.writeFileSync(jsonPath, JSON.stringify(results, null, 2)); | ||
console.log(`\nTest results written to ${jsonPath}`); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = CustomReporter; |
Oops, something went wrong.