Skip to content

Commit

Permalink
Test: Module's setup/teardown => beforeEach/afterEach
Browse files Browse the repository at this point in the history
QUnit.module setup and teardown functions are now deprecated as
they were moved to beforeEach/afterEach.

Tests were added to confirm they still work while deprecated.

beforeEach and afterEach are no longer exposed to the test context
as setup/teardown were accidentaly.
  • Loading branch information
leobalter committed Aug 26, 2014
1 parent 9a4c822 commit 982063e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 54 deletions.
14 changes: 13 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,20 @@ QUnit = {
// call on start of module test to prepend name to all tests
module: function( name, testEnvironment ) {
config.currentModule = name;
config.currentModuleTestEnvironment = testEnvironment;
config.modules[ name ] = true;

// DEPRECATED: handles setup/teardown functions,
// beforeEach and afterEach should be used instead
if ( testEnvironment && testEnvironment.setup ) {
testEnvironment.beforeEach = testEnvironment.setup;
delete testEnvironment.setup;
}
if ( testEnvironment && testEnvironment.teardown ) {
testEnvironment.afterEach = testEnvironment.teardown;
delete testEnvironment.teardown;
}

config.currentModuleTestEnvironment = testEnvironment;
},

asyncTest: function( testName, expected, callback ) {
Expand Down
26 changes: 13 additions & 13 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Test( settings ) {
Test.count = 0;

Test.prototype = {
setup: function() {
beforeEach: function() {
if (

// Emit moduleStart when we're switching from one module to another
Expand Down Expand Up @@ -37,11 +37,15 @@ Test.prototype = {

config.current = this;

this.testEnvironment = extend({
setup: function() {},
teardown: function() {}
this.moduleTestEnvironment = extend({
beforeEach: function() {},
afterEach: function() {}
}, this.moduleTestEnvironment );

this.testEnvironment = extend({}, this.moduleTestEnvironment );
delete this.testEnvironment.beforeEach;
delete this.testEnvironment.afterEach;

this.started = now();
runLoggingCallbacks( "testStart", {
name: this.testName,
Expand Down Expand Up @@ -94,7 +98,7 @@ Test.prototype = {
}
}
},
teardown: function() {
afterEach: function() {
config.current = this;
if ( config.notrycatch ) {
this.globalSetup( "afterEach" );
Expand All @@ -103,20 +107,16 @@ Test.prototype = {
try {
this.globalSetup( "afterEach" );
} catch ( e ) {
this.pushFailure( "Teardown failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
this.pushFailure( "afterEach failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
}
}
checkPollution();
},
globalSetup: function( handler ) {
var translate = {
beforeEach: "setup",
afterEach: "teardown"
};
if ( QUnit.config[ handler ] ) {
QUnit.config[ handler ].call( this.testEnvironment, this.assert );
}
this.testEnvironment[ translate[ handler ] ].call( this.testEnvironment, this.assert );
this.moduleTestEnvironment[ handler ].call( this.testEnvironment, this.assert );
},
finish: function() {
config.current = this;
Expand Down Expand Up @@ -169,13 +169,13 @@ Test.prototype = {
function run() {
// each of these can by async
synchronize(function() {
test.setup();
test.beforeEach();
});
synchronize(function() {
test.run();
});
synchronize(function() {
test.teardown();
test.afterEach();
});
synchronize(function() {
test.finish();
Expand Down
4 changes: 2 additions & 2 deletions test/setTimeout.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
QUnit.config.updateRate = 1;

QUnit.module( "Module that mucks with time", {
setup: function() {
beforeEach: function() {
this.setTimeout = window.setTimeout;
window.setTimeout = function() {};
},

teardown: function() {
afterEach: function() {
window.setTimeout = this.setTimeout;
}
});
Expand Down
8 changes: 4 additions & 4 deletions test/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ QUnit.test( "dies on test", function() {
throw new Error( "foo" );
});

// Setup and teardown fail
QUnit.module( "setup/teardown fail", {
setup: function() {
// beforeEach/afterEach fail
QUnit.module( "beforeEach/afterEach fail", {
beforeEach: function() {
throw new Error( "foo" );
},
teardown: function() {
afterEach: function() {
throw new Error( "bar" );
}
});
Expand Down
105 changes: 71 additions & 34 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getPreviousTests( rTestName, rModuleName ) {
return matches;
}

QUnit.test( "module without setup/teardown (default)", function( assert ) {
QUnit.test( "module without beforeEach/afterEach (default)", function( assert ) {
assert.expect( 1 );
assert.ok( true );
});
Expand All @@ -68,18 +68,18 @@ QUnit.test( "expect query and multiple issue", function( assert ) {
assert.ok( true );
});

QUnit.module( "setup test", {
setup: function( assert ) {
QUnit.module( "beforeEach test", {
beforeEach: function( assert ) {
assert.ok( true );
}
});

QUnit.test( "module with setup", function( assert ) {
QUnit.test( "module with beforeEach", function( assert ) {
assert.expect( 2 );
assert.ok( true );
});

QUnit.test( "module with setup, expect in test call", function( assert ) {
QUnit.test( "module with beforeEach, expect in test call", function( assert ) {
assert.expect( 2 );
assert.ok( true );
});
Expand All @@ -88,9 +88,9 @@ QUnit.test( "module with setup, expect in test call", function( assert ) {
if ( typeof document !== "undefined" ) {

QUnit.module( "<script id='qunit-unescaped-module'>'module';</script>", {
setup: function() {
beforeEach: function() {
},
teardown: function( assert ) {
afterEach: function( assert ) {

// We can't use ok(false) inside script tags since some browsers
// don't evaluate script tags inserted through innerHTML after domready.
Expand All @@ -116,12 +116,12 @@ if ( typeof document !== "undefined" ) {
});
}

QUnit.module( "setup/teardown test", {
setup: function( assert ) {
QUnit.module( "beforeEach/afterEach test", {
beforeEach: function( assert ) {
state = true;
assert.ok( true );

// Assert that we can introduce and delete globals in setup/teardown
// Assert that we can introduce and delete globals in beforeEach/afterEach
// without noglobals sounding any alarm.

// Using an implied global variable instead of explicit window property
Expand All @@ -137,7 +137,7 @@ QUnit.module( "setup/teardown test", {
/*@end
@*/
},
teardown: function( assert ) {
afterEach: function( assert ) {
assert.ok( true );

/*@cc_on
Expand All @@ -150,20 +150,20 @@ QUnit.module( "setup/teardown test", {
}
});

QUnit.test( "module with setup/teardown", function( assert ) {
QUnit.test( "module with beforeEach/afterEach", function( assert ) {
assert.expect( 3 );
assert.ok( true );
});

QUnit.module( "setup/teardown test 2" );
QUnit.module( "beforeEach/afterEach test 2" );

QUnit.test( "module without setup/teardown", function( assert ) {
QUnit.test( "module without beforeEach/afterEach", function( assert ) {
assert.expect( 1 );
assert.ok( true );
});

QUnit.module( "Date test", {
setup: function( assert ) {
beforeEach: function( assert ) {
OrgDate = Date;
window.Date = function() {
assert.ok(
Expand All @@ -173,7 +173,7 @@ QUnit.module( "Date test", {
return new OrgDate();
};
},
teardown: function() {
afterEach: function() {
window.Date = OrgDate;
}
});
Expand All @@ -186,13 +186,13 @@ QUnit.test( "sample test for Date test", function( assert ) {
if ( typeof setTimeout !== "undefined" ) {
state = "fail";

QUnit.module( "teardown and stop", {
teardown: function( assert ) {
assert.equal( state, "done", "Test teardown." );
QUnit.module( "afterEach and stop", {
afterEach: function( assert ) {
assert.equal( state, "done", "Test afterEach." );
}
});

QUnit.test( "teardown must be called after test ended", function( assert ) {
QUnit.test( "afterEach must be called after test ended", function( assert ) {
assert.expect( 1 );
QUnit.stop();
setTimeout(function() {
Expand Down Expand Up @@ -226,8 +226,8 @@ QUnit.test( "parameter passed to start decrements semaphore n times", function(
}, 18 );
});

QUnit.module( "async setup test", {
setup: function( assert ) {
QUnit.module( "async beforeEach test", {
beforeEach: function( assert ) {
QUnit.stop();
setTimeout(function() {
assert.ok( true );
Expand All @@ -236,14 +236,14 @@ QUnit.module( "async setup test", {
}
});

QUnit.asyncTest( "module with async setup", function( assert ) {
QUnit.asyncTest( "module with async beforeEach", function( assert ) {
assert.expect( 2 );
assert.ok( true );
QUnit.start();
});

QUnit.module( "async teardown test", {
teardown: function( assert ) {
QUnit.module( "async afterEach test", {
afterEach: function( assert ) {
QUnit.stop();
setTimeout(function() {
assert.ok( true );
Expand All @@ -252,7 +252,7 @@ QUnit.module( "async teardown test", {
}
});

QUnit.asyncTest( "module with async teardown", function( assert ) {
QUnit.asyncTest( "module with async afterEach", function( assert ) {
assert.expect( 2 );
assert.ok( true );
QUnit.start();
Expand Down Expand Up @@ -310,10 +310,10 @@ QUnit.test( "test synchronous calls to stop", function( assert ) {
}

QUnit.module( "save scope", {
setup: function() {
beforeEach: function() {
this.foo = "bar";
},
teardown: function( assert ) {
afterEach: function( assert ) {
assert.deepEqual( this.foo, "bar" );
}
});
Expand Down Expand Up @@ -741,7 +741,7 @@ QUnit.test( "running test name displayed", function( assert ) {
};

QUnit.module( "timing", {
setup: function() {
beforeEach: function() {
if ( delayNextSetup ) {
delayNextSetup = false;
sleep( 250 );
Expand Down Expand Up @@ -770,7 +770,7 @@ QUnit.test( "running test name displayed", function( assert ) {
"Fast runtime for trivial test"
);
assert.ok( parseInt( slow.lastChild.previousSibling.innerHTML, 10 ) > 250,
"Runtime includes setup"
"Runtime includes beforeEach"
);
});
})();
Expand Down Expand Up @@ -926,13 +926,18 @@ QUnit.config.afterEach = function( assert ) {
assert.ok( true );
this.afterTest = false;
}

if ( this.contextTest ) {
assert.ok( true );
this.contextTest = false;
}
};

QUnit.module( "beforeEach/afterEach", {
setup: function() {
beforeEach: function() {
this.myModuleSetup = true;
},
teardown: function( assert ) {
afterEach: function( assert ) {
if ( this.moduleAfterTest ) {
assert.ok( true );
this.moduleAfterTest = false;
Expand All @@ -943,7 +948,7 @@ QUnit.module( "beforeEach/afterEach", {
QUnit.test( "before", function( assert ) {
assert.expect( 2 );
assert.ok( this.mySetup, "global beforeEach method" );
assert.ok( this.myModuleSetup, "module's teardown method" );
assert.ok( this.myModuleSetup, "module's afterEach method" );
});

QUnit.test( "after", function( assert ) {
Expand All @@ -952,10 +957,42 @@ QUnit.test( "after", function( assert ) {
// This will trigger an assertion on the global afterEach
this.afterTest = true;

// This will trigger an assertion on the module's teardown
// This will trigger an assertion on the module's afterEach
this.moduleAfterTest = true;
});

QUnit.module( "Test context object", {
beforeEach: function( assert ) {
var key,
keys = [];

for ( key in this ) {
keys.push( key );
}
assert.deepEqual( keys, [ "helper", "mySetup" ] );
},
afterEach: function() {},
helper: function() {}
});

QUnit.test( "keys", function( assert ) {
assert.expect( 2 );
this.contextTest = true;
});

QUnit.module( "Deprecated setup/teardown", {
setup: function() {
this.deprecatedSetup = true;
},
teardown: function( assert ) {
assert.ok( this.deprecatedSetup );
}
});

QUnit.test( "before/after order", function( assert ) {
assert.expect( 1 );
});

// Get a reference to the global object, like window in browsers
}( (function() {
return this;
Expand Down

0 comments on commit 982063e

Please sign in to comment.