diff --git a/src/test.js b/src/test.js index e9828b37c..92dd8bae0 100644 --- a/src/test.js +++ b/src/test.js @@ -52,15 +52,6 @@ Test.prototype = { if ( !config.pollution ) { saveGlobal(); } - if ( config.notrycatch ) { - this.hooks( "beforeEach" ); - return; - } - try { - this.hooks( "beforeEach" ); - } catch ( e ) { - this.pushFailure( "beforeEach failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) ); - } }, run: function() { config.current = this; @@ -91,25 +82,34 @@ Test.prototype = { } }, after: function() { - if ( config.notrycatch ) { - this.hooks( "afterEach" ); - return; - } else { + checkPollution(); + }, + queueHook: function( hook, hookName ) { + var test = this; + return function runHook() { + config.current = test; + if ( config.notrycatch ) { + hook.call( test.testEnvironment, test.assert ); + return; + } try { - this.hooks( "afterEach" ); - } catch ( e ) { - this.pushFailure( "afterEach failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) ); + hook.call( test.testEnvironment, test.assert ); + } catch ( error ) { + test.pushFailure( hookName + " failed on " + test.testName + ": " + ( error.message || error ), extractStacktrace( error, 0 ) ); } - } - checkPollution(); + }; }, hooks: function( handler ) { - if ( QUnit.config[ handler ] ) { - QUnit.config[ handler ].call( this.testEnvironment, this.assert ); + var hooks = []; + + if ( QUnit.objectType( config[ handler ] ) === "function" ) { + hooks.push( this.queueHook( config[ handler ], handler ) ); } - if ( this.moduleTestEnvironment && this.moduleTestEnvironment[ handler ] ) { - this.moduleTestEnvironment[ handler ].call( this.testEnvironment, this.assert ); + if ( this.moduleTestEnvironment && QUnit.objectType( this.moduleTestEnvironment[ handler ] ) === "function" ) { + hooks.push( this.queueHook( this.moduleTestEnvironment[ handler ], handler ) ); } + + return hooks; }, finish: function() { config.current = this; @@ -166,9 +166,15 @@ Test.prototype = { function() { test.before(); }, + + test.hooks( "beforeEach" ), + function() { test.run(); }, + + test.hooks( "afterEach" ).reverse(), + function() { test.after(); }, diff --git a/test/modules.js b/test/modules.js index 0aed3b1de..e8027cefb 100644 --- a/test/modules.js +++ b/test/modules.js @@ -1,12 +1,12 @@ // Before and after each tests QUnit.config.beforeEach = function() { - this.mySetup = true; + this.lastHook = "global-beforeEach"; }; QUnit.config.afterEach = function( assert ) { - if ( this.afterTest ) { - assert.ok( true ); - this.afterTest = false; + if ( this.hooksTest ) { + assert.strictEqual( this.lastHook, "module-afterEach", "Global afterEach runs after module's afterEach" ); + this.hooksTest = false; } if ( this.contextTest ) { @@ -17,31 +17,25 @@ QUnit.config.afterEach = function( assert ) { QUnit.module( "beforeEach/afterEach", { beforeEach: function( assert ) { - assert.ok( true, "beforeEach allow assertions inside" ); - this.myModuleSetup = true; + assert.strictEqual( this.lastHook, "global-beforeEach", "Global beforeEach runs before module's beforeEach" ); + this.lastHook = "module-beforeEach"; }, afterEach: function( assert ) { - if ( this.moduleAfterTest ) { - assert.ok( true ); - this.moduleAfterTest = false; + if ( this.hooksTest ) { + assert.strictEqual( this.lastHook, "test-block", "Module's afterEach runs after current test block" ); + this.lastHook = "module-afterEach"; } } }); -QUnit.test( "before", function( assert ) { - assert.expect( 3 ); - assert.ok( this.mySetup, "global beforeEach method" ); - assert.ok( this.myModuleSetup, "module's afterEach method" ); -}); - -QUnit.test( "after", function( assert ) { - assert.expect( 3 ); +QUnit.test( "hooks order", function( assert ) { + assert.expect( 4 ); - // This will trigger an assertion on the global afterEach - this.afterTest = true; + // This will trigger an assertion on the global and one on the module's afterEach + this.hooksTest = true; - // This will trigger an assertion on the module's afterEach - this.moduleAfterTest = true; + assert.strictEqual( this.lastHook, "module-beforeEach", "Module's beforeEach runs before current test block" ); + this.lastHook = "test-block"; }); QUnit.module( "Test context object", { @@ -52,7 +46,7 @@ QUnit.module( "Test context object", { for ( key in this ) { keys.push( key ); } - assert.deepEqual( keys, [ "helper", "mySetup" ] ); + assert.deepEqual( keys, [ "helper", "lastHook" ] ); }, afterEach: function() {}, helper: function() {}