Skip to content

airportyh/jspec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSpec

JSpec is a minimalistic JavaScript behavior driven development framework, providing simple installation, usage, and extremely low learning curve.

Features

  • Sexiest syntax of them all

  • Evalute or load any number of suites

  • Several helpful formatters

  • Custom formatters

  • Evaluation contexts for providing helper methods and properties

  • Allows parens to be optional when using matchers (‘foobar’.should_include ‘foo’)

  • Extremely simple matcher declaration

Installation

Simply download JSpec and include JSpec.css and JSpec.js in your markup. A div with the id of #jspec should be present for displaying when using the default formatter.

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, then run any number of specific suites, and report.

...
function runSuites() {
  JSpec
  .eval(JSpec.load('test.js'))
  .runSuite(JSpec.suites['Matchers'])
  .run()
  .report()
}
...

Examples

describe 'Shopping cart'

  before each
    this.cart = new ShoppingCart
  end

  it 'should add products'
    this.cart.addProduct('cookie')
    this.cart.products.should_include('cookie')
    this.cart.empty.should_be_false
  end

end

Matchers

* Core

- eql, be          ==
- equal            ===
- be_a, be_an      have constructor of x
- be_at_least      >=
- be_at_most       <=
- be_empty         length of 0
- be_true          == true
- be_false         == false
- be_type          be type of x
- be_greater_than  >
- be_less_than     <
- 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

* jQuery

- have_tag       have exactly one tag   
- have_tags      have more than one tag
- have_child     have exactly one child
- have_children  have more than one child
- have_text      have plain text
- 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

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.

...
before
  JSpec.context = { foo : 'bar' }
end

it 'will work ;)'
  this.foo.should_equal 'bar'
end
...

Misc

  • _END_

    • Place the _END_ token in your document to prevent evaluation of the following code. Useful for quickly debugging or speeding up suites.

Todo

  • add pre-processing to prevent pollution of prototypes

  • JSpec exceptions .. evaluations .. better debugging

  • add formatter hooks to display spinner etc

  • finish terminal usage

  • nesting

  • clean up Rakefile

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.

About

The simplest, sexiest, highly extensible, framework / DOM independent JavaScript BDD framework

Resources

Stars

Watchers

Forks

Packages

No packages published