Skip to content

Commit

Permalink
Merge pull request #161 from Hargne/160-added-into-report-configurati…
Browse files Browse the repository at this point in the history
…on-additional-flag-includestacktrace-false

Added includeStackTrace configuration option
  • Loading branch information
Hargne authored May 3, 2023
2 parents 19c8944 + 50ec5d2 commit 722aebb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 64 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Please note that all configuration properties are optional.
| `executionTimeWarningThreshold` | `NUMBER` | The threshold for test execution time (in seconds) in each test suite that will render a warning on the report page. 5 seconds is the default timeout in Jest. | `5` |
| `includeConsoleLog` | `BOOLEAN` | If set to true, this will output all triggered console logs for each test suite. Please note that you have to run Jest together with `--verbose=false` in order to have Jest catch any logs during the tests. | `false` |
| `includeFailureMsg` | `BOOLEAN` | If this setting is set to true, this will output the detailed failure message for each failed test. | `false` |
| `includeStackTrace` | `BOOLEAN` | Turning this option off will cut the stack trace from the failure messages. | `true` |
| `includeSuiteFailure` | `BOOLEAN` | If set to true, this will output the detailed failure message for complete suite failures. | `false` |
| `includeObsoleteSnapshots` | `BOOLEAN` | If set to true, this will output obsolete snapshot names. | `false` |
| `logo` | `STRING` | Path to a logo that will be included in the header of the report | `null` |
Expand Down
2 changes: 1 addition & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"statements": 50
}
},
"verbose": false,
"verbose": true,
"reporters": [
"default",
[
Expand Down
39 changes: 27 additions & 12 deletions src/htmlreporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class HTMLReporter {
// Decide whether to inline the CSS or not
const inlineCSS: boolean =
!this.getConfigValue("useCssFile") &&
!!!this.getConfigValue("styleOverridePath");
!this.getConfigValue("styleOverridePath");

if (inlineCSS) {
const stylesheetContent = fs.readFileSync(stylesheetFilePath, "utf8");
Expand All @@ -116,7 +116,7 @@ class HTMLReporter {
reportBody.raw(reportContent.toString());
}
// Add any given custom script to the end of the body
if (!!this.getConfigValue("customScriptPath")) {
if (this.getConfigValue("customScriptPath")) {
reportBody.raw(
`<script src="${this.getConfigValue("customScriptPath")}"></script>`
);
Expand Down Expand Up @@ -404,13 +404,22 @@ class HTMLReporter {
},
" "
);
test.failureMessages.forEach((failureMsg) => {
failureMsgDiv.ele(
"pre",
{ class: "failureMsg" },
this.sanitizeOutput(failureMsg)
);
});
test.failureMessages
.map((failureMsg) =>
!this.getConfigValue("includeStackTrace")
? failureMsg
.split(/\n\s+at/)[0]
.trim()
.replace(/\n+$/, "")
: failureMsg
)
.forEach((failureMsg) => {
failureMsgDiv.ele(
"pre",
{ class: "failureMsg" },
this.sanitizeOutput(failureMsg)
);
});
}
});

Expand Down Expand Up @@ -517,6 +526,7 @@ class HTMLReporter {
logo,
includeConsoleLog,
includeFailureMsg,
includeStackTrace,
includeSuiteFailure,
includeObsoleteSnapshots,
outputPath,
Expand Down Expand Up @@ -565,6 +575,11 @@ class HTMLReporter {
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_FAILURE_MSG",
configValue: includeFailureMsg,
},
includeStackTrace: {
defaultValue: true,
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_STACK_TRACE",
configValue: includeStackTrace,
},
includeSuiteFailure: {
defaultValue: false,
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_SUITE_FAILURE",
Expand Down Expand Up @@ -625,7 +640,7 @@ class HTMLReporter {
if (jesthtmlreporterconfig) {
const parsedConfig = JSON.parse(jesthtmlreporterconfig);
for (const key of Object.keys(parsedConfig)) {
if (this.config[key as keyof IJestHTMLReporterConfig]) {
if (key in this.config) {
this.config[key as keyof IJestHTMLReporterConfig].configValue =
parsedConfig[key];
}
Expand All @@ -644,7 +659,7 @@ class HTMLReporter {
if (packageJson) {
const parsedConfig = JSON.parse(packageJson)["jest-html-reporter"];
for (const key of Object.keys(parsedConfig)) {
if (this.config[key as keyof IJestHTMLReporterConfig]) {
if (key in this.config) {
this.config[key as keyof IJestHTMLReporterConfig].configValue =
parsedConfig[key];
}
Expand All @@ -669,7 +684,7 @@ class HTMLReporter {
if (process.env[option.environmentVariable]) {
return process.env[option.environmentVariable];
}
return option.configValue || option.defaultValue;
return option.configValue ?? option.defaultValue;
}

/**
Expand Down
51 changes: 5 additions & 46 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type IJestHTMLReporterConfigOptions = {
executionTimeWarningThreshold?: number;
includeConsoleLog?: boolean;
includeFailureMsg?: boolean;
includeStackTrace?: boolean;
includeSuiteFailure?: boolean;
includeObsoleteSnapshots?: boolean;
logo?: string;
Expand All @@ -35,53 +36,11 @@ export interface IJestHTMLReporterConfigOption<T> {
defaultValue: T;
}

export interface IJestHTMLReporterConfig {
append: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["append"]
export type IJestHTMLReporterConfig = {
[key in keyof IJestHTMLReporterConfigOptions]: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions[key]
>;
boilerplate: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["boilerplate"]
>;
customScriptPath: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["customScriptPath"]
>;
dateFormat: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["dateFormat"]
>;
executionTimeWarningThreshold: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["executionTimeWarningThreshold"]
>;
includeConsoleLog: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["includeConsoleLog"]
>;
includeFailureMsg: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["includeFailureMsg"]
>;
includeSuiteFailure: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["includeSuiteFailure"]
>;
includeObsoleteSnapshots: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["includeObsoleteSnapshots"]
>;
logo: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["logo"]>;
outputPath: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["outputPath"]
>;
pageTitle: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["pageTitle"]
>;
sort: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["sort"]>;
statusIgnoreFilter: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["statusIgnoreFilter"]
>;
styleOverridePath: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["styleOverridePath"]
>;
theme: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["theme"]>;
useCssFile: IJestHTMLReporterConfigOption<
IJestHTMLReporterConfigOptions["useCssFile"]
>;
}
};

export interface IJestHTMLReporterConsole {
filePath: string;
Expand Down
48 changes: 48 additions & 0 deletions test/htmlreporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,54 @@ describe("HTMLReporter", () => {
});
});

describe("includeStackTrace", () => {
it("should remove stack traces in failure messages if set to false", async () => {
const reporter = new HTMLReporter({
testData: mockedJestResponseSingleTestResult,
options: {
includeFailureMsg: true,
includeStackTrace: false,
},
});
const reportContent = await reporter.renderTestReportContent();
expect(reportContent).toBeDefined();
expect(
reportContent!
.toString()
.indexOf(
'<pre class="failureMsg">Error: failures that happened</pre>'
)
).not.toBe(-1);
});
it("should keep stack trace in failure messages if set to true", async () => {
const reporter = new HTMLReporter({
testData: mockedJestResponseSingleTestResult,
options: {
includeFailureMsg: true,
includeStackTrace: true,
},
});
const reportContent = await reporter.renderTestReportContent();
expect(reportContent).toBeDefined();
expect(
reportContent!.toString().indexOf("at stack trace")
).toBeGreaterThan(-1);
});
it("should keep stack trace in failure messages if undefined", async () => {
const reporter = new HTMLReporter({
testData: mockedJestResponseSingleTestResult,
options: {
includeFailureMsg: true,
},
});
const reportContent = await reporter.renderTestReportContent();
expect(reportContent).toBeDefined();
expect(
reportContent!.toString().indexOf("at stack trace")
).toBeGreaterThan(-1);
});
});

describe("includeSuiteFailure", () => {
it("should include suite failure message", async () => {
const reporter = new HTMLReporter({
Expand Down
5 changes: 3 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import sinon, { SinonStub } from "sinon";

import JestHTMLReporter from "../src";
import {
mockedFullReportOutput,
mockedJestGlobalConfig,
mockedJestResponseSingleTestResult,
} from "./mockdata";
Expand All @@ -12,7 +11,9 @@ describe("index", () => {
let writeFileSync: SinonStub;

beforeEach(() => {
writeFileSync = sinon.stub(fs, "writeFileSync").returns(null as void);
writeFileSync = sinon
.stub(fs, "writeFileSync")
.returns(null as unknown as void);
});
afterEach(() => {
writeFileSync.restore();
Expand Down
12 changes: 9 additions & 3 deletions test/mockdata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export const mockedJestResponseSingleTestResult: AggregatedResult = {
title: "title",
status: "pending",
ancestorTitles: ["ancestor"],
failureMessages: ["failure"],
failureMessages: [
"Error: failures that happened\n" + "\n" + " at stack trace",
],
failureDetails: ["detailed failure"],
numPassingAsserts: 0,
fullName: "pending",
Expand Down Expand Up @@ -215,7 +217,9 @@ export const mockedJestResponseMultipleTestResult: AggregatedResult = {
title: "title c",
status: "failed",
ancestorTitles: ["ancestor c", "ancestor child"],
failureMessages: ["failure"],
failureMessages: [
"Error: failures that happened\n" + "\n" + " at stack trace",
],
failureDetails: ["detailed failure"],
numPassingAsserts: 0,
fullName: "failed",
Expand Down Expand Up @@ -279,7 +283,9 @@ export const mockedJestResponseMultipleTestResult: AggregatedResult = {
title: "title c",
status: "failed",
ancestorTitles: ["ancestor c"],
failureMessages: ["failure"],
failureMessages: [
"Error: failures that happened\n" + "\n" + " at stack trace",
],
failureDetails: ["detailed failure"],
numPassingAsserts: 0,
fullName: "failed",
Expand Down

0 comments on commit 722aebb

Please sign in to comment.