Skip to content

Commit

Permalink
fix: support reportDir mocha reporter option
Browse files Browse the repository at this point in the history
Adding other fixes to make file paths less “hardcoded”
  • Loading branch information
SimeonC committed Aug 5, 2024
1 parent f758d45 commit 6eb2795
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lib/json-stream.reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var constants = require('mocha/lib/runner').constants;
var path = require('path');
var fs = require('fs');

const { resultsPath } = require('./shared-config');
const { resultsPath: defaultResultsPath } = require('./shared-config');

const { EVENT_SUITE_END } = constants;

Expand Down Expand Up @@ -43,7 +43,7 @@ function JSONStreamCustom(runner, options) {
}

runner.on(EVENT_SUITE_END, function () {
writeFile(cleanStatistics());
writeFile.apply(self, [cleanStatistics()]);
});
}

Expand All @@ -54,10 +54,13 @@ function calculateDuration(start, end) {
}

function writeFile(statistics) {
const resultsPath = this.options.reporterOptions.reportDir
? path.join(process.cwd(), this.options.reporterOptions.reportDir)
: defaultResultsPath;
// replace forward and backward slash with _ to generate filename
const fileName = statistics.file.replace(/\\|\//g, '_');
if (!fs.existsSync(resultsPath)) {
fs.mkdirSync(resultsPath);
fs.mkdirSync(resultsPath, { recursive: true });
}
const specResultPath = path.join(resultsPath, `${fileName}.json`);
fs.writeFileSync(specResultPath, JSON.stringify(statistics, null, 2));
Expand Down
13 changes: 9 additions & 4 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ function generateWeightsFile(specWeights, totalDuration, totalWeight) {
});
const weightsJson = JSON.stringify(specWeights);
try {
fs.writeFileSync(`${settings.weightsJSON}`, weightsJson, 'utf8');
console.log('Weights file generated.')
} catch(e) {
console.error(e)
const filepath = path.join(process.cwd(), `${settings.weightsJSON}`);
const directory = path.dirname(filepath);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
fs.writeFileSync(filepath, weightsJson, 'utf8');
console.log('Weights file generated.');
} catch (e) {
console.error(e);
}
}

Expand Down

0 comments on commit 6eb2795

Please sign in to comment.