Now setting mocked console in warn module for tests
- Replacing console broken in newer versions of node/v8 - Replacing console.warn/log works fine, but is a poor choice for testingclosure/master
parent
0f4ce6acc1
commit
27eea93d6f
25
lib/warn.js
25
lib/warn.js
|
@ -28,6 +28,16 @@
|
||||||
*/
|
*/
|
||||||
var _handler = null;
|
var _handler = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Console to use for logging
|
||||||
|
*
|
||||||
|
* This reference allows an alternative console to be used. Must contain warn()
|
||||||
|
* or log() methods.
|
||||||
|
*
|
||||||
|
* @type {Object}
|
||||||
|
*/
|
||||||
|
var _console = console;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Permits wrapping an exception as a warning
|
* Permits wrapping an exception as a warning
|
||||||
|
@ -106,7 +116,7 @@ exports.handlers = {
|
||||||
{
|
{
|
||||||
var dest;
|
var dest;
|
||||||
|
|
||||||
console && ( dest = console.warn || console.log ) &&
|
_console && ( dest = _console.warn || _console.log ) &&
|
||||||
dest( 'Warning: ' + warning.message );
|
dest( 'Warning: ' + warning.message );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -175,6 +185,19 @@ exports.handle = function( warning )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets active console
|
||||||
|
*
|
||||||
|
* @param {Object} console containing warn() or log() method
|
||||||
|
*
|
||||||
|
* @return {undefined}
|
||||||
|
*/
|
||||||
|
exports.setConsole = function( console )
|
||||||
|
{
|
||||||
|
_console = console;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// set the default handler
|
// set the default handler
|
||||||
_handler = exports.handlers.log;
|
_handler = exports.handlers.log;
|
||||||
|
|
||||||
|
|
|
@ -31,36 +31,15 @@ var common = require( './common' ),
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the console object, without throwing errors if it does not exist
|
|
||||||
*
|
|
||||||
* @return {Object} console
|
|
||||||
*/
|
|
||||||
function backupConsole()
|
|
||||||
{
|
|
||||||
// ensure that we don't throw errors if console is not defined
|
|
||||||
if ( typeof console !== 'undefined' )
|
|
||||||
{
|
|
||||||
return console;
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The log warning handler should log warnings to the console
|
* The log warning handler should log warnings to the console
|
||||||
*/
|
*/
|
||||||
( function testLogWarningHandlerLogsMessageToConsole()
|
( function testLogWarningHandlerLogsMessageToConsole()
|
||||||
{
|
{
|
||||||
var logged = false,
|
var logged = false;
|
||||||
|
|
||||||
// back up console ref
|
|
||||||
console_ = backupConsole()
|
|
||||||
;
|
|
||||||
|
|
||||||
// mock console
|
// mock console
|
||||||
console = {
|
warn.setConsole( {
|
||||||
warn: function( message )
|
warn: function( message )
|
||||||
{
|
{
|
||||||
assert.equal( ( 'Warning: ' + warning.message ), message,
|
assert.equal( ( 'Warning: ' + warning.message ), message,
|
||||||
|
@ -69,7 +48,7 @@ function backupConsole()
|
||||||
|
|
||||||
logged = true;
|
logged = true;
|
||||||
},
|
},
|
||||||
};
|
} );
|
||||||
|
|
||||||
// call handler with the warning
|
// call handler with the warning
|
||||||
warn.handlers.log( warning );
|
warn.handlers.log( warning );
|
||||||
|
@ -79,7 +58,7 @@ function backupConsole()
|
||||||
);
|
);
|
||||||
|
|
||||||
// restore console
|
// restore console
|
||||||
console = console_;
|
warn.setConsole( console );
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,17 +69,14 @@ function backupConsole()
|
||||||
*/
|
*/
|
||||||
( function testLogWarningHandlerHandlesMissingConsole()
|
( function testLogWarningHandlerHandlesMissingConsole()
|
||||||
{
|
{
|
||||||
// back up console
|
|
||||||
var console_ = backupConsole();
|
|
||||||
|
|
||||||
// destroy it
|
// destroy it
|
||||||
console = undefined;
|
warn.setConsole( undefined );
|
||||||
|
|
||||||
// attempt to log
|
// attempt to log
|
||||||
warn.handlers.log( warning );
|
warn.handlers.log( warning );
|
||||||
|
|
||||||
// restore console
|
// restore console
|
||||||
console = console_;
|
warn.setConsole( console );
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
||||||
|
@ -111,16 +87,14 @@ function backupConsole()
|
||||||
*/
|
*/
|
||||||
( function testLogWarningHandlerWillFallBackToLogMethodIfWarnIsMissing()
|
( function testLogWarningHandlerWillFallBackToLogMethodIfWarnIsMissing()
|
||||||
{
|
{
|
||||||
// back up and overwrite console to contain only log()
|
var given = '';
|
||||||
var console_ = backupConsole(),
|
|
||||||
given = '';
|
|
||||||
|
|
||||||
console = {
|
warn.setConsole( {
|
||||||
log: function( message )
|
log: function( message )
|
||||||
{
|
{
|
||||||
given = message;
|
given = message;
|
||||||
}
|
}
|
||||||
};
|
} );
|
||||||
|
|
||||||
// attempt to log
|
// attempt to log
|
||||||
warn.handlers.log( warning );
|
warn.handlers.log( warning );
|
||||||
|
@ -130,7 +104,7 @@ function backupConsole()
|
||||||
);
|
);
|
||||||
|
|
||||||
// restore console
|
// restore console
|
||||||
console = console_;
|
warn.setConsole( console );
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,14 +140,13 @@ function backupConsole()
|
||||||
( function testDismissWarningHandlerShouldDoNothing()
|
( function testDismissWarningHandlerShouldDoNothing()
|
||||||
{
|
{
|
||||||
// destroy the console to ensure nothing is logged
|
// destroy the console to ensure nothing is logged
|
||||||
var console_ = backupConsole();
|
warn.setConsole( undefined );
|
||||||
console = undefined;
|
|
||||||
|
|
||||||
// don't catch anything, to ensure no errors occur and that no exceptions
|
// don't catch anything, to ensure no errors occur and that no exceptions
|
||||||
// are thrown
|
// are thrown
|
||||||
warn.handlers.dismiss( warning );
|
warn.handlers.dismiss( warning );
|
||||||
|
|
||||||
// restore console
|
// restore console
|
||||||
console = console_;
|
warn.setConsole( console );
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
|
@ -35,17 +35,15 @@ var common = require( './common' ),
|
||||||
*/
|
*/
|
||||||
( function testDefaultHandlerIsLogger()
|
( function testDefaultHandlerIsLogger()
|
||||||
{
|
{
|
||||||
// back up console object
|
var called = false;
|
||||||
var console_ = ( typeof console !== 'undefined' ) ? console : undefined,
|
|
||||||
called = false;
|
|
||||||
|
|
||||||
// stub it
|
// stub it
|
||||||
console = {
|
warn.setConsole( {
|
||||||
warn: function()
|
warn: function()
|
||||||
{
|
{
|
||||||
called = true;
|
called = true;
|
||||||
},
|
},
|
||||||
};
|
} );
|
||||||
|
|
||||||
warn.handle( warn.Warning( Error( 'foo' ) ) );
|
warn.handle( warn.Warning( Error( 'foo' ) ) );
|
||||||
|
|
||||||
|
@ -54,7 +52,7 @@ var common = require( './common' ),
|
||||||
);
|
);
|
||||||
|
|
||||||
// restore console
|
// restore console
|
||||||
console = console_;
|
warn.setConsole( console );
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue