Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Understand Jasmine testing framework setup and teardown by examples.
// 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
You can’t perform that action at this time.