Skip to content

Commit

Permalink
test: check 'errorProperties' on report outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jun 25, 2020
1 parent aee1485 commit 4a4d3df
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
32 changes: 26 additions & 6 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ function findReports(pid, dir) {
return results;
}

function validate(filepath) {
function validate(filepath, fields) {
const report = fs.readFileSync(filepath, 'utf8');
if (process.report.compact) {
const end = report.indexOf('\n');
assert.strictEqual(end, report.length - 1);
}
validateContent(JSON.parse(report));
validateContent(JSON.parse(report), fields);
}

function validateContent(report) {
function validateContent(report, fields = []) {
if (typeof report === 'string') {
try {
report = JSON.parse(report);
Expand All @@ -43,7 +43,7 @@ function validateContent(report) {
}
}
try {
_validateContent(report);
_validateContent(report, fields);
} catch (err) {
try {
err.stack += util.format('\n------\nFailing Report:\n%O', report);
Expand All @@ -52,7 +52,7 @@ function validateContent(report) {
}
}

function _validateContent(report) {
function _validateContent(report, fields = []) {
const isWindows = process.platform === 'win32';

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

fields.forEach((field) => {
function checkLoop(actual, rest, expect) {
actual = actual[rest.shift()];
if (rest.length === 0 && actual !== undefined) {
assert.strictEqual(actual, expect);
} else {
assert(actual);
checkLoop(actual, rest, expect);
}
}
let actual, expect;
if (Array.isArray(field)) {
[actual, expect] = field;
} else {
actual = field;
expect = undefined;
}
checkLoop(report, actual.split('.'), expect);
});

// Verify the format of the header section.
const header = report.header;
const headerFields = ['event', 'trigger', 'filename', 'dumpEventTime',
Expand Down Expand Up @@ -265,7 +285,7 @@ function _validateContent(report) {

// Verify the format of the workers section.
assert(Array.isArray(report.workers));
report.workers.forEach(_validateContent);
report.workers.forEach((worker) => _validateContent(worker));
}

function checkForUnknownFields(actual, expected) {
Expand Down
7 changes: 7 additions & 0 deletions test/report/test-report-getreport.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const helper = require('../common/report');
assert.deepStrictEqual(helper.findReports(process.pid, process.cwd()), []);
}

{
const error = new Error();
error.foo = 'goo';
helper.validateContent(process.report.getReport(error),
[['javascriptStack.errorProperties.foo', 'goo']]);
}

// Test with an invalid error argument.
[null, 1, Symbol(), function() {}, 'foo'].forEach((error) => {
assert.throws(() => {
Expand Down
9 changes: 8 additions & 1 deletion test/report/test-report-writereport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ process.report.directory = tmpdir.path;
function validate() {
const reports = helper.findReports(process.pid, tmpdir.path);
assert.strictEqual(reports.length, 1);
helper.validate(reports[0]);
helper.validate(reports[0], arguments[0]);
fs.unlinkSync(reports[0]);
return reports[0];
}
Expand All @@ -40,6 +40,13 @@ function validate() {
validate();
}

{
const error = new Error();
error.foo = 'goo';
process.report.writeReport(error);
validate([['javascriptStack.errorProperties.foo', 'goo']]);
}

{
// Test with a file argument.
const file = process.report.writeReport('custom-name-1.json');
Expand Down

0 comments on commit 4a4d3df

Please sign in to comment.