From 2089f5970acf426e4123e05374c6e58ab658d66d Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 9 Mar 2019 20:49:24 -0800 Subject: [PATCH] test,console: add testing for monkeypatching of console stdio lib/internal/console/constructor.js contains setters for console._stdout and console._stderr but these setters are not used in our tests or in Node.js core. (This is confirmed by our nightly coverage reports.) Add a test to check monkeypatching _stdout and _stderr on a console object. Version 2.6.9 of the very-popular npm module `debug` used this monkeypatching in its code. No other version did and they are now at version 4.something. It is not inconceivable that we would want to change the setters to throw rather than work. Given that this has seen use in the ecosystem, I'm inclined to leave the functionality in place. --- test/parallel/test-console-stdio-setters.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 test/parallel/test-console-stdio-setters.js diff --git a/test/parallel/test-console-stdio-setters.js b/test/parallel/test-console-stdio-setters.js new file mode 100644 index 00000000000000..6c7d0adfa03aed --- /dev/null +++ b/test/parallel/test-console-stdio-setters.js @@ -0,0 +1,20 @@ +'use strict'; + +// Test that monkeypatching console._stdout and console._stderr works. +const common = require('../common'); + +const { Writable } = require('stream'); +const { Console } = require('console'); + +const streamToNowhere = new Writable({ write: common.mustCall() }); +const anotherStreamToNowhere = new Writable({ write: common.mustCall() }); +const myConsole = new Console(process.stdout); + +// Overriding the _stdout and _stderr properties this way is what we are +// testing. Don't change this to be done via arguments passed to the constructor +// above. +myConsole._stdout = streamToNowhere; +myConsole._stderr = anotherStreamToNowhere; + +myConsole.log('fhqwhgads'); +myConsole.error('fhqwhgads');