Skip to content

Commit

Permalink
fix: add a fallback for root file/fullFile
Browse files Browse the repository at this point in the history
This addresses an issue where the ouput from a test run with Cypress is not the
same as the test run with mocha directly. Specifically mocha does not set the
`file` property on the root suite and instead sets it on the sub suites or
tests whereas Cypress does the opposite.
  • Loading branch information
adamgruber committed Mar 3, 2022
1 parent 77eba22 commit 3d099f7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# mochawesome changelog

## [Unreleased]
### Fixed
- Add fallback when attempting to set `file` and `fullFile` on root suite. [#371](https://github.com/adamgruber/mochawesome/issues/371)

## [7.1.0] - 2022-02-24
### Added
Expand Down
8 changes: 4 additions & 4 deletions src/mochawesome.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ function Mochawesome(runner, options) {
// support `reportFilename` [name] replacement token
if (rootSuite.suites.length === 1) {
const firstSuite = rootSuite.suites[0];
rootSuite.file = firstSuite.file;
rootSuite.fullFile = firstSuite.fullFile;
rootSuite.file = firstSuite.file || rootSuite.file;
rootSuite.fullFile = firstSuite.fullFile || rootSuite.fullFile;
} else if (!rootSuite.suites.length && rootSuite.tests.length) {
const firstTest = this.runner.suite.tests[0];
rootSuite.file = firstTest.file;
rootSuite.fullFile = firstTest.fullFile;
rootSuite.file = firstTest.file || rootSuite.file;
rootSuite.fullFile = firstTest.fullFile || rootSuite.fullFile;
}

const obj = {
Expand Down
31 changes: 28 additions & 3 deletions test/reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,34 @@ describe('Mochawesome Reporter', () => {
subSuite.addTest(test);
subSuite.file = 'testfile.js';
runner.run(() => {
mochaReporter.output.results[0].suites[0].fullFile.should.equal(
'testfile.js'
);
mochaReporter.output.results[0].fullFile.should.equal('testfile.js');
done();
});
});

it('should handle root suite with file and test without', done => {
const test = makeTest('test', () => {});
suite.file = 'testfile.js';
suite.fullFile = 'testfile.js';
test.file = '';
test.fullFile = '';
suite.addTest(test);
suite.suites = [];
runner.run(() => {
mochaReporter.output.results[0].fullFile.should.equal('testfile.js');
done();
});
});

it('should handle root suite with file and subsuite without', done => {
const test = makeTest('test', () => {});
subSuite.addTest(test);
suite.file = 'testfile.js';
suite.fullFile = 'testfile.js';
subSuite.file = '';
subSuite.fullFile = '';
runner.run(() => {
mochaReporter.output.results[0].fullFile.should.equal('testfile.js');
done();
});
});
Expand Down

0 comments on commit 3d099f7

Please sign in to comment.