diff --git a/test/node-report/test-api-nohooks.js b/test/node-report/test-api-nohooks.js index 478586fe8d8e14..6e7195d6ab84f7 100644 --- a/test/node-report/test-api-nohooks.js +++ b/test/node-report/test-api-nohooks.js @@ -5,6 +5,8 @@ const common = require('../common'); common.skipIfReportDisabled(); const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); const helper = require('../common/report'); const tmpdir = require('../common/tmpdir'); @@ -13,7 +15,55 @@ common.expectWarning('ExperimentalWarning', 'change at any time'); tmpdir.refresh(); process.report.setOptions({ path: tmpdir.path }); -process.report.triggerReport(); -const reports = helper.findReports(process.pid, tmpdir.path); -assert.strictEqual(reports.length, 1); -helper.validate(reports[0]); + +function validate() { + const reports = helper.findReports(process.pid, tmpdir.path); + assert.strictEqual(reports.length, 1); + helper.validate(reports[0]); + fs.unlinkSync(reports[0]); + return reports[0]; +} + +{ + // Test with no arguments. + process.report.triggerReport(); + validate(); +} + +{ + // Test with an error argument. + process.report.triggerReport(new Error('test error')); + validate(); +} + +{ + // Test with a file argument. + const file = process.report.triggerReport('custom-name-1.json'); + const absolutePath = path.join(tmpdir.path, file); + assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0); + assert.strictEqual(file, 'custom-name-1.json'); + helper.validate(absolutePath); + fs.unlinkSync(absolutePath); +} + +{ + // Test with file and error arguments. + const file = process.report.triggerReport('custom-name-2.json', + new Error('test error')); + const absolutePath = path.join(tmpdir.path, file); + assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0); + assert.strictEqual(file, 'custom-name-2.json'); + helper.validate(absolutePath); + fs.unlinkSync(absolutePath); +} + +{ + // Test with a filename option. + const filename = path.join(tmpdir.path, 'custom-name-3.json'); + process.report.setOptions({ filename }); + const file = process.report.triggerReport(); + assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0); + assert.strictEqual(file, filename); + helper.validate(filename); + fs.unlinkSync(filename); +} diff --git a/test/node-report/test-api-pass-error.js b/test/node-report/test-api-pass-error.js deleted file mode 100644 index 9bf59ec425c3a8..00000000000000 --- a/test/node-report/test-api-pass-error.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -// Testcase for passing an error object to the API call. -const common = require('../common'); -common.skipIfReportDisabled(); -const assert = require('assert'); -if (process.argv[2] === 'child') { - try { - throw new Error('Testing error handling'); - } catch (err) { - process.report.triggerReport(err); - } -} else { - const helper = require('../common/report.js'); - const spawn = require('child_process').spawn; - const tmpdir = require('../common/tmpdir'); - tmpdir.refresh(); - const child = spawn(process.execPath, ['--experimental-report', - __filename, 'child'], - { cwd: tmpdir.path }); - child.on('exit', common.mustCall((code) => { - const report_msg = 'No reports found'; - const process_msg = 'Process exited unexpectedly'; - assert.strictEqual(code, 0, process_msg); - const reports = helper.findReports(child.pid, tmpdir.path); - assert.strictEqual(reports.length, 1, report_msg); - const report = reports[0]; - helper.validate(report); - })); -} diff --git a/test/node-report/test-api-trigger-with-filename.js b/test/node-report/test-api-trigger-with-filename.js deleted file mode 100644 index 8603c0a7c31b68..00000000000000 --- a/test/node-report/test-api-trigger-with-filename.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -// Tests when a report is triggered with a given filename. -const common = require('../common'); -common.skipIfReportDisabled(); -const filename = 'myreport.json'; -if (process.argv[2] === 'child') { - process.report.triggerReport(filename); -} else { - const helper = require('../common/report.js'); - const spawn = require('child_process').spawn; - const assert = require('assert'); - const { join } = require('path'); - const tmpdir = require('../common/tmpdir'); - tmpdir.refresh(); - - const child = spawn(process.execPath, - ['--experimental-report', __filename, 'child'], - { cwd: tmpdir.path }); - child.on('exit', common.mustCall((code) => { - const process_msg = 'Process exited unexpectedly'; - assert.strictEqual(code, 0, process_msg + ':' + code); - const reports = helper.findReports(child.pid, tmpdir.path); - assert.strictEqual(reports.length, 0, - `Found unexpected report ${reports[0]}`); - const report = join(tmpdir.path, filename); - helper.validate(report); - })); -} diff --git a/test/node-report/test-api-trigger-with-options.js b/test/node-report/test-api-trigger-with-options.js deleted file mode 100644 index a236fa1305bf46..00000000000000 --- a/test/node-report/test-api-trigger-with-options.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -// Tests when a report is triggered with options set. -const common = require('../common'); -common.skipIfReportDisabled(); -const filename = 'myreport.json'; -if (process.argv[2] === 'child') { - process.report.setOptions({ filename: filename }); - process.report.triggerReport(); -} else { - const helper = require('../common/report.js'); - const spawn = require('child_process').spawn; - const assert = require('assert'); - const { join } = require('path'); - const tmpdir = require('../common/tmpdir'); - tmpdir.refresh(); - - const child = spawn(process.execPath, - ['--experimental-report', __filename, 'child'], - { cwd: tmpdir.path }); - child.on('exit', common.mustCall((code) => { - const process_msg = 'Process exited unexpectedly'; - assert.strictEqual(code, 0, process_msg + ':' + code); - const reports = helper.findReports(child.pid, tmpdir.path); - assert.strictEqual(reports.length, 0, - `Found unexpected report ${reports[0]}`); - const report = join(tmpdir.path, filename); - helper.validate(report); - })); -}