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

fix: support reportDir mocha reporter option #203

Open
wants to merge 1 commit into
base: master
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
12 changes: 9 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ async function start() {
console.error(
'Some test suites likely terminated without passing on results, exit with error'
);
const missingTestResults = testSuitePaths.filter(
(path) => !timeMap.get(path)
);
const missingTestResults = [].concat(testSuitePaths);
timeMap.forEach((_, pathEnd) => {
const index = missingTestResults.findIndex((p) =>
path.resolve(p).endsWith(pathEnd)
);
if (index > -1) {
missingTestResults.splice(index, 1);
}
});
console.log(
`The following test suites are missing results: ${missingTestResults}`
);
Expand Down
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