Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Apr 24, 2011

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

This comment has been minimized.

Copy link

commented Jun 2, 2011

thanks for this!

@gregmalcolm

This comment has been minimized.

Copy link

commented Aug 17, 2011

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

This comment has been minimized.

Copy link
Owner Author

commented Aug 17, 2011

Thank you, this is very useful!

@curiousily

This comment has been minimized.

Copy link

commented May 6, 2012

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

commented Jun 13, 2012

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
You can’t perform that action at this time.