Skip to content

Commit

Permalink
Test: Synchronize hooks order
Browse files Browse the repository at this point in the history
The 'synchronize' functions now also accepts arrays of functions

Fixes #647
Closes #650
  • Loading branch information
JamesMGreene committed Sep 4, 2014
1 parent a9aca10 commit 76ff48c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
6 changes: 6 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ function sourceFromStacktrace( offset ) {
}

function synchronize( callback, last ) {
if ( QUnit.objectType( callback ) === "array" ) {
while ( callback.length ) {
synchronize( callback.shift() );
}
return;
}
config.queue.push( callback );

if ( config.autorun && !config.blocking ) {
Expand Down
78 changes: 43 additions & 35 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,26 +82,34 @@ Test.prototype = {
}
},
after: function() {
config.current = this;
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 @@ -161,19 +160,28 @@ Test.prototype = {
test = this;

function run() {

// each of these can by async
synchronize(function() {
test.before();
});
synchronize(function() {
test.run();
});
synchronize(function() {
test.after();
});
synchronize(function() {
test.finish();
});
synchronize([
function() {
test.before();
},

test.hooks( "beforeEach" ),

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

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

function() {
test.after();
},
function() {
test.finish();
}
]);
}

// `bad` initialized at top of scope
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 76ff48c

Please sign in to comment.