Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Implement universal module setup/teardown #635

Merged
merged 3 commits into from
Sep 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ grunt.registerTask( "test-on-node", function() {

require( "./test/logs" );
require( "./test/test" );
require( "./test/modules" );
require( "./test/deepEqual" );
require( "./test/globals" );

Expand Down
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
35 changes: 21 additions & 14 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() {
before: function() {
if (

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

config.current = this;

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

this.started = now();
runLoggingCallbacks( "testStart", {
Expand All @@ -53,13 +52,13 @@ Test.prototype = {
saveGlobal();
}
if ( config.notrycatch ) {
this.testEnvironment.setup.call( this.testEnvironment, this.assert );
this.hooks( "beforeEach" );
return;
}
try {
this.testEnvironment.setup.call( this.testEnvironment, this.assert );
this.hooks( "beforeEach" );
} catch ( e ) {
this.pushFailure( "Setup failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
this.pushFailure( "beforeEach failed on " + this.testName + ": " + ( e.message || e ), extractStacktrace( e, 0 ) );
}
},
run: function() {
Expand Down Expand Up @@ -94,20 +93,28 @@ Test.prototype = {
}
}
},
teardown: function() {
after: function() {
config.current = this;
if ( config.notrycatch ) {
this.testEnvironment.teardown.call( this.testEnvironment, this.assert );
this.hooks( "afterEach" );
return;
} else {
try {
this.testEnvironment.teardown.call( this.testEnvironment, this.assert );
this.hooks( "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();
},
hooks: function( handler ) {
if ( QUnit.config[ handler ] ) {
QUnit.config[ handler ].call( this.testEnvironment, this.assert );
}
if ( this.moduleTestEnvironment && this.moduleTestEnvironment[ handler ] ) {
this.moduleTestEnvironment[ handler ].call( this.testEnvironment, this.assert );
}
},
finish: function() {
config.current = this;
if ( config.requireExpects && this.expected === null ) {
Expand Down Expand Up @@ -159,13 +166,13 @@ Test.prototype = {
function run() {
// each of these can by async
synchronize(function() {
test.setup();
test.before();
});
synchronize(function() {
test.run();
});
synchronize(function() {
test.teardown();
test.after();
});
synchronize(function() {
test.finish();
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script src="../dist/qunit.js"></script>
<script src="test.js"></script>
<script src="dump.js"></script>
<script src="modules.js"></script>
<script src="deepEqual.js"></script>
<script src="globals.js"></script>
<script src="swarminject.js"></script>
Expand Down
216 changes: 216 additions & 0 deletions test/modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Before and after each tests
QUnit.config.beforeEach = function() {
this.mySetup = true;
};

QUnit.config.afterEach = function( assert ) {
if ( this.afterTest ) {
assert.ok( true );
this.afterTest = false;
}

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

QUnit.module( "beforeEach/afterEach", {
beforeEach: function( assert ) {
assert.ok( true, "beforeEach allow assertions inside" );
this.myModuleSetup = true;
},
afterEach: function( assert ) {
if ( this.moduleAfterTest ) {
assert.ok( true );
this.moduleAfterTest = false;
}
}
});

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 );

// This will trigger an assertion on the global afterEach
this.afterTest = true;

// 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( "afterEach and QUnit.stop", {
beforeEach: function() {
this.state = false;
},
afterEach: function( assert ) {
assert.strictEqual( this.state, true, "Test afterEach." );
}
});

QUnit.test( "afterEach must be called after test ended", function( assert ) {
var testContext = this;
assert.expect( 1 );
QUnit.stop();
setTimeout(function() {
testContext.state = true;
QUnit.start();
}, 13 );
});

QUnit.test( "parameter passed to stop increments semaphore n times", function( assert ) {
var testContext = this;
assert.expect( 1 );
QUnit.stop( 3 );
setTimeout(function() {
QUnit.start();
QUnit.start();
}, 13 );
setTimeout(function() {
testContext.state = true;
QUnit.start();
}, 15 );
});

QUnit.test( "parameter passed to start decrements semaphore n times", function( assert ) {
var testContext = this;
assert.expect( 1 );
QUnit.stop();
QUnit.stop();
QUnit.stop();
setTimeout(function() {
testContext.state = true;
QUnit.start( 3 );
}, 18 );
});

QUnit.module( "async beforeEach test", {
beforeEach: function( assert ) {
QUnit.stop();
setTimeout(function() {
assert.ok( true );
QUnit.start();
}, 500 );
}
});

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

QUnit.module( "async afterEach test", {
afterEach: function( assert ) {
QUnit.stop();
setTimeout(function() {
assert.ok( true );
QUnit.start();
}, 500 );
}
});

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

QUnit.module( "save scope", {
beforeEach: function() {
this.foo = "bar";
},
afterEach: function( assert ) {
assert.deepEqual( this.foo, "bar" );
}
});

QUnit.test( "scope check", function( assert ) {
assert.expect( 2 );
assert.deepEqual( this.foo, "bar" );
});

QUnit.module( "simple testEnvironment setup", {
foo: "bar",
// example of meta-data
bugid: "#5311"
});

QUnit.test( "scope check", function( assert ) {
assert.deepEqual( this.foo, "bar" );
});

QUnit.test( "modify testEnvironment", function( assert ) {
assert.expect( 0 );
this.foo = "hamster";
});

QUnit.test( "testEnvironment reset for next test", function( assert ) {
assert.deepEqual( this.foo, "bar" );
});

QUnit.module( "testEnvironment with object", {
options: {
recipe: "soup",
ingredients: [ "hamster", "onions" ]
}
});

QUnit.test( "scope check", function( assert ) {
assert.deepEqual( this.options, {
recipe: "soup",
ingredients: [ "hamster", "onions" ]
});
});

QUnit.test( "modify testEnvironment", function( assert ) {
assert.expect( 0 );

// since we only do a shallow copy, nested children of testEnvironment can be modified
// and survice
this.options.ingredients.push( "carrots" );
});

QUnit.test( "testEnvironment reset for next test", function( assert ) {
assert.deepEqual( this.options, {
recipe: "soup",
ingredients: [ "hamster", "onions", "carrots" ]
}, "Is this a bug or a feature? Could do a deep copy" );
});

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 );
});
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
Loading