Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Add Console wrapper to introduce logging level to console for unit tests #20782

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/unit/src/math/Color.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Color } from '../../../../src/math/Color';
import { eps } from './Constants.tests';
import { CONSOLE_LEVEL } from '../../utils/console-wrapper';

export default QUnit.module( 'Maths', () => {

Expand Down Expand Up @@ -483,7 +484,11 @@ export default QUnit.module( 'Maths', () => {
QUnit.test( "setStyleRGBARed", ( assert ) => {

var c = new Color();

console.level = CONSOLE_LEVEL.ERROR;
c.setStyle( 'rgba(255,0,0,0.5)' );
console.level = CONSOLE_LEVEL.DEFAULT;

assert.ok( c.r == 1, "Red: " + c.r );
assert.ok( c.g === 0, "Green: " + c.g );
assert.ok( c.b === 0, "Blue: " + c.b );
Expand Down
1 change: 1 addition & 0 deletions test/unit/three.source.unit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './utils/console-wrapper.js';
import './utils/qunit-utils.js';

//src
Expand Down
62 changes: 62 additions & 0 deletions test/unit/utils/console-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This easy console wrapper introduces the logging level to console for
// preventing console outputs caused when we purposely test the code path
// including console outputs.
//
// Example: Prevent the console warnings caused by Color.setStyle().
// const c = new Color();
// console.level = CONSOLE_LEVEL.ERROR;
// c.setStyle( 'rgba(255,0,0,0.5)' );
// console.level = CONSOLE_LEVEL.DEFAULT;
//
// See https://github.com/mrdoob/three.js/issues/20760#issuecomment-735190998

export const CONSOLE_LEVEL = {
OFF : 0,
ERROR : 1,
WARN : 2,
LOG : 3,
INFO : 4,
DEBUG : 5,
ALL: 6,
DEFAULT: 6
};

console.level = CONSOLE_LEVEL.DEFAULT;

// Save the original methods
console._error = console.error;
console._warn = console.warn;
console._log = console.log;
console._info = console.info;
console._debug = console.debug;

// Wrap console methods
console.error = function () {

if ( this.level >= CONSOLE_LEVEL.ERROR ) this._error.apply( this, arguments );

};

console.warn = function () {

if ( this.level >= CONSOLE_LEVEL.WARN ) this._warn.apply( this, arguments );

};

console.log = function () {

if ( this.level >= CONSOLE_LEVEL.LOG ) this._log.apply( this, arguments );

};

console.info = function () {

if ( this.level >= CONSOLE_LEVEL.INFO ) this._info.apply( this, arguments );

};

console.debug = function () {

if ( this.level >= CONSOLE_LEVEL.DEBUG ) this._debug.apply( this, arguments );

};