Skip to content

Commit

Permalink
feat: set filename for root suite
Browse files Browse the repository at this point in the history
Supports new replacement tokens in `reportFilename` option
  • Loading branch information
Adam Gruber committed Feb 24, 2022
1 parent c933b63 commit 3a103cc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/mochawesome.js
Expand Up @@ -15,8 +15,8 @@ const { log, mapSuites } = utils;
// Track the total number of tests registered/skipped
const testTotals = {
registered: 0,
skipped: 0
}
skipped: 0,
};

/**
* Done function gets called before mocha exits
Expand Down Expand Up @@ -143,7 +143,7 @@ function Mochawesome(runner, options) {
runner.on(EVENT_SUITE_END, function (suite) {
if (suite.root) {
setSuiteDefaults(suite);
runner.suite.suites.push(...suite.suites)
runner.suite.suites.push(...suite.suites);
}
});
}
Expand All @@ -157,11 +157,19 @@ function Mochawesome(runner, options) {
// so we ensure the suite is processed only once
endCalled = true;

const rootSuite = mapSuites(
this.runner.suite,
testTotals,
this.config
);
const rootSuite = mapSuites(this.runner.suite, testTotals, this.config);

// Attempt to set a filename for the root suite to
// support `reportFilename` [name] replacement token
if (rootSuite.suites.length === 1) {
const firstSuite = rootSuite.suites[0];
rootSuite.file = firstSuite.file;
rootSuite.fullFile = firstSuite.fullFile;
} else if (!rootSuite.suites.length && rootSuite.tests.length) {
const firstTest = this.runner.suite.tests[0];
rootSuite.file = firstTest.file;
rootSuite.fullFile = firstTest.fullFile;
}

const obj = {
stats: this.stats,
Expand Down
7 changes: 7 additions & 0 deletions test-functional/no-suite.js
@@ -0,0 +1,7 @@
it('should pass', () => {
(1 + 1).should.equal(2);
});

it('shall not pass', () => {
(1 + 12).should.equal(2);
});
12 changes: 12 additions & 0 deletions test/reporter.test.js
Expand Up @@ -140,6 +140,18 @@ describe('Mochawesome Reporter', () => {
});
});

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

it('should handle suite with file', done => {
const test = makeTest('test', () => {});
subSuite.addTest(test);
Expand Down

0 comments on commit 3a103cc

Please sign in to comment.