Skip to content

Commit 4a4d3df

Browse files
committed
test: check 'errorProperties' on report outputs
1 parent aee1485 commit 4a4d3df

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

test/common/report.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ function findReports(pid, dir) {
2424
return results;
2525
}
2626

27-
function validate(filepath) {
27+
function validate(filepath, fields) {
2828
const report = fs.readFileSync(filepath, 'utf8');
2929
if (process.report.compact) {
3030
const end = report.indexOf('\n');
3131
assert.strictEqual(end, report.length - 1);
3232
}
33-
validateContent(JSON.parse(report));
33+
validateContent(JSON.parse(report), fields);
3434
}
3535

36-
function validateContent(report) {
36+
function validateContent(report, fields = []) {
3737
if (typeof report === 'string') {
3838
try {
3939
report = JSON.parse(report);
@@ -43,7 +43,7 @@ function validateContent(report) {
4343
}
4444
}
4545
try {
46-
_validateContent(report);
46+
_validateContent(report, fields);
4747
} catch (err) {
4848
try {
4949
err.stack += util.format('\n------\nFailing Report:\n%O', report);
@@ -52,7 +52,7 @@ function validateContent(report) {
5252
}
5353
}
5454

55-
function _validateContent(report) {
55+
function _validateContent(report, fields = []) {
5656
const isWindows = process.platform === 'win32';
5757

5858
// Verify that all sections are present as own properties of the report.
@@ -71,6 +71,26 @@ function _validateContent(report) {
7171
assert(typeof report[section] === 'object' && report[section] !== null);
7272
});
7373

74+
fields.forEach((field) => {
75+
function checkLoop(actual, rest, expect) {
76+
actual = actual[rest.shift()];
77+
if (rest.length === 0 && actual !== undefined) {
78+
assert.strictEqual(actual, expect);
79+
} else {
80+
assert(actual);
81+
checkLoop(actual, rest, expect);
82+
}
83+
}
84+
let actual, expect;
85+
if (Array.isArray(field)) {
86+
[actual, expect] = field;
87+
} else {
88+
actual = field;
89+
expect = undefined;
90+
}
91+
checkLoop(report, actual.split('.'), expect);
92+
});
93+
7494
// Verify the format of the header section.
7595
const header = report.header;
7696
const headerFields = ['event', 'trigger', 'filename', 'dumpEventTime',
@@ -265,7 +285,7 @@ function _validateContent(report) {
265285

266286
// Verify the format of the workers section.
267287
assert(Array.isArray(report.workers));
268-
report.workers.forEach(_validateContent);
288+
report.workers.forEach((worker) => _validateContent(worker));
269289
}
270290

271291
function checkForUnknownFields(actual, expected) {

test/report/test-report-getreport.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const helper = require('../common/report');
2323
assert.deepStrictEqual(helper.findReports(process.pid, process.cwd()), []);
2424
}
2525

26+
{
27+
const error = new Error();
28+
error.foo = 'goo';
29+
helper.validateContent(process.report.getReport(error),
30+
[['javascriptStack.errorProperties.foo', 'goo']]);
31+
}
32+
2633
// Test with an invalid error argument.
2734
[null, 1, Symbol(), function() {}, 'foo'].forEach((error) => {
2835
assert.throws(() => {

test/report/test-report-writereport.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ process.report.directory = tmpdir.path;
1515
function validate() {
1616
const reports = helper.findReports(process.pid, tmpdir.path);
1717
assert.strictEqual(reports.length, 1);
18-
helper.validate(reports[0]);
18+
helper.validate(reports[0], arguments[0]);
1919
fs.unlinkSync(reports[0]);
2020
return reports[0];
2121
}
@@ -40,6 +40,13 @@ function validate() {
4040
validate();
4141
}
4242

43+
{
44+
const error = new Error();
45+
error.foo = 'goo';
46+
process.report.writeReport(error);
47+
validate([['javascriptStack.errorProperties.foo', 'goo']]);
48+
}
49+
4350
{
4451
// Test with a file argument.
4552
const file = process.report.writeReport('custom-name-1.json');

0 commit comments

Comments
 (0)