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

reportfilename updates #370

Merged
merged 3 commits into from
Feb 24, 2022
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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