jasmine/README.markdown

477 lines
15 KiB
Markdown
Raw Normal View History

2009-03-01 13:47:02 +00:00
Jasmine
2008-12-03 03:59:13 +00:00
=======
2008-12-04 00:23:17 +00:00
**YET ANOTHER JavaScript testing framework**
2008-12-03 03:59:13 +00:00
2009-09-10 01:46:29 +00:00
Quick Start
----------
### Ruby Suite Running
sudo gem sources -a http://gems.github.com
2010-02-20 19:08:45 +00:00
sudo gem install geminstaller
2009-09-10 01:46:29 +00:00
git clone git://github.com/pivotal/jasmine.git
2009-10-21 16:28:34 +00:00
cd jasmine
2010-02-20 19:08:45 +00:00
sudo geminstaller
2009-10-21 16:28:34 +00:00
cd examples/ruby
2009-09-10 01:46:29 +00:00
rake jasmine_server
open `http://localhost:8888/` in your favorite browser.
### HTML Suite Running
2010-01-04 22:57:05 +00:00
[Get the latest release from the downloads page](http://github.com/pivotal/jasmine/downloads)
2009-09-10 01:46:29 +00:00
2010-02-20 19:08:45 +00:00
open `example/example_runner.html` in your favorite browser
2009-09-10 01:46:29 +00:00
### Automatic Suite Running (w/ Selenium)
sudo gem sources -a http://gems.github.com
2010-02-20 19:08:45 +00:00
sudo gem install geminstaller
2009-09-10 01:46:29 +00:00
git clone git://github.com/pivotal/jasmine.git
2009-10-21 16:28:34 +00:00
cd jasmine
sudo geminstaller
cd examples/ruby
2009-09-10 01:46:29 +00:00
rake test:ci
Releases
----------
2010-01-04 22:57:05 +00:00
0.10.0 [[download]](http://cloud.github.com/downloads/pivotal/jasmine/jasmine-0.10.0.zip)
2009-09-10 01:46:29 +00:00
2009-10-16 06:27:19 +00:00
0.9.0 [[download]](http://github.com/pivotal/jasmine/zipball/0.9.0)
2009-09-10 01:46:29 +00:00
0.8.0 [[download]](http://github.com/pivotal/jasmine/zipball/0.8.0)
### Which Release Should I Use?
Please use the latest version unless you have a good reason not to. Some of this documentation may not be applicable to older versions.
2009-10-25 00:06:42 +00:00
Pull Requests
----------
We welcome your contributions! Jasmine is currently maintained by Davis Frank ([infews](http://github.com/infews)), Rajan Agaskar ([ragaskar](http://github.com/ragaskar)), and Christian Williams ([Xian](http://github.com/Xian)). You can help us by removing all other recipients from your pull request.
2009-09-10 01:46:29 +00:00
2008-12-04 00:23:17 +00:00
Why Another Frickin' JS TDD/BDD Framework?
2008-12-03 03:59:13 +00:00
-----------
2009-03-01 13:47:02 +00:00
There are some situations when you want to test-drive JavaScript, but you don't want to be bothered with or even have an explicit document. You have no DOM to work with and thus lack HTML elements on which to hang event handlers. You may need to make asynchronous calls (say, to an AJAX API) and cannot mock/stub them.
2008-12-03 03:59:13 +00:00
But you still need to write tests.
What's an Agile Engineer to do?
Enter Jasmine
------------
2008-12-04 00:23:17 +00:00
Jasmine is yet another JavaScript testing framework. It's *heavily* influenced by JSSpec, ScrewUnit & [JSpec](http://github.com/visionmedia/jspec/tree/master), which are all influenced by RSpec. But each of those was lacking in some way: JSSpec & ScrewUnit require a DOM. JSpec's DOM-less assumption was a great start, but it needed asynchronous support.
2008-12-03 03:59:13 +00:00
So we started over. And TDD'd a whole new framework. Enjoy.
How To
------
2008-12-04 00:23:17 +00:00
There is a nice example of how to use Jasmine in the /example directory. But here's more information.
2008-12-03 03:59:13 +00:00
2009-03-01 13:34:39 +00:00
Exciting changes are afoot and many syntax changes have been made to make Jasmine more usable. Please read the examples below for updates.
2008-12-04 00:23:17 +00:00
### Specs
2008-12-03 03:59:13 +00:00
2009-03-01 14:55:39 +00:00
Each spec is, naturally, a JavaScript function. You tell Jasmine about this spec with a call to `it()` with a string and the function. The string is a description that will be helpful to you when reading a report.
2008-12-03 03:59:13 +00:00
2008-12-04 00:23:17 +00:00
it('should be a test', function () {
2010-02-20 19:08:45 +00:00
var foo = 0
2009-03-01 14:55:39 +00:00
foo++;
2008-12-04 00:23:17 +00:00
});
2008-12-03 03:59:13 +00:00
2008-12-04 00:23:17 +00:00
### Expectations
2008-12-03 03:59:13 +00:00
2009-03-01 14:55:39 +00:00
Within your spec you will want/need to make expectations. These are made with the `expect()` funciton and expectation matchers. like this:
2008-12-03 03:59:13 +00:00
2008-12-04 00:23:17 +00:00
it('should be a test', function () {
2009-03-01 14:55:39 +00:00
var foo = 0
foo++;
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
expect(foo).toEqual(1);
2008-12-04 00:23:17 +00:00
});
2008-12-03 03:59:13 +00:00
2008-12-04 00:23:17 +00:00
Results of the expectations are logged for later for reporting.
2008-12-03 03:59:13 +00:00
2009-03-01 14:55:39 +00:00
#### Expectation Matchers
Jasmine has several built-in matchers. Here are a few:
>`expect(x).toEqual(y);` compares objects or primitives `x` and `y` and passes if they are equivalent
>`expect(x).toMatch(pattern);` compares `x` to string or regular expression `pattern` and passes if they match
>`expect(x).toBeDefined();` passes if `x` is not `undefined`
>`expect(x).toBeNull();` passes if `x` is not `null`
>`expect(x).toBeTruthy();` passes if `x` evaluates to true
>`expect(x).toBeFalsy();` passes if `x` evaluates to false
>`expect(x).toContain(y);` passes if array or string `x` contains `y`
2009-03-01 14:55:39 +00:00
#### Writing New Matchers
2009-03-01 14:55:39 +00:00
We've provided a small set of matchers that cover many common situations. However, we recommend that you write custom matchers when you want to assert a more specific sort of expectation. Custom matchers help to document the intent of your specs, and can help to remove code duplication in your specs.
2009-03-01 14:55:39 +00:00
It's extremely easy to create new matchers for your app. A matcher function receives the actual value as `this.actual`, and zero or more arguments may be passed in the function call. The function should return `true` if the actual value passes the matcher's requirements, and `false` if it does not.
2009-03-01 14:55:39 +00:00
Here's the definition of `toBeLessThan()`:
2009-03-01 14:55:39 +00:00
toBeLessThan: function(expected) {
return this.actual < expected;
2010-02-20 19:08:45 +00:00
};
2009-03-01 14:55:39 +00:00
To add the matcher to your suite, call `this.addMatchers()` from within a `before` or `it` block. Call it with an object mapping matcher name to function:
beforeEach(function() {
this.addMatchers({
toBeVisible: function() { return this.actual.isVisible(); }
});
});
2009-03-01 14:55:39 +00:00
2009-03-01 13:34:39 +00:00
### Asynchronous Specs
2008-12-03 03:59:13 +00:00
2009-03-01 14:55:39 +00:00
You may be thinking, "That's all well and good, but you mentioned something about asynchronous tests."
2008-12-03 03:59:13 +00:00
2009-03-01 13:34:39 +00:00
Well, say you need to make a call that is asynchronous - an AJAX API, or some other JavaScript library. That is, the call returns immediately, yet you want to make expectations 'at some point in the future' after some magic happens in the background.
2008-12-04 00:23:17 +00:00
2009-03-01 13:34:39 +00:00
Jasmine allows you to do this with `runs()` and `waits()` blocks.
2008-12-04 00:23:17 +00:00
2009-03-01 13:47:02 +00:00
`runs()` blocks by themselves simply run as if they were called directly. The following snippets of code should provide similar results:
2009-03-01 13:34:39 +00:00
it('should be a test', function () {
2009-03-01 14:55:39 +00:00
var foo = 0
foo++;
2008-12-04 00:23:17 +00:00
2009-03-01 14:55:39 +00:00
expect(foo).toEqual(1);
2009-03-01 13:34:39 +00:00
});
2009-03-01 13:47:02 +00:00
and
2009-03-01 13:34:39 +00:00
it('should be a test', function () {
runs( function () {
2009-03-01 14:55:39 +00:00
var foo = 0
foo++;
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(foo).toEqual(1);
2009-03-01 13:34:39 +00:00
});
});
2009-03-01 13:47:02 +00:00
multiple `runs()` blocks in a spec will run serially. For example,
2008-12-04 00:23:17 +00:00
2009-03-01 13:34:39 +00:00
it('should be a test', function () {
runs( function () {
2009-03-01 14:55:39 +00:00
var foo = 0
foo++;
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(foo).toEqual(1);
2009-03-01 13:34:39 +00:00
});
runs( function () {
2009-03-01 14:55:39 +00:00
var bar = 0
bar++;
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(bar).toEqual(1);
2009-03-01 13:34:39 +00:00
});
});
2008-12-04 00:23:17 +00:00
2009-03-01 14:55:39 +00:00
`runs()` blocks share functional scope -- `this` properties will be common to all blocks, but declared `var`'s will not!
2008-12-04 00:23:17 +00:00
2009-03-01 13:34:39 +00:00
it('should be a test', function () {
runs( function () {
2009-03-01 14:55:39 +00:00
this.foo = 0
this.foo++;
2009-03-01 13:34:39 +00:00
var bar = 0;
bar++;
2009-03-01 14:55:39 +00:00
expect(this.foo).toEqual(1);
expect(bar).toEqual(1);
2009-03-01 13:34:39 +00:00
});
runs( function () {
this.foo++;
2009-03-01 14:55:39 +00:00
var bar = 0
bar++;
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(foo).toEqual(2);
expect(bar).toEqual(1);
2009-03-01 13:34:39 +00:00
});
});
2009-03-01 13:47:02 +00:00
`runs()` blocks exist so you can test asynchronous processes. The function `waits()` works with `runs()` to provide a naive
timeout before the next block is run. You supply a time to wait before the next `runs()` function is executed. For example:
2008-12-04 00:23:17 +00:00
2010-02-20 19:08:45 +00:00
it('should be a test', function () {
2009-03-01 14:55:39 +00:00
runs(function () {
this.foo = 0;
var that = this;
setTimeout(function () {
that.foo++;
}, 250);
});
2009-03-01 14:55:39 +00:00
runs(function () {
2009-06-28 20:36:51 +00:00
this.expects(this.foo).toEqual(0);
2009-03-01 14:55:39 +00:00
});
2009-03-01 14:55:39 +00:00
waits(500);
2009-03-01 14:55:39 +00:00
runs(function () {
2009-06-28 20:36:51 +00:00
this.expects(this.foo).toEqual(1);
2009-03-01 14:55:39 +00:00
});
2010-02-20 19:08:45 +00:00
});
2008-12-04 00:23:17 +00:00
2009-03-01 13:47:02 +00:00
What's happening here?
2008-12-04 00:23:17 +00:00
2009-03-01 13:47:02 +00:00
* The first call to `runs()` sets call for 1/4 of a second in the future that increments `this.foo`.
* The second `runs()` is executed immediately and then verifies that `this.foo` was indeed initialized to zero in the previous `runs()`.
* Then we wait for half a second.
2008-12-04 00:23:17 +00:00
* Then the last call to `runs()` expects that `this.foo` was incremented by the `setTimeout`.
2009-03-01 13:34:39 +00:00
2008-12-04 00:23:17 +00:00
### Suites
Specs are grouped in Suites. Suites are defined using the global `describe()` function:
2009-03-01 13:47:02 +00:00
2010-02-20 19:08:45 +00:00
describe('One suite', function () {
it('has a test', function () {
...
});
2010-02-20 19:08:45 +00:00
it('has another test', function () {
...
});
});
2008-12-03 03:59:13 +00:00
2009-03-01 14:55:39 +00:00
The Suite name is so that reporting is more descriptive.
2008-12-04 00:23:17 +00:00
2009-03-01 14:55:39 +00:00
Suites are executed in the order in which `describe()` calls are made, usually in the order in which their script files are included. Additionally, specs within a suite share a functional scope. So you may declare variables inside a describe block and they are accessible from within your specs. For example:
2008-12-04 00:23:17 +00:00
2010-02-20 19:08:45 +00:00
describe('A suite with some variables', function () {
2009-03-01 14:55:39 +00:00
var bar = 0
2010-02-20 19:08:45 +00:00
2009-03-01 14:55:39 +00:00
it('has a test', function () {
2010-02-20 19:08:45 +00:00
bar++;
2009-03-01 14:55:39 +00:00
expect(bar).toEqual(1);
2010-02-20 19:08:45 +00:00
});
2009-03-01 13:47:02 +00:00
2010-02-20 19:08:45 +00:00
it('has another test', function () {
bar++;
2009-03-01 14:55:39 +00:00
expect(bar).toEqual(2);
2010-02-20 19:08:45 +00:00
});
});
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
#### beforeEach
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
A suite can have a beforeEach declaration. It takes a function that is run before each spec. For example:
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
describe('some suite', function () {
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
var suiteWideFoo;
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
beforeEach(function () {
suiteWideFoo = 1;
});
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
it('should equal bar', function () {
expect(suiteWideFoo).toEqual(1);
});
2009-03-01 14:55:39 +00:00
});
2009-03-01 13:47:02 +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:
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']);
});
});
2009-03-01 14:55:39 +00:00
#### afterEach
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
Similarly, there is an afterEach declaration. It takes a function that is run after each spec. For example:
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
describe('some suite', function () {
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
var suiteWideFoo;
afterEach(function () {
suiteWideFoo = 0;
});
2008-12-04 00:23:17 +00:00
2009-03-01 14:55:39 +00:00
it('should equal 1', function () {
expect(suiteWideFoo).toEqual(1);
});
2009-03-01 13:47:02 +00:00
2009-03-01 14:55:39 +00:00
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']);
2009-03-01 14:55:39 +00:00
};
});
2009-03-01 13:34:39 +00:00
2009-05-29 03:27:43 +00:00
### Nested Describes
2009-06-28 20:36:51 +00:00
Jasmine supports nested describes. An example:
2009-05-29 03:27:43 +00:00
describe('some suite', function () {
var suiteWideFoo;
beforeEach(function () {
suiteWideFoo = 0;
});
describe('some nested suite', function() {
var nestedSuiteBar;
beforeEach(function() {
nestedSuiteBar=1;
2010-02-20 19:08:45 +00:00
});
2009-05-29 03:27:43 +00:00
it('nested expectation', function () {
expect(suiteWideFoo).toEqual(0);
expect(nestedSuiteBar).toEqual(1);
});
2010-02-20 19:08:45 +00:00
2009-05-29 03:27:43 +00:00
});
it('top-level describe', function () {
expect(suiteWideFoo).toEqual(0);
expect(nestedSuiteBar).toEqual(undefined);
});
2009-05-29 03:27:43 +00:00
});
2009-03-01 14:55:39 +00:00
### Spies
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
Jasmine integrates 'spies' that permit many spying, mocking, and faking behaviors.
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
Here are a few examples:
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
var Klass = function () {
};
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
var Klass.prototype.method = function (arg) {
return arg;
};
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
var Klass.prototype.methodWithCallback = function (callback) {
return callback('foo');
};
2010-02-20 19:08:45 +00:00
2009-03-01 14:55:39 +00:00
...
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
it('should spy on Klass#method') {
spyOn(Klass, 'method');
Klass.method('foo argument');
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(Klass.method).wasCalledWith('foo argument');
});
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
it('should spy on Klass#methodWithCallback') {
var callback = Jasmine.createSpy();
Klass.method(callback);
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
expect(callback).wasCalledWith('foo');
});
2008-12-04 00:23:17 +00:00
Spies can be very useful for testing AJAX or other asynchronous behaviors that take callbacks by faking the method firing an async call.
var Klass = function () {
};
var Klass.prototype.asyncMethod = function (callback) {
someAsyncCall(callback);
};
...
it('should test async call') {
spyOn(Klass, 'asyncMethod');
var callback = Jasmine.createSpy();
Klass.asyncMethod(callback);
expect(callback).wasNotCalled();
var someResponseData = 'foo';
Klass.asyncMethod.mostRecentCall.args[0](someResponseData);
expect(callback).wasCalledWith(someResponseData);
});
2009-03-01 14:55:39 +00:00
There are spy-specfic matchers that are very handy.
2009-03-01 14:55:39 +00:00
`wasCalled()` returns true if the object is a spy and was called
2009-03-01 14:55:39 +00:00
`wasCalledWith(arguments)` returns true if the object is a spy and was called with the passed arguments
2009-03-01 14:55:39 +00:00
`wasNotCalled()` returns true if the object is a spy and was not called
2009-03-01 14:55:39 +00:00
`wasNotCalledWith(arguments)` returns true if the object is a spy and was not called with the passed arguments
2009-03-01 13:34:39 +00:00
2009-07-08 07:07:29 +00:00
Spies can be trained to respond in a variety of ways when invoked:
2009-03-01 14:55:39 +00:00
`andCallThrough()`: spies on AND calls the original function spied on
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
`andReturn(arguments)`: returns passed arguments when spy is called
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
`andThrow(exception)`: throws passed exception when spy is called
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
`andCallFake(function)`: calls passed function when spy is called
2009-03-01 13:34:39 +00:00
2009-07-08 07:07:29 +00:00
Spies have some useful properties:
2009-03-01 14:55:39 +00:00
`callCount`: returns number of times spy was called
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
`mostRecentCall.args`: returns argument array from last call to spy.
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
`argsForCall[i]` returns arguments array for call `i` to spy.
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
Spies are automatically removed after each spec. They may be set in the beforeEach function.
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
### Disabling Tests & Suites
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
Specs may be disabled by calling `xit()` instead of `it()`. Suites may be disabled by calling `xdescribe()` instead of `describe()`. A simple find/replace in your editor of choice will allow you to run a subset of your specs.
2009-03-01 13:34:39 +00:00
2009-03-01 14:55:39 +00:00
## Support
We now have a Google Group for support & discussion.
* Homepage: [http://groups.google.com/group/jasmine-js](http://groups.google.com/group/jasmine-js)
* Group email: [jasmine-js@googlegroups.com](jasmine-js@googlegroups.com)
2010-01-04 22:57:05 +00:00
* Current build status of Jasmine is visible at [ci.pivotallabs.com](http://ci.pivotallabs.com)
2009-03-01 14:55:39 +00:00
2008-12-04 00:23:17 +00:00
## Maintainers
2009-09-10 01:46:29 +00:00
* [Davis W. Frank](mailto:dwfrank@pivotallabs.com), Pivotal Labs
* [Rajan Agaskar](mailto:rajan@pivotallabs.com), Pivotal Labs
2010-01-28 03:19:30 +00:00
* [Christian Williams](mailto:xian@pivotallabs.com), Pivotal Labs
2008-12-04 00:23:17 +00:00
## Acknowledgments
* A big shout out to the various JavaScript test framework authors, especially TJ for [JSpec](http://github.com/visionmedia/jspec/tree/master) - we played with it a bit before deciding that we really needed to roll our own.
* Thanks to Pivot [Jessica Miller](http://www.jessicamillerworks.com/) for our fancy pass/fail/pending icons
2010-01-28 03:19:30 +00:00
* Huge contributions have been made by [Erik Hanson](mailto:erik@pivotallabs.com), [Adam Abrons](mailto:adam@pivotallabs.com) and [Carl Jackson](mailto:carl@pivotallabs.com), and many other Pivots.