Skip to content

Instantly share code, notes, and snippets.

@netzpirat
Created November 12, 2010 10:42
Show Gist options
  • Save netzpirat/673967 to your computer and use it in GitHub Desktop.
Save netzpirat/673967 to your computer and use it in GitHub Desktop.
Continuous CoffeeScript testing with Guard and Jasmine

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

gem 'guard'
gem 'guard-coffeescript'
gem 'guard-livereload'
gem 'jasmine'
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
src_dir: public/javascripts
src_files:
- underscore.js
- compiled/math.js
spec_dir: spec/javascripts
spec_files:
- **/*_spec.js
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
#
# 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('')
@chrishomer
Copy link

Thanks for this. I had to change one thing in my Guardfile:

guard 'livereload', :apply_js_live => false do

instead of

guard 'livereload' do

@starkcoffee
Copy link

thanks for this!

@gregmalcolm
Copy link

In case this helps anyone, I've followed the instructions on a fresh rails app and dropped it into github:

https://github.com/gregmalcolm/guard-jasmine-coffeescript

Thanks netzpirat!

@netzpirat
Copy link
Author

Thank you, this is very useful!

@curiousily
Copy link

I setup a repo that might be a good example of a starting point if you aren't using rails
https://github.com/bapplz/hit-it

@khr128
Copy link

khr128 commented Jun 13, 2012

bundle exec jasmine init creates spec/javascripts/support/jasmine.yml file that you can edit
bundle exec rake jasmine recommends to remove jasmine.rake. Yes, it can and should be removed.

@netzpirat
Copy link
Author

Folks, I do not use this setup anymore, I've created Guard::Jasmine to run my specs in the console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment