Created
June 10, 2014 02:42
-
-
Save realguess/71b89263ebb11521cf49 to your computer and use it in GitHub Desktop.
Understand Jasmine testing framework setup and teardown by examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup and Teardown | |
// ================== | |
// | |
// Jasmine [Setup and Teardown] examples. | |
// | |
// [Setup and Teardown]: http://jasmine.github.io/2.0/introduction.html#section-Setup_and_Teardown | |
// Specific to Firefox only. | |
function watchHandler(prop, oldVal, newVal) { | |
console.log(prop + ' changed from ' + oldVal + ' to ' + newVal); | |
return newVal; | |
} | |
describe('Without "beforeEach"', function () { | |
var obj = {}; | |
var prop = 'foo1'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
obj[prop] += 1; | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
}); | |
it(prop + ' should not be equal to 2', function () { | |
expect(obj[prop]).not.toBe(2); | |
}); | |
}); | |
describe('With "beforeEach"', function () { | |
var obj = {}; | |
var prop = 'foo2'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
beforeEach(function () { | |
obj[prop] += 1; // Should be invoked twice. | |
}); | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
}); | |
it(prop + ' should be equal to 2', function () { | |
expect(obj[prop]).toBe(2); | |
}); | |
}); | |
describe('Without "beforeEach" but updating in spec', function () { | |
var obj = {}; | |
var prop = 'foo3'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
obj[prop] += 1; | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
obj[prop] += 1; | |
}); | |
it(prop + ' should be equal to 2', function () { | |
expect(obj[prop]).toBe(2); | |
}); | |
}); | |
describe('With "beforeEach" and updating in spec', function () { | |
var obj = {}; | |
var prop = 'foo4'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
beforeEach(function () { | |
obj[prop] += 1; | |
}); | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
obj[prop] += 1; | |
}); | |
it(prop + ' should be equal to 3', function () { | |
expect(obj[prop]).toBe(3); | |
}); | |
}); | |
describe('Reset with "afterEach"', function () { | |
var obj = {}; | |
var prop = 'foo5'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
beforeEach(function () { | |
obj[prop] += 1; | |
}); | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
obj[prop] += 1; | |
}); | |
it(prop + ' should still be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
}); | |
// Teardown | |
afterEach(function () { | |
obj[prop] = 0; | |
}); | |
}); | |
describe('Reverse the order of "beforeEach" and "afterEach"', function () { | |
var obj = {}; | |
var prop = 'foo6'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Teardown | |
afterEach(function () { | |
obj[prop] = 0; | |
}); | |
it(prop + ' should be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
obj[prop] += 1; | |
}); | |
it(prop + ' should still be equal to 1', function () { | |
expect(obj[prop]).toBe(1); | |
}); | |
// Setup | |
beforeEach(function () { | |
obj[prop] += 1; | |
}); | |
}); | |
describe('Async with "beforeEach" and "afterEach"', function () { | |
var obj = {}; | |
var prop = 'foo7'; | |
obj[prop] = 0; | |
obj.watch(prop, watchHandler); // Firefox only | |
// Setup | |
beforeEach(function (done) { | |
setTimeout(function () { | |
obj[prop] += 1; | |
done(); | |
}, 100); | |
}); | |
it(prop + ' should be equal to 1', function (done) { | |
setTimeout(function () { | |
expect(obj[prop]).toBe(1); | |
obj[prop] += 1; | |
done(); | |
}, 100); | |
}); | |
it(prop + ' should still be equal to 1', function (done) { | |
setTimeout(function () { | |
expect(obj[prop]).toBe(1); | |
done(); | |
}, 100); | |
}); | |
// Teardown | |
afterEach(function () { | |
setTimeout(function () { | |
obj[prop] = 0; | |
}, 100); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment