qunit: Implement suppressWarnings/restoreWarnings
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 7 Apr 2014 18:43:19 +0000 (11:43 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 8 Apr 2014 01:02:47 +0000 (01:02 +0000)
There are various tests triggering deprecation warnings because
they are testing deprecated functionality, on purpose.

Surpress these so that the logs aren't filled with false
positives in Jenkins.

Change-Id: I4bb546781a0c89999b2e5df7715abf492a44856d

tests/qunit/data/testrunner.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.test.js

index 73ae0e6..7dff354 100644 (file)
         * </code>
         */
        QUnit.newMwEnvironment = ( function () {
-               var log, liveConfig, liveMessages;
+               var warn, log, liveConfig, liveMessages;
 
                liveConfig = mw.config.values;
                liveMessages = mw.messages.values;
 
+               function suppressWarnings() {
+                       warn = mw.log.warn;
+                       mw.log.warn = $.noop;
+               }
+
+               function restoreWarnings() {
+                       if ( warn !== undefined ) {
+                               mw.log.warn = warn;
+                               warn = undefined;
+                       }
+               }
+
                function freshConfigCopy( custom ) {
-                       var copy, warn;
+                       var copy;
                        // Tests should mock all factors that directly influence the tested code.
                        // For backwards compatibility though we set mw.config to a fresh copy of the live
                        // config. This way any modifications made to mw.config during the test will not
                        // affect other tests, nor the global scope outside the test runner.
                        // This is a shallow copy, since overriding an array or object value via "custom"
                        // should replace it. Setting a config property means you override it, not extend it.
-                       // NOTE: It is important that we temporarily disable mw.log#warn as extend() will
-                       // trigger MWDeprecationWarning for each of the deprecated properties.
-                       warn = mw.log.warn;
-                       mw.log.warn = $.noop;
-
+                       // NOTE: It is important that we suppress warnings because extend() will also access
+                       // deprecated properties and trigger deprecation warnings from mw.log#deprecate.
+                       suppressWarnings();
                        copy = $.extend( {}, liveConfig, custom );
-
-                       mw.log.warn = warn;
+                       restoreWarnings();
 
                        return copy;
                }
                                        // Greetings, mock environment!
                                        mw.config.values = freshConfigCopy( localEnv.config );
                                        mw.messages.values = freshMessagesCopy( localEnv.messages );
+                                       this.suppressWarnings = suppressWarnings;
+                                       this.restoreWarnings = restoreWarnings;
 
                                        localEnv.setup.call( this );
                                },
                                        // Farewell, mock environment!
                                        mw.config.values = liveConfig;
                                        mw.messages.values = liveMessages;
+
+                                       // As a convenience feature, automatically restore warnings if they're
+                                       // still suppressed by the end of the test.
+                                       restoreWarnings();
                                }
                        };
                };
index e5066e0..dab35f6 100644 (file)
@@ -51,6 +51,8 @@
 
                var api = new mw.Api();
 
+               this.suppressWarnings();
+
                api.get( {}, function () {
                        assert.ok( true, 'Function argument treated as success callback.' );
                } );
@@ -67,6 +69,8 @@
                        }
                } );
 
+               this.restoreWarnings();
+
                this.server.respondWith( /action=query/, function ( request ) {
                        request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
                } );
index 41b0cb7..e1fcb6a 100644 (file)
 
        QUnit.test( 'Initial check', 8, function ( assert ) {
                assert.ok( window.jQuery, 'jQuery defined' );
-               assert.ok( window.$, '$j defined' );
-               assert.ok( window.$j, '$j defined' );
+               assert.ok( window.$, '$ defined' );
                assert.strictEqual( window.$, window.jQuery, '$ alias to jQuery' );
+
+               this.suppressWarnings();
+               assert.ok( window.$j, '$j defined' );
                assert.strictEqual( window.$j, window.jQuery, '$j alias to jQuery' );
+               this.restoreWarnings();
 
                assert.ok( window.mediaWiki, 'mediaWiki defined' );
                assert.ok( window.mw, 'mw defined' );