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.
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.
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.
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.
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 .
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With