summaryrefslogtreecommitdiff
path: root/test/parallel/test-global-console-exists.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-01-20 11:38:35 -0800
committerJames M Snell <jasnell@gmail.com>2016-03-24 13:19:11 -0700
commitc6656db352973d6aea24cb1a3c76adf042b25446 (patch)
treeef3468c6fb403d3dabbb3e3428ae79fb2fbfa884 /test/parallel/test-global-console-exists.js
parentfc8542f1c0e7996cf13a9b5dd4ba9a55bc410cdd (diff)
downloadandroid-node-v8-c6656db352973d6aea24cb1a3c76adf042b25446.tar.gz
android-node-v8-c6656db352973d6aea24cb1a3c76adf042b25446.tar.bz2
android-node-v8-c6656db352973d6aea24cb1a3c76adf042b25446.zip
process: add 'warning' event and process.emitWarning()
In several places throughout the code we write directly to stderr to report warnings (deprecation, possible eventemitter memory leak). The current design of simply dumping the text to stderr is less than ideal. This PR introduces a new "process warnings" mechanism that emits 'warning' events on the global process object. These are invoked with a `warning` argument whose value is an Error object. By default, these warnings will be printed to stderr. This can be suppressed using the `--no-warnings` and `--no-deprecation` command line flags. For warnings, the 'warning' event will still be emitted by the process, allowing applications to handle the warnings in custom ways. The existing `--no-deprecation` flag will continue to supress all deprecation output generated by the core lib. The `--trace-warnings` command line flag will tell Node.js to print the full stack trace of warnings as part of the default handling. The existing `--no-deprecation`, `--throw-deprecation` and `--trace-deprecation` flags continue to work as they currently do, but the exact output of the warning message is modified to occur on process.nextTick(). The stack trace for the warnings and deprecations preserve and point to the correct call site. A new `process.emitWarning()` API is provided to permit userland to emit warnings and deprecations using the same consistent mechanism. Test cases and documentation are included. PR-URL: https://github.com/nodejs/node/pull/4782 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Wyatt Preul <wpreul@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'test/parallel/test-global-console-exists.js')
-rw-r--r--test/parallel/test-global-console-exists.js29
1 files changed, 18 insertions, 11 deletions
diff --git a/test/parallel/test-global-console-exists.js b/test/parallel/test-global-console-exists.js
index 1a13ffec29..32e640cc52 100644
--- a/test/parallel/test-global-console-exists.js
+++ b/test/parallel/test-global-console-exists.js
@@ -4,18 +4,29 @@
'use strict';
+const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');
const leak_warning = /EventEmitter memory leak detected\. 2 hello listeners/;
var write_calls = 0;
-process.stderr.write = function(data) {
+
+process.on('warning', (warning) => {
+ // This will be called after the default internal
+ // process warning handler is called. The default
+ // process warning writes to the console, which will
+ // invoke the monkeypatched process.stderr.write
+ // below.
+ assert.strictEqual(write_calls, 1);
+ EventEmitter.defaultMaxListeners = old_default;
+ // when we get here, we should be done
+});
+
+process.stderr.write = (data) => {
if (write_calls === 0)
assert.ok(data.match(leak_warning));
- else if (write_calls === 1)
- assert.ok(data.match(/Trace/));
else
- assert.ok(false, 'stderr.write should be called only twice');
+ common.fail('stderr.write should be called only once');
write_calls++;
};
@@ -24,13 +35,9 @@ const old_default = EventEmitter.defaultMaxListeners;
EventEmitter.defaultMaxListeners = 1;
const e = new EventEmitter();
-e.on('hello', function() {});
-e.on('hello', function() {});
+e.on('hello', () => {});
+e.on('hello', () => {});
-// TODO: figure out how to validate console. Currently,
+// TODO: Figure out how to validate console. Currently,
// there is no obvious way of validating that console
// exists here exactly when it should.
-
-assert.equal(write_calls, 2);
-
-EventEmitter.defaultMaxListeners = old_default;