Skip to content

Commit

Permalink
Merge pull request #22 from Hargne/feature/#20-config-enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
Hargne committed Jan 23, 2018
2 parents 4f15e5e + 0487cbf commit 9305e8a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ npm install jest-html-reporter --save-dev

## Usage
Configure Jest to process the test results by adding the following entry to the Jest config (jest.config.js):
```JSON
```
{
"testResultsProcessor": "./node_modules/jest-html-reporter"
}
Expand All @@ -36,7 +36,7 @@ Then when you run Jest from within the terminal, a file called *test-report.html
### Alternative usage with package.json
Although jest.config.js is specifically created for configuring Jest (and not this plugin), it is possible to configure Jest from within package.json by adding the following as a new line:
```JSON
"jest": { "testResultsProcessor": "./node_modules/jest-html-reporter" }
"jest": {"testResultsProcessor": "./node_modules/jest-html-reporter" }
```

## Node Compatibility
Expand All @@ -45,30 +45,20 @@ This plugin is compatible with Node version `^4.8.3`
## Configuration
The configurations are done directly within your *package.json* file

### pageTitle (string)
*[Default: "Test Suite"]*

The title of the document. This string will also be outputted on the top of the page.

### outputPath (string)
*[Default: "./test-report.html"]*

The path to where the plugin will output the HTML report. The path must include the filename and end with .html

### includeFailureMsg (boolean)
*[Default: false]*

If this setting is set to true, this will output the detailed failure message for each failed test.
| Property | Type | Description | Default
|--|--|--|--|
| `pageTitle` | `STRING` | The title of the document. This string will also be outputted on the top of the page. | `"Test Suite"`
| `outputPath` | `STRING` | The path to where the plugin will output the HTML report. The path must include the filename and end with .html | `"./test-report.html"`
| `includeFailureMsg` | `BOOLEAN` | If this setting is set to true, this will output the detailed failure message for each failed test. | `false`
| `styleOverridePath` | `STRING` | The path to a file containing CSS styles that should override the default styling.* | `null`

### styleOverridePath (string)
*[Default: null]*

The path to a file containing CSS styles that will override the default styling of the report. The plugin will search for the file from the root directory, therefore there is no need to prepend the string with ./ or ../
#### *A Note on styleOverridePath
The plugin will search for the file from the root directory, therefore there is no need to prepend the string with ./ or ../

Have a look at the default styling (located within this repository at *src/style.js*) for a reference to the IDs and classes available for styling.

### Example configuration (package.json)
```JSON
```
{
...
"jest-html-reporter": {
Expand All @@ -82,13 +72,24 @@ Have a look at the default styling (located within this repository at *src/style

## Continuous Integration

The output path and report title can be set with an environment variable for dynamic file saving paths in different environments.
Configuration can also be performed with environment variables for dynamic file saving paths in different environments.

Values in package.json will take precedence over environment variables.
**NOTE:** *Values in package.json will take precedence over environment variables.*

### Example
Here is an example of dynamically naming your output file and test report title to match your current branch that one might see in a automated deployment pipeline before running their tests.

~~~ bash
```bash
export BRANCH_NAME=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`
export TEST_REPORT_PATH=/home/username/jest-test-output/test-reports/"$BRANCH_NAME".html
export TEST_REPORT_TITLE="$BRANCH_NAME"\ Test\ Report
export JEST_HTML_REPORTER_OUTPUT_PATH=/home/username/jest-test-output/test-reports/"$BRANCH_NAME".html
export JEST_HTML_REPORTER_PAGE_TITLE="$BRANCH_NAME"\ Test\ Report
```

### Configuration Environment Variables
| Variable Name | Type | Description | Default
|--|--|--|--|
| `JEST_HTML_REPORTER_PAGE_TITLE` | `STRING` | The title of the document. This string will also be outputted on the top of the page. | `"Test Suite"`
| `JEST_HTML_REPORTER_OUTPUT_PATH` | `STRING` | The path to where the plugin will output the HTML report. The path must include the filename and end with .html | `"./test-report.html"`
| `JEST_HTML_REPORTER_INCLUDE_FAILURE_MSG` | `BOOLEAN` | If this setting is set to true, this will output the detailed failure message for each failed test. | `false`
| `JEST_HTML_REPORTER_STYLE_OVERRIDE_PATH` | `STRING` | The path to a file containing CSS styles that should override the default styling. | `null`

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-html-reporter",
"version": "0.5.8",
"version": "0.6.0",
"description": "Jest test results processor for generating a summary in HTML",
"main": "./src/index",
"scripts": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"jest-html-reporter": {
"pageTitle": "Test Suite",
"outputPath": "test-report.html",
"includeFailureMsg": true
"includeFailureMsg": true,
"styleOverridePath": "src/style.js"
}
}
15 changes: 8 additions & 7 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const logMessage = (type, msg) => {
* Returns the output path for the test report
* @return {String}
*/
const getOutputFilepath = () => config.outputPath || process.env.TEST_REPORT_PATH || path.join(process.cwd(), 'test-report.html');
const getOutputFilepath = () => config.outputPath || process.env.JEST_HTML_REPORTER_OUTPUT_PATH || process.env.TEST_REPORT_PATH || path.join(process.cwd(), 'test-report.html');
/**
* Creates a file at the given destination
* @param {String} filePath
Expand All @@ -45,11 +45,12 @@ const writeFile = (filePath, content) => new Promise((resolve, reject) => {
* @return {Promise}
*/
const getStylesheet = () => new Promise((resolve, reject) => {
const pathToStylesheet = config.styleOverridePath || process.env.JEST_HTML_REPORTER_STYLE_OVERRIDE_PATH;
// If the styleOverridePath has not been set, return the default stylesheet (style.js).
if (!config.styleOverridePath) { return resolve(defaultStylesheet); }
fs.readFile(config.styleOverridePath, 'utf8', (err, content) => {
if (!pathToStylesheet) { return resolve(defaultStylesheet); }
fs.readFile(pathToStylesheet, 'utf8', (err, content) => {
// If there were no errors, return the content of the given file.
return !err ? resolve(content) : reject(`Could not find the specified styleOverridePath: '${config.styleOverridePath}'`);
return !err ? resolve(content) : reject(`Could not find the specified styleOverridePath: '${pathToStylesheet}'`);
});
});
/**
Expand All @@ -60,11 +61,11 @@ const createHtml = (stylesheet) => xmlbuilder.create({
html: {
head: {
meta: { '@charset': 'utf-8' },
title: { '#text': config.pageTitle || process.env.TEST_REPORT_TITLE || 'Test suite' },
title: { '#text': config.pageTitle || process.env.JEST_HTML_REPORTER_PAGE_TITLE || process.env.TEST_REPORT_TITLE || 'Test suite' },
style: { '@type': 'text/css', '#text': stylesheet },
},
body: {
h1: { '@id': 'title', '#text': config.pageTitle || process.env.TEST_REPORT_TITLE || 'Test suite' },
h1: { '@id': 'title', '#text': config.pageTitle || process.env.JEST_HTML_REPORTER_PAGE_TITLE || process.env.TEST_REPORT_TITLE || 'Test suite' },
},
},
});
Expand Down Expand Up @@ -105,7 +106,7 @@ const renderHTML = (testData, stylesheet) => new Promise((resolve, reject) => {
// Test name
const testTitleTd = testTr.ele('td', { class: 'test' }, test.title);
// Test Failure Messages
if (test.failureMessages && config.includeFailureMsg) {
if (test.failureMessages && (config.includeFailureMsg || process.env.JEST_HTML_REPORTER_INCLUDE_FAILURE_MSG)) {
const failureMsgDiv = testTitleTd.ele('div', { class: 'failureMessages' })
test.failureMessages.forEach((failureMsg) => {
failureMsgDiv.ele('p', { class: 'failureMsg' }, stripAnsi(failureMsg));
Expand Down

0 comments on commit 9305e8a

Please sign in to comment.