97 lines
2.5 KiB
Markdown
97 lines
2.5 KiB
Markdown
---
|
|
layout: default
|
|
title: Before and After a Jasmine Spec
|
|
---
|
|
|
|
#### beforeEach
|
|
|
|
A suite can have a `beforeEach()` declaration. It takes a function that is run before each spec. For example:
|
|
|
|
describe('some suite', function () {
|
|
|
|
var suiteWideFoo;
|
|
|
|
beforeEach(function () {
|
|
suiteWideFoo = 1;
|
|
});
|
|
|
|
it('should equal bar', function () {
|
|
expect(suiteWideFoo).toEqual(1);
|
|
});
|
|
});
|
|
|
|
A runner can also have `beforeEach()` declarations. Runner `beforeEach()` functions are executed before every spec in all suites, and execute BEFORE suite `beforeEach()` functions. For example:
|
|
|
|
var runnerWideFoo = [];
|
|
|
|
beforeEach(function () {
|
|
runnerWideFoo.push('runner');
|
|
});
|
|
|
|
describe('some suite', function () {
|
|
|
|
beforeEach(function () {
|
|
runnerWideFoo.push('suite');
|
|
});
|
|
|
|
it('should equal bar', function () {
|
|
expect(runnerWideFoo).toEqual(['runner', 'suite']);
|
|
});
|
|
});
|
|
|
|
#### afterEach
|
|
|
|
Similarly, there is an `afterEach()` declaration. It takes a function that is run after each spec. For example:
|
|
|
|
describe('some suite', function () {
|
|
|
|
var suiteWideFoo;
|
|
afterEach(function () {
|
|
suiteWideFoo = 0;
|
|
});
|
|
|
|
it('should equal 1', function () {
|
|
expect(suiteWideFoo).toEqual(1);
|
|
});
|
|
|
|
it('should equal 0 after', function () {
|
|
expect(suiteWideFoo).toEqual(0);
|
|
};
|
|
});
|
|
|
|
A runner can also have an `afterEach()` declarations. Runner `afterEach()` functions are executed after every spec in all suites, and execute AFTER suite `afterEach()` functions. For example:
|
|
|
|
var runnerWideFoo = [];
|
|
|
|
afterEach(function () {
|
|
runnerWideFoo.push('runner');
|
|
});
|
|
|
|
describe('some suite', function () {
|
|
|
|
afterEach(function () {
|
|
runnerWideFoo.push('suite');
|
|
});
|
|
|
|
it('should be empty', function () {
|
|
expect(runnerWideFoo).toEqual([]);
|
|
});
|
|
|
|
it('should be populated after', function () {
|
|
expect(runnerWideFoo).toEqual(['suite', 'runner']);
|
|
};
|
|
});
|
|
|
|
### Single-spec After functions
|
|
|
|
A spec may ask Jasmine to execute some code after the spec has finished running; the code will run whether the spec finishes successfully or not. Multiple after functions may be given.
|
|
|
|
describe('some suite', function () {
|
|
it(function () {
|
|
var originalTitle = window.title;
|
|
this.after(function() { window.title = originalTitle; });
|
|
MyWindow.setTitle("new value");
|
|
expect(window.title).toEqual("new value");
|
|
});
|
|
|