• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Test producing a report via API call, using the no-hooks/no-signal interface.
4require('../common');
5const assert = require('assert');
6const { spawnSync } = require('child_process');
7const fs = require('fs');
8const path = require('path');
9const helper = require('../common/report');
10const tmpdir = require('../common/tmpdir');
11
12tmpdir.refresh();
13process.report.directory = tmpdir.path;
14
15function validate() {
16  const reports = helper.findReports(process.pid, tmpdir.path);
17  assert.strictEqual(reports.length, 1);
18  helper.validate(reports[0], arguments[0]);
19  fs.unlinkSync(reports[0]);
20  return reports[0];
21}
22
23{
24  // Test with no arguments.
25  process.report.writeReport();
26  validate();
27}
28
29{
30  // Test with an error argument.
31  process.report.writeReport(new Error('test error'));
32  validate();
33}
34
35{
36  // Test with an error with one line stack
37  const error = new Error();
38  error.stack = 'only one line';
39  process.report.writeReport(error);
40  validate();
41}
42
43{
44  const error = new Error();
45  error.foo = 'goo';
46  process.report.writeReport(error);
47  validate([['javascriptStack.errorProperties.foo', 'goo']]);
48}
49
50{
51  // Test with a file argument.
52  const file = process.report.writeReport('custom-name-1.json');
53  const absolutePath = path.join(tmpdir.path, file);
54  assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
55  assert.strictEqual(file, 'custom-name-1.json');
56  helper.validate(absolutePath);
57  fs.unlinkSync(absolutePath);
58}
59
60{
61  // Test with file and error arguments.
62  const file = process.report.writeReport('custom-name-2.json',
63                                          new Error('test error'));
64  const absolutePath = path.join(tmpdir.path, file);
65  assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
66  assert.strictEqual(file, 'custom-name-2.json');
67  helper.validate(absolutePath);
68  fs.unlinkSync(absolutePath);
69}
70
71{
72  // Test with a filename option.
73  process.report.filename = 'custom-name-3.json';
74  const file = process.report.writeReport();
75  assert.strictEqual(helper.findReports(process.pid, tmpdir.path).length, 0);
76  const filename = path.join(process.report.directory, 'custom-name-3.json');
77  assert.strictEqual(file, process.report.filename);
78  helper.validate(filename);
79  fs.unlinkSync(filename);
80}
81
82// Test with an invalid file argument.
83[null, 1, Symbol(), function() {}].forEach((file) => {
84  assert.throws(() => {
85    process.report.writeReport(file);
86  }, { code: 'ERR_INVALID_ARG_TYPE' });
87});
88
89// Test with an invalid error argument.
90[null, 1, Symbol(), function() {}, 'foo'].forEach((error) => {
91  assert.throws(() => {
92    process.report.writeReport('file', error);
93  }, { code: 'ERR_INVALID_ARG_TYPE' });
94});
95
96{
97  // Test the special "stdout" filename.
98  const args = ['-e', 'process.report.writeReport("stdout")'];
99  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
100  assert.strictEqual(child.status, 0);
101  assert.strictEqual(child.signal, null);
102  assert.strictEqual(helper.findReports(child.pid, tmpdir.path).length, 0);
103  helper.validateContent(child.stdout.toString());
104}
105
106{
107  // Test the special "stderr" filename.
108  const args = ['-e', 'process.report.writeReport("stderr")'];
109  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
110  assert.strictEqual(child.status, 0);
111  assert.strictEqual(child.signal, null);
112  assert.strictEqual(child.stdout.toString().trim(), '');
113  assert.strictEqual(helper.findReports(child.pid, tmpdir.path).length, 0);
114  const report = child.stderr.toString().split('Node.js report completed')[0];
115  helper.validateContent(report);
116}
117
118{
119  // Test the case where the report file cannot be opened.
120  const reportDir = path.join(tmpdir.path, 'does', 'not', 'exist');
121  const args = [`--report-directory=${reportDir}`,
122                '-e',
123                'process.report.writeReport()'];
124  const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });
125
126  assert.strictEqual(child.status, 0);
127  assert.strictEqual(child.signal, null);
128  assert.strictEqual(child.stdout.toString().trim(), '');
129  const stderr = child.stderr.toString();
130  assert(stderr.includes('Failed to open Node.js report file:'));
131}
132