Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Karma with Rails asset pipeline?

I would like to use the Karma test runner in my AngularJS + Rails project. Has anyone integrated them successfully? More specifically, I'm interested to know how to integrate with the asset pipeline (I have files with extension .coffee.erb, which would need to be preprocessed twice).

I use Karma version 0.10.1 and Rails 4.

Any help / examples would be appreciated.

like image 956
Misha Moroshko Avatar asked Aug 15 '13 00:08

Misha Moroshko


People also ask

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What is require_ tree in Rails?

js file. The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.

What is assets precompile in Rails?

By default, Rails uses CoffeeScript for JavaScript and SCSS for CSS. DHH has a great introduction during his keynote for RailsConf. The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots.

What is assets precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .


2 Answers

I have been struggling with marking Karma aware of Sprockets in my rails app, and manage to come with a solution. You need a rake task to get the list of assets declare in your application.js, and inject them in the files array of your Karma configuration file.

In my rake task I have this:

sprockets = Rails.application.assets
sprockets.append_path Rails.root.join("spec/karma")
files = Rails.application.assets.find_asset("application_spec.js").to_a.map {|e| e.pathname.to_s }

along with my karma configuration file:

files: [
  APPLICATION_SPEC,
  'app/assets/javascripts/angular/*/*.{coffee,js}',
  'spec/javascripts/**/*_spec.{coffee,js}'
]

where I replace APPLICATION_SPEC with all the sprocket files. I wrote a tutorial if you want the big picture.

like image 63
Sébastien Saunier Avatar answered Sep 22 '22 14:09

Sébastien Saunier


I haven't integrated them as such, but for my project I've chosen to go the other way, and serve each entirely separately. So I've created a rails app and an angular app in separate directories, then I've sym-linked the angular app into the public directory of the rails app so that the rails server will serve up the angular scripts and templates.

My logic in doing it this way was that I didn't want two different pre-processors in the same set of code, and I wasn't confident that the testing tools would play nice with each other. I've also chosen to use the build scripts that came with ng-boilerplate, and those scripts are doing quite a bit of work that otherwise would be done by rake or other tools.

I've been writing a tutorial series based around that, it can be found here: http://technpol.wordpress.com/2013/08/11/rails-app-for-angularjs-simple-application/

like image 40
PaulL Avatar answered Sep 23 '22 14:09

PaulL