visionmedia / jspec
- Source
- Commits
- Network (13)
- Issues (65)
- Downloads (83)
- Wiki (1)
- Graphs
-
Tag:
0.9.2
| name | age | message | |
|---|---|---|---|
| |
.gitignore | ||
| |
.re/ | ||
| |
History.rdoc | ||
| |
README.rdoc | ||
| |
Rakefile | ||
| |
lib/ | ||
| |
pkg/ | ||
| |
spec/ |
JSpec
JSpec is a minimalistic JavaScript behavior driven development framework, providing simple installation, extremely low learning curve, absolutely no pollution to core prototypes, async request support, and incredibly sexy syntax, tons of matchers and much more.
Features
- Sexiest syntax of them all
- Does not pollute core object prototypes
- Async support (jQuery only at the moment)
- Evalute any number of suites (or cherry pick specific suites loaded)
- Evaluation contexts for providing helper methods and properties
- Allows parens to be optional when using matchers (‘foobar’.should_include ‘foo’)
- Extremely simple matcher declaration
- Several helpful formatters (dom, console, terminal, …)
- Custom formatters
- Profiling
- Tiny (10 kb compressed, 800-ish LOC)
Example
describe 'Shopping cart'
before_each
.cart = new ShoppingCart
end
it 'should add products'
.cart.addProduct('cookie')
.cart.products.should_include 'cookie'
.cart.should_not_be_empty
end
end
Installation
Simply download JSpec and include JSpec.css and JSpec.js in your markup ( head to the downloads section on Github, or clone this public repo). A div with the id of #jspec should be present for displaying when using the default DOM formatter, or you may pass options to report() to alter these default settings (view source code documentation for options).
JSpec scripts should NOT be referenced via the <script> tag, they should be loaded using the exec method. Below is an example:
...
<script>
function runSuites() {
JSpec
.exec('spec.core.js')
.exec('spec.jquery.js')
.run()
.report()
}
</script>
<body onLoad="runSuites()">
...
To run specific suites we first load and evaluate a JSpec script, using the exec() method, then run specific suite(s), and report.
...
function runSuites() {
JSpec
.exec('spec.core.js')
.runSuite('Matchers')
.report()
}
...
You may optionally want to use sources in the /pkg directory for your project, since it includes compressed alternatives. As mentioned throughout the documentation, options may be passed to report(), which are in turn passed to the formatter. This includes options like failuresOnly, which displays only failing suites / specs, etc.
As an alternative to using runSuite() which can take extra time to alter, you may use the query string key ‘suite’.
Options
You may alter the way JSpec operates by assigning options via the JSpec.options object.
- profile {bool} when enabled, uses console.time() in order to display performance information in your console log as specs are completed.
Matchers
* Core
- equal ===
- be_a, be_an have constructor of x
- be_at_least >=
- be_at_most <=
- be_null == null
- be_empty length of 0
- be_true == true
- be_false == false
- be_type be type of x
- be_greater_than >
- be_less_than <
- be_within checks if x is within the range passed
- have_length length of x
- include include substring, array element, or hash key
- match string should match regexp x
- respond_to property x should be a function
- eql, be matches simple literals (strings, numbers) with == .
However composites like arrays or 'hashes' are recursively matched,
meaning that [1, 2, [3]].should_eql([1, 2, [3]]) will be true.
* jQuery
- have_tag, have_one have exactly one tag
- have_tags, have_many have more than one tag
- have_child have exactly one child
- have_children have more than one child
- have_text have plain text
- have_attr have an attribute, with optional value
- have_value
- have_class
- be_visible
- be_hidden
Helpers
* Core - sandbox used to generate new DOM sandbox * jQuery - sandbox used to generate new DOM sandbox, using jQuery object - element same as invoking jQuery, just reads better and no need to worry about $ collisions - elements alias of element
Custom Contexts
Custom contexts can be applied to supply helper methods or properties to all subsequent bodies (other hooks, or specs).
In most cases the default context will suffice, in combination with the ‘this’ keyword. Keep in mind that when replacing the default context you will loose functionality provided by it, unless you manually merge it into your custom context.
To reset the context simply assign null to obtain the original context.
...
before
JSpec.context = { foo : 'bar' }
end
after
JSpec.context = null
end
it 'will work ;)'
this.foo.should_equal 'bar'
end
...
Async Support
Currently only jspec.jquery.js supports async requests. JSpec uses jQuery.ajaxSetup and sets all requests to sync, which preserves execution order, and reports correctly.
it 'should load mah cookies (textfile)'
$.post('async', function(text){
text.should_eql 'cookies!'
})
end
Pre-processor
The pre-processing capability of JSpec is extremely powerful. Your JavaScript code is not necessarily what it seems. For example when you seemingly invoke a object’s prototype like below:
'foobar'.should_include 'bar'
First parens are added:
'foobar'.should_include('bar')
Secondly the matcher invocation is converted:
JSpec.match('foobar', 'should', 'include', 'bar')
This also means instead of:
var object = { foo : 'bar' }
object.should_include 'foo'
We can do:
{ foo : 'bar' }.should_include 'foo'
There is also an alternative closure literal so these are equivalent:
-{ throw 'test' }.should_throw_error
function() { throw 'test' }.should_throw_error
Inclusive range literals are available as well:
n.should_be_within 1..5
Commonly throughout using JSpec you will often need to do things like the following, while referencing ‘this.cart’ throughout your specs:
before
this.cart = new ShoppingCart
end
Thanks to the pre-processor we can simply use the terse alternative below:
before
.cart = new ShoppingCart
end
it '...'
.cart.products.should_have_length 4
end
Formatters
DOM
Clean attractive HTML reporting
Console
Reporting utilizing the console object (tested using Firebug and Safari 4)
Terminal
Coming soon
Misc
- Place the __END__ token in your document to prevent interpretation of the below it code. Useful for quickly debugging or speeding up suites.
- Get the TextMate bundle at github.com/visionmedia/jspec.tmbundle/tree
- For more information consult the JSpec source code documentation or visit visionmedia.github.com/jspec
License
(The MIT License)
Copyright © 2008 - 2009 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


