diff --git a/CHANGELOG.md b/CHANGELOG.md
index e531e70..8b51328 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,11 @@
# mochawesome changelog
## [Unreleased]
+### Added
+- Support for `[name]` replacement token in `reportFilename` option
+
+### Changed
+- Bump mochawesome-report-generator to 6.1.0
## [7.0.1] - 2021-11-05
### Changed
diff --git a/README.md b/README.md
index 2efb3c8..2046231 100644
--- a/README.md
+++ b/README.md
@@ -109,13 +109,37 @@ var mocha = new Mocha({
The options below are specific to the reporter. For a list of all available options see [mochawesome-report-generator options][marge-options].
-| Option Name | Type | Default | Description |
-| :---------------- | :------ | :---------- | :---------------------------------------------------------------------------------------------------- |
-| `quiet` | boolean | false | Silence console messages |
-| `reportFilename` | string | mochawesome | Filename of saved report
_Applies to the generated html and json files._ |
-| `html` | boolean | true | Save the HTML output for the test run |
-| `json` | boolean | true | Save the JSON output for the test run |
-| `consoleReporter` | string | spec | Name of mocha reporter to use for console output, or `none` to disable console report output entirely |
+| Option Name | Type | Default | Description |
+| :---------------- | :------ | :---------- | :-------------------------------------------------------------------------------------------------------------------------------- |
+| `quiet` | boolean | false | Silence console messages |
+| `reportFilename` | string | mochawesome | Filename of saved report (html and json)
_See [notes](#reportfilename-replacement-tokens) for available token replacements._ |
+| `html` | boolean | true | Save the HTML output for the test run |
+| `json` | boolean | true | Save the JSON output for the test run |
+| `consoleReporter` | string | spec | Name of mocha reporter to use for console output, or `none` to disable console report output entirely |
+
+#### reportFilename replacement tokens
+
+Using the following tokens it is possible to dynamically alter the filename of the generated report.
+
+- **[name]** will be replaced with the spec filename when possible.
+- **[status]** will be replaced with the status (pass/fail) of the test run.
+- **[datetime]** will be replaced with a timestamp. The format can be - specified using the `timestamp` option.
+
+For example, given the spec `cypress/integration/sample.spec.js` and the following config:
+
+```
+{
+ reporter: "mochawesome",
+ reporterOptions: {
+ reportFilename: "[status]_[datetime]-[name]-report",
+ timestamp: "longDate"
+ }
+}
+```
+
+The resulting report file will be named `pass_February_23_2022-sample-report.html`
+
+**Note:** The `[name]` replacement only occurs when mocha is running one spec file per process and outputting a separate report for each spec. The most common use-case is with Cypress.
### Adding Test Context
@@ -199,7 +223,9 @@ describe('test suite', () => {
```
## Typescript
+
This project does not maintain its own type definitions, however they are available on npm from [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/mochawesome).
+
```
$ npm install --save-dev @types/mochawesome
```
diff --git a/package-lock.json b/package-lock.json
index e2c8165..7087307 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -16,7 +16,7 @@
"lodash.isfunction": "^3.0.9",
"lodash.isobject": "^3.0.2",
"lodash.isstring": "^4.0.1",
- "mochawesome-report-generator": "^6.0.1",
+ "mochawesome-report-generator": "^6.1.0",
"strip-ansi": "^6.0.1",
"uuid": "^8.3.2"
},
@@ -2866,9 +2866,9 @@
}
},
"node_modules/mochawesome-report-generator": {
- "version": "6.0.1",
- "resolved": "https://registry.npmjs.org/mochawesome-report-generator/-/mochawesome-report-generator-6.0.1.tgz",
- "integrity": "sha512-/+RoE/lD7QTJBh7VsymdQOTqS8QuB94xKJCJXxZ3fy3JsDdPNgTBgUlEOR99r+2W1zPgEWVhRBU1Tl9fe6kI+w==",
+ "version": "6.1.0",
+ "resolved": "https://registry.npmjs.org/mochawesome-report-generator/-/mochawesome-report-generator-6.1.0.tgz",
+ "integrity": "sha512-qzSZu7gpddeFzRv1dCEQSFhmePg1sYzKfwXMSd5jSlthw5yDPdRV89yMc3NBpfPY/JjclZiHvDtz5S4HgWZ0Rg==",
"dependencies": {
"chalk": "^4.1.2",
"dateformat": "^4.5.1",
@@ -6671,9 +6671,9 @@
}
},
"mochawesome-report-generator": {
- "version": "6.0.1",
- "resolved": "https://registry.npmjs.org/mochawesome-report-generator/-/mochawesome-report-generator-6.0.1.tgz",
- "integrity": "sha512-/+RoE/lD7QTJBh7VsymdQOTqS8QuB94xKJCJXxZ3fy3JsDdPNgTBgUlEOR99r+2W1zPgEWVhRBU1Tl9fe6kI+w==",
+ "version": "6.1.0",
+ "resolved": "https://registry.npmjs.org/mochawesome-report-generator/-/mochawesome-report-generator-6.1.0.tgz",
+ "integrity": "sha512-qzSZu7gpddeFzRv1dCEQSFhmePg1sYzKfwXMSd5jSlthw5yDPdRV89yMc3NBpfPY/JjclZiHvDtz5S4HgWZ0Rg==",
"requires": {
"chalk": "^4.1.2",
"dateformat": "^4.5.1",
diff --git a/package.json b/package.json
index 75e2a77..89f4819 100644
--- a/package.json
+++ b/package.json
@@ -55,7 +55,7 @@
"lodash.isfunction": "^3.0.9",
"lodash.isobject": "^3.0.2",
"lodash.isstring": "^4.0.1",
- "mochawesome-report-generator": "^6.0.1",
+ "mochawesome-report-generator": "^6.1.0",
"strip-ansi": "^6.0.1",
"uuid": "^8.3.2"
},
diff --git a/src/mochawesome.js b/src/mochawesome.js
index e59c6b4..7c9445e 100755
--- a/src/mochawesome.js
+++ b/src/mochawesome.js
@@ -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
@@ -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);
}
});
}
@@ -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,
diff --git a/test-functional/no-suite.js b/test-functional/no-suite.js
new file mode 100644
index 0000000..5316086
--- /dev/null
+++ b/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);
+});
diff --git a/test/reporter.test.js b/test/reporter.test.js
index 2ed1788..7b53659 100644
--- a/test/reporter.test.js
+++ b/test/reporter.test.js
@@ -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);