summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2017-04-28 09:06:10 -0700
committerJames M Snell <jasnell@gmail.com>2017-05-01 11:53:47 -0700
commitdd20e68b0feb966c2a7439c947bbb4d46e3b19fe (patch)
treea43357ad4f099b554ede9160aef2d2edf3a3d9ce /test
parent23fc082409e46b5d85d66c74affb7a9422f8f489 (diff)
downloadandroid-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.tar.gz
android-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.tar.bz2
android-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.zip
process: add optional detail to process emitWarning
Adds a new method signature variant for process.emitWarning() that accepts an options object. The options object may include a new `detail` option that allows additional detail text to be associated with the warning. By default, this additional text will be printed to stderr along with the warning, and included on the Warning Error object using the `.detail` property. e.g. ```js process.emitWarning('A message', { code: 'WARNING123', detail: 'This is additional detail' }); // Emits: // (node {pid}) [WARNING123] Warning: A message // This is additional detail ``` PR-URL: https://github.com/nodejs/node/pull/12725 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-process-emitwarning.js57
1 files changed, 37 insertions, 20 deletions
diff --git a/test/parallel/test-process-emitwarning.js b/test/parallel/test-process-emitwarning.js
index bebdc989a2..61ca05cfe9 100644
--- a/test/parallel/test-process-emitwarning.js
+++ b/test/parallel/test-process-emitwarning.js
@@ -4,30 +4,48 @@
const common = require('../common');
const assert = require('assert');
-const util = require('util');
+
+const testMsg = 'A Warning';
+const testCode = 'CODE001';
+const testDetail = 'Some detail';
+const testType = 'CustomWarning';
process.on('warning', common.mustCall((warning) => {
assert(warning);
assert(/^(Warning|CustomWarning)/.test(warning.name));
- assert.strictEqual(warning.message, 'A Warning');
- if (warning.code) assert.strictEqual(warning.code, 'CODE001');
-}, 8));
-
-process.emitWarning('A Warning');
-process.emitWarning('A Warning', 'CustomWarning');
-process.emitWarning('A Warning', CustomWarning);
-process.emitWarning('A Warning', 'CustomWarning', CustomWarning);
-process.emitWarning('A Warning', 'CustomWarning', 'CODE001');
-
-function CustomWarning() {
- Error.call(this);
- this.name = 'CustomWarning';
- this.message = 'A Warning';
- this.code = 'CODE001';
- Error.captureStackTrace(this, CustomWarning);
+ assert.strictEqual(warning.message, testMsg);
+ if (warning.code) assert.strictEqual(warning.code, testCode);
+ if (warning.detail) assert.strictEqual(warning.detail, testDetail);
+}, 15));
+
+class CustomWarning extends Error {
+ constructor() {
+ super();
+ this.name = testType;
+ this.message = testMsg;
+ this.code = testCode;
+ Error.captureStackTrace(this, CustomWarning);
+ }
}
-util.inherits(CustomWarning, Error);
-process.emitWarning(new CustomWarning());
+
+[
+ [testMsg],
+ [testMsg, testType],
+ [testMsg, CustomWarning],
+ [testMsg, testType, CustomWarning],
+ [testMsg, testType, testCode],
+ [testMsg, { type: testType }],
+ [testMsg, { type: testType, code: testCode }],
+ [testMsg, { type: testType, code: testCode, detail: testDetail }],
+ [new CustomWarning()],
+ // detail will be ignored for the following. No errors thrown
+ [testMsg, { type: testType, code: testCode, detail: true }],
+ [testMsg, { type: testType, code: testCode, detail: [] }],
+ [testMsg, { type: testType, code: testCode, detail: null }],
+ [testMsg, { type: testType, code: testCode, detail: 1 }]
+].forEach((i) => {
+ assert.doesNotThrow(() => process.emitWarning.apply(null, i));
+});
const warningNoToString = new CustomWarning();
warningNoToString.toString = null;
@@ -47,7 +65,6 @@ assert.throws(() => process.emitWarning(1), expectedError);
assert.throws(() => process.emitWarning({}), expectedError);
assert.throws(() => process.emitWarning(true), expectedError);
assert.throws(() => process.emitWarning([]), expectedError);
-assert.throws(() => process.emitWarning('', {}), expectedError);
assert.throws(() => process.emitWarning('', '', {}), expectedError);
assert.throws(() => process.emitWarning('', 1), expectedError);
assert.throws(() => process.emitWarning('', '', 1), expectedError);