Skip to content

Commit

Permalink
Merge pull request #370 from adamgruber/reportfilename-updates
Browse files Browse the repository at this point in the history
reportfilename updates
  • Loading branch information
adamgruber committed Feb 24, 2022
2 parents c933b63 + e8786e0 commit 7105e62
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
5 changes: 5 additions & 0 deletions 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
Expand Down
40 changes: 33 additions & 7 deletions README.md
Expand Up @@ -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 <br> _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) <br> _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

Expand Down Expand Up @@ -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
```
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
},
Expand Down
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 7105e62

Please sign in to comment.