Skip to content

Commit 722aebb

Browse files
authored
Merge pull request #161 from Hargne/160-added-into-report-configuration-additional-flag-includestacktrace-false
Added includeStackTrace configuration option
2 parents 19c8944 + 50ec5d2 commit 722aebb

File tree

7 files changed

+94
-64
lines changed

7 files changed

+94
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Please note that all configuration properties are optional.
7878
| `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` |
7979
| `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` |
8080
| `includeFailureMsg` | `BOOLEAN` | If this setting is set to true, this will output the detailed failure message for each failed test. | `false` |
81+
| `includeStackTrace` | `BOOLEAN` | Turning this option off will cut the stack trace from the failure messages. | `true` |
8182
| `includeSuiteFailure` | `BOOLEAN` | If set to true, this will output the detailed failure message for complete suite failures. | `false` |
8283
| `includeObsoleteSnapshots` | `BOOLEAN` | If set to true, this will output obsolete snapshot names. | `false` |
8384
| `logo` | `STRING` | Path to a logo that will be included in the header of the report | `null` |

jest.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"statements": 50
2929
}
3030
},
31-
"verbose": false,
31+
"verbose": true,
3232
"reporters": [
3333
"default",
3434
[

src/htmlreporter.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class HTMLReporter {
9797
// Decide whether to inline the CSS or not
9898
const inlineCSS: boolean =
9999
!this.getConfigValue("useCssFile") &&
100-
!!!this.getConfigValue("styleOverridePath");
100+
!this.getConfigValue("styleOverridePath");
101101

102102
if (inlineCSS) {
103103
const stylesheetContent = fs.readFileSync(stylesheetFilePath, "utf8");
@@ -116,7 +116,7 @@ class HTMLReporter {
116116
reportBody.raw(reportContent.toString());
117117
}
118118
// Add any given custom script to the end of the body
119-
if (!!this.getConfigValue("customScriptPath")) {
119+
if (this.getConfigValue("customScriptPath")) {
120120
reportBody.raw(
121121
`<script src="${this.getConfigValue("customScriptPath")}"></script>`
122122
);
@@ -404,13 +404,22 @@ class HTMLReporter {
404404
},
405405
" "
406406
);
407-
test.failureMessages.forEach((failureMsg) => {
408-
failureMsgDiv.ele(
409-
"pre",
410-
{ class: "failureMsg" },
411-
this.sanitizeOutput(failureMsg)
412-
);
413-
});
407+
test.failureMessages
408+
.map((failureMsg) =>
409+
!this.getConfigValue("includeStackTrace")
410+
? failureMsg
411+
.split(/\n\s+at/)[0]
412+
.trim()
413+
.replace(/\n+$/, "")
414+
: failureMsg
415+
)
416+
.forEach((failureMsg) => {
417+
failureMsgDiv.ele(
418+
"pre",
419+
{ class: "failureMsg" },
420+
this.sanitizeOutput(failureMsg)
421+
);
422+
});
414423
}
415424
});
416425

@@ -517,6 +526,7 @@ class HTMLReporter {
517526
logo,
518527
includeConsoleLog,
519528
includeFailureMsg,
529+
includeStackTrace,
520530
includeSuiteFailure,
521531
includeObsoleteSnapshots,
522532
outputPath,
@@ -565,6 +575,11 @@ class HTMLReporter {
565575
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_FAILURE_MSG",
566576
configValue: includeFailureMsg,
567577
},
578+
includeStackTrace: {
579+
defaultValue: true,
580+
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_STACK_TRACE",
581+
configValue: includeStackTrace,
582+
},
568583
includeSuiteFailure: {
569584
defaultValue: false,
570585
environmentVariable: "JEST_HTML_REPORTER_INCLUDE_SUITE_FAILURE",
@@ -625,7 +640,7 @@ class HTMLReporter {
625640
if (jesthtmlreporterconfig) {
626641
const parsedConfig = JSON.parse(jesthtmlreporterconfig);
627642
for (const key of Object.keys(parsedConfig)) {
628-
if (this.config[key as keyof IJestHTMLReporterConfig]) {
643+
if (key in this.config) {
629644
this.config[key as keyof IJestHTMLReporterConfig].configValue =
630645
parsedConfig[key];
631646
}
@@ -644,7 +659,7 @@ class HTMLReporter {
644659
if (packageJson) {
645660
const parsedConfig = JSON.parse(packageJson)["jest-html-reporter"];
646661
for (const key of Object.keys(parsedConfig)) {
647-
if (this.config[key as keyof IJestHTMLReporterConfig]) {
662+
if (key in this.config) {
648663
this.config[key as keyof IJestHTMLReporterConfig].configValue =
649664
parsedConfig[key];
650665
}
@@ -669,7 +684,7 @@ class HTMLReporter {
669684
if (process.env[option.environmentVariable]) {
670685
return process.env[option.environmentVariable];
671686
}
672-
return option.configValue || option.defaultValue;
687+
return option.configValue ?? option.defaultValue;
673688
}
674689

675690
/**

src/types/index.d.ts

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type IJestHTMLReporterConfigOptions = {
1717
executionTimeWarningThreshold?: number;
1818
includeConsoleLog?: boolean;
1919
includeFailureMsg?: boolean;
20+
includeStackTrace?: boolean;
2021
includeSuiteFailure?: boolean;
2122
includeObsoleteSnapshots?: boolean;
2223
logo?: string;
@@ -35,53 +36,11 @@ export interface IJestHTMLReporterConfigOption<T> {
3536
defaultValue: T;
3637
}
3738

38-
export interface IJestHTMLReporterConfig {
39-
append: IJestHTMLReporterConfigOption<
40-
IJestHTMLReporterConfigOptions["append"]
39+
export type IJestHTMLReporterConfig = {
40+
[key in keyof IJestHTMLReporterConfigOptions]: IJestHTMLReporterConfigOption<
41+
IJestHTMLReporterConfigOptions[key]
4142
>;
42-
boilerplate: IJestHTMLReporterConfigOption<
43-
IJestHTMLReporterConfigOptions["boilerplate"]
44-
>;
45-
customScriptPath: IJestHTMLReporterConfigOption<
46-
IJestHTMLReporterConfigOptions["customScriptPath"]
47-
>;
48-
dateFormat: IJestHTMLReporterConfigOption<
49-
IJestHTMLReporterConfigOptions["dateFormat"]
50-
>;
51-
executionTimeWarningThreshold: IJestHTMLReporterConfigOption<
52-
IJestHTMLReporterConfigOptions["executionTimeWarningThreshold"]
53-
>;
54-
includeConsoleLog: IJestHTMLReporterConfigOption<
55-
IJestHTMLReporterConfigOptions["includeConsoleLog"]
56-
>;
57-
includeFailureMsg: IJestHTMLReporterConfigOption<
58-
IJestHTMLReporterConfigOptions["includeFailureMsg"]
59-
>;
60-
includeSuiteFailure: IJestHTMLReporterConfigOption<
61-
IJestHTMLReporterConfigOptions["includeSuiteFailure"]
62-
>;
63-
includeObsoleteSnapshots: IJestHTMLReporterConfigOption<
64-
IJestHTMLReporterConfigOptions["includeObsoleteSnapshots"]
65-
>;
66-
logo: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["logo"]>;
67-
outputPath: IJestHTMLReporterConfigOption<
68-
IJestHTMLReporterConfigOptions["outputPath"]
69-
>;
70-
pageTitle: IJestHTMLReporterConfigOption<
71-
IJestHTMLReporterConfigOptions["pageTitle"]
72-
>;
73-
sort: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["sort"]>;
74-
statusIgnoreFilter: IJestHTMLReporterConfigOption<
75-
IJestHTMLReporterConfigOptions["statusIgnoreFilter"]
76-
>;
77-
styleOverridePath: IJestHTMLReporterConfigOption<
78-
IJestHTMLReporterConfigOptions["styleOverridePath"]
79-
>;
80-
theme: IJestHTMLReporterConfigOption<IJestHTMLReporterConfigOptions["theme"]>;
81-
useCssFile: IJestHTMLReporterConfigOption<
82-
IJestHTMLReporterConfigOptions["useCssFile"]
83-
>;
84-
}
43+
};
8544

8645
export interface IJestHTMLReporterConsole {
8746
filePath: string;

test/htmlreporter.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,54 @@ describe("HTMLReporter", () => {
182182
});
183183
});
184184

185+
describe("includeStackTrace", () => {
186+
it("should remove stack traces in failure messages if set to false", async () => {
187+
const reporter = new HTMLReporter({
188+
testData: mockedJestResponseSingleTestResult,
189+
options: {
190+
includeFailureMsg: true,
191+
includeStackTrace: false,
192+
},
193+
});
194+
const reportContent = await reporter.renderTestReportContent();
195+
expect(reportContent).toBeDefined();
196+
expect(
197+
reportContent!
198+
.toString()
199+
.indexOf(
200+
'<pre class="failureMsg">Error: failures that happened</pre>'
201+
)
202+
).not.toBe(-1);
203+
});
204+
it("should keep stack trace in failure messages if set to true", async () => {
205+
const reporter = new HTMLReporter({
206+
testData: mockedJestResponseSingleTestResult,
207+
options: {
208+
includeFailureMsg: true,
209+
includeStackTrace: true,
210+
},
211+
});
212+
const reportContent = await reporter.renderTestReportContent();
213+
expect(reportContent).toBeDefined();
214+
expect(
215+
reportContent!.toString().indexOf("at stack trace")
216+
).toBeGreaterThan(-1);
217+
});
218+
it("should keep stack trace in failure messages if undefined", async () => {
219+
const reporter = new HTMLReporter({
220+
testData: mockedJestResponseSingleTestResult,
221+
options: {
222+
includeFailureMsg: true,
223+
},
224+
});
225+
const reportContent = await reporter.renderTestReportContent();
226+
expect(reportContent).toBeDefined();
227+
expect(
228+
reportContent!.toString().indexOf("at stack trace")
229+
).toBeGreaterThan(-1);
230+
});
231+
});
232+
185233
describe("includeSuiteFailure", () => {
186234
it("should include suite failure message", async () => {
187235
const reporter = new HTMLReporter({

test/index.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import sinon, { SinonStub } from "sinon";
33

44
import JestHTMLReporter from "../src";
55
import {
6-
mockedFullReportOutput,
76
mockedJestGlobalConfig,
87
mockedJestResponseSingleTestResult,
98
} from "./mockdata";
@@ -12,7 +11,9 @@ describe("index", () => {
1211
let writeFileSync: SinonStub;
1312

1413
beforeEach(() => {
15-
writeFileSync = sinon.stub(fs, "writeFileSync").returns(null as void);
14+
writeFileSync = sinon
15+
.stub(fs, "writeFileSync")
16+
.returns(null as unknown as void);
1617
});
1718
afterEach(() => {
1819
writeFileSync.restore();

test/mockdata/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ export const mockedJestResponseSingleTestResult: AggregatedResult = {
9999
title: "title",
100100
status: "pending",
101101
ancestorTitles: ["ancestor"],
102-
failureMessages: ["failure"],
102+
failureMessages: [
103+
"Error: failures that happened\n" + "\n" + " at stack trace",
104+
],
103105
failureDetails: ["detailed failure"],
104106
numPassingAsserts: 0,
105107
fullName: "pending",
@@ -215,7 +217,9 @@ export const mockedJestResponseMultipleTestResult: AggregatedResult = {
215217
title: "title c",
216218
status: "failed",
217219
ancestorTitles: ["ancestor c", "ancestor child"],
218-
failureMessages: ["failure"],
220+
failureMessages: [
221+
"Error: failures that happened\n" + "\n" + " at stack trace",
222+
],
219223
failureDetails: ["detailed failure"],
220224
numPassingAsserts: 0,
221225
fullName: "failed",
@@ -279,7 +283,9 @@ export const mockedJestResponseMultipleTestResult: AggregatedResult = {
279283
title: "title c",
280284
status: "failed",
281285
ancestorTitles: ["ancestor c"],
282-
failureMessages: ["failure"],
286+
failureMessages: [
287+
"Error: failures that happened\n" + "\n" + " at stack trace",
288+
],
283289
failureDetails: ["detailed failure"],
284290
numPassingAsserts: 0,
285291
fullName: "failed",

0 commit comments

Comments
 (0)