Skip to content

Commit

Permalink
Test: Synchronize hooks order
Browse files Browse the repository at this point in the history
Fixes #647
  • Loading branch information
leobalter committed Sep 4, 2014
1 parent 1ab0eef commit 103c75a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
50 changes: 28 additions & 22 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -166,9 +166,15 @@ Test.prototype = {
function() {
test.before();
},

test.hooks( "beforeEach" ),

function() {
test.run();
},

test.hooks( "afterEach" ).reverse(),

function() {
test.after();
},
Expand Down
38 changes: 16 additions & 22 deletions test/modules.js
Original file line number Diff line number Diff line change
@@ -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 ) {
Expand All @@ -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", {
Expand All @@ -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() {}
Expand Down

0 comments on commit 103c75a

Please sign in to comment.