Twitter GitHub Facebook Instagram

Daniel Irvine on building software

CoffeeScript in Rails, the TDD way

20 July 2014

Here’s a quick summary of CoffeeScript development with Rails.

CoffeeScript is not executed directly but is instead transpiled to JavaScript first. This transpilation must be done not only when the app is deployed but also when tests are run.

The Rails asset pipeline takes care of transpilation during app deployment. Teaspoon takes care of transpilation for specs.

For Vim usage, you’ll need vim-coffee-script for its syntax highlighting at least. It also registers the CoffeeScript file type.

Teaspoon for running CoffeeScript specs

Vim plus Tmux is a great way to run tests without having to leave Vim. Teaspoon is easy to setup in this environment. After installing Teaspoon, I created the file ~/.vim/ftplugin/coffee.vim with the following line:

map <buffer> <leader>r :w <CR> :call VimuxRunCommand(clear; rake teaspoon)

This allows me to run all of my tests within the Rails solution. (Soon I’ll figure out how to run just one test and add it in here, too.)

The Teaspoon Quick Start Walkthrough is a great guide for writing your first CoffeeScript tests.

The asset pipeline

I didn’t have this set up in my Rails project, so I’ve had to add in the various pieces manually. To get both the asset pipeline and Teaspoon running, all of the following dependencies must be added to the Gemfile.

gem 'sprockets-rails'
gem 'coffee-rails'
gem 'uglifier'
gem 'teaspoon'
gem 'turbolinks'

Then the asset pipeline must be enabled for each Rails environment. So for example, in config/environments/development.rb I added the following two lines.

config.assets.debug = true
config.assets.enabled = true

That should be enough to get started.

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram