public
Last active — forked from netzpirat/0_README.md

Continuous CoffeeScript testing with Guard and Jasmine

  • Download Gist
0_README.md
Markdown

Continuous CoffeeScript testing with Guard and Jasmine

This Gist shows how to set up a Rails project to practice BDD with CoffeeScript, Guard and Jasmine. You can see this setup in action on Vimeo

  • Install Gems with Bundler with bundle install
  • Define your guards with mate Guardfile
  • Initialize Jasmine with bundle exec jasmine init
  • Configure Jasmine with mate spec/support/yasmine.ym
  • Start Guard with bundle exec guard
  • Start Jasmine with bundle exec rake jasmine
  • Open your browser with open http://localhost:8888
  • Press the LiveReload button
  • Write your specs (example app_coffeescripts_math.coffee goes to app/coffeescripts/math.coffee)
  • See the Growl notifications
  • Write your code (example spec_coffeescripts_math_spec.coffee goes to app/coffeescripts/math_spec.coffee)
  • Watch the Jasmine report

That's it

1_Gemfile
1 2 3 4
gem 'guard'
gem 'guard-coffeescript'
gem 'guard-livereload'
gem 'jasmine'
2_Guardfile
1 2 3 4 5 6 7 8 9 10 11 12
guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch('^app/coffeescripts/(.*)\.coffee')
end
 
guard 'coffeescript', :output => 'spec/javascripts' do
watch('^spec/coffeescripts/(.*)\.coffee')
end
 
guard 'livereload' do
watch('^spec/javascripts/.+\.js$')
watch('^public/javascripts/compiled/.+\.js$')
end
3_jasmine.yml
YAML
1 2 3 4 5 6 7 8 9 10
src_dir: public/javascripts
 
src_files:
- underscore.js
- compiled/math.js
 
spec_dir: spec/javascripts
 
spec_files:
- **/*_spec.js
4_math_spec.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
describe 'Math:', ->
describe 'fib()', ->
it 'should calculate the numbers correctly up to fib(16)', ->
fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
expect(Math.fib(i)).toEqual fib[i] for i in [0..16]
 
describe 'uuid()', ->
it 'should have the proper UUID format', ->
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{13}/
 
it 'should have always the numer 4 at position 14', ->
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
 
it 'should generate a unique uuid for 1000 generated uuids at least', ->
uuids = []
counter = 0
 
while counter < 1000
uuids.push Math.uuid()
counter++
 
expect(uuids.length).toEqual _.uniq(uuids).length
5_math.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#
# Calculate the fibonacci numbers
#
# Warning: This script can go really slow if you try to calculate the number with n > 20
#
Math.fib = (n) ->
s = 0
return s if n == 0
if n == 1
s += 1
else
Math.fib(n - 1) + Math.fib(n - 2)
 
 
#
# Generate a RFC 4122 GUID
#
# See section 4.4 (Algorithms for Creating a UUID from Truly Random or
# Pseudo-Random Numbers) for generating a GUID, since we don't have
# hardware access within JavaScript.
#
# More info:
#
# - http://www.rfc-archive.org/getrfc.php?rfc=4122
# - http://www.broofa.com/2008/09/javascript-uuid-function/
# - http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
#
Math.uuid = ->
 
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
uuid = new Array(36)
random = 0
 
for digit in [0..36]
switch digit
when 8, 13, 18, 23
uuid[digit] = '-'
when 14
uuid[digit] = '4'
else
random = 0x2000000 + (Math.random() * 0x1000000) | 0 if (random <= 0x02)
r = random & 0xf
random = random >> 4
uuid[digit] = chars[if digit == 19 then (r & 0x3) | 0x8 else r]
 
uuid.join('')

I had to modify your livereload guard clause to get it to update my browser automatically

guard 'livereload', :apply_js_live => false do

instead of

guard 'livereload' do

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.