jasmine/before-and-after.html.md

102 lines
2.5 KiB
Markdown
Raw Normal View History

---
layout: default
title: Before and After a Jasmine Spec
---
2010-09-18 07:02:58 +00:00
#### beforeEach
A suite can have a `beforeEach()` declaration. It takes a function that is run before each spec. For example:
2010-09-22 07:25:34 +00:00
{% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
beforeEach(function () {
suiteWideFoo = 1;
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should equal bar', function () {
expect(suiteWideFoo).toEqual(1);
});
});
{% endhighlight %}
2010-09-18 07:02:58 +00:00
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:
2010-09-22 07:25:34 +00:00
{% highlight javascript %}
var runnerWideFoo = [];
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
beforeEach(function () {
runnerWideFoo.push('runner');
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
describe('some suite', function () {
beforeEach(function () {
runnerWideFoo.push('suite');
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should equal bar', function () {
expect(runnerWideFoo).toEqual(['runner', 'suite']);
});
});
{% endhighlight %}
2010-09-18 07:02:58 +00:00
#### afterEach
Similarly, there is an `afterEach()` declaration. It takes a function that is run after each spec. For example:
2010-09-22 07:25:34 +00:00
{% highlight javascript %}
describe('some suite', function () {
var suiteWideFoo;
afterEach(function () {
suiteWideFoo = 0;
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should equal 1', function () {
expect(suiteWideFoo).toEqual(1);
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should equal 0 after', function () {
expect(suiteWideFoo).toEqual(0);
};
});
{% endhighlight %}
2010-09-18 07:02:58 +00:00
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:
2010-09-22 07:25:34 +00:00
{% highlight javascript %}
var runnerWideFoo = [];
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
afterEach(function () {
runnerWideFoo.push('runner');
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
describe('some suite', function () {
afterEach(function () {
runnerWideFoo.push('suite');
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should be empty', function () {
expect(runnerWideFoo).toEqual([]);
});
2010-09-18 07:02:58 +00:00
2010-09-22 07:25:34 +00:00
it('should be populated after', function () {
expect(runnerWideFoo).toEqual(['suite', 'runner']);
};
});
{% endhighlight %}
2010-09-18 07:02:58 +00:00
### 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.
2010-09-22 07:25:34 +00:00
{% highlight javascript %}
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");
});
{% endhighlight %}