No commit activity in last 3 years
No release in over 3 years
This gem will compile your js manifest in a specific location and the run Karma tests.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.10.0

Runtime

~> 4.0
 Project Readme

RailsKarmaSprockets

This gem will help you to integrate Rails (Sprockets) and Karma (test runner for Angular.js app)

The problem

If you are using Rails, probably you are using Sprockets, the asset pipeline by default. And if you are building your Angular.js app on top of Rails/Sprockets, then you'll have a manifest file where you glue together the Javascript files needed.

Unfortunately for us, Karma can't process those manifests files automatically. Karma require us to set the files where the app's code live.

One solution is to set the files with the server's URL, i.e.

files: [
  'http://localhost:3000/assets/application.js'
]

But that would mean that we need to have a server running while running tests. Adding unnecessary complexity to the task.

Solution

This gem will compile your manifests, to use them specifically on your Karma test and run Karma for you. You just need to run a generator, set up a couple of configuration options and then run a rake task every time you want run your Karma's tests.

Usage

Important Note

You will need to have Karma installed on your project, I recommend you to install it as a node module, that's documented here. If you installed Karma globally or on a different path, you will be able to set up this on a configuration file that is generated by the gem.

Steps

  1. Include the gem in your Gemfile:
  group :development, :test do
    gem 'rails_karma_sprockets'
  end
  1. Install the new gem:
  $ bundle install
  1. Run the generator to create the configuration file:
  $ bundle exec rails g rails_karma_sprockets:init
  1. Update the config/initializers/karma_sprockets to your needs:
  RailsKarmaSprockets.configure do |c|
    c.manifests :manifest1_name, :manifest2_name
    c.karma = {
      cmd: '/custom/path/to/karma start' # Default is: ./node_modules/karma/bin/karma start
      opts: '--single-run=false'   # Custom options for Karma runner.
    }
  end
  1. Add the following file to your karma.conf.js files array:
  'tmp/karma/compiled_application.js'

i.e.

  // Karma example configuration file

  module.exports = function(config) {
    config.set({
      basePath: '',
      frameworks: ['jasmine'],

      files: [
        'vendor/assets/javascripts/**/*.js', // Any js asset that's not included on your manifests.
        'node_modules/angular-mocks/angular-mocks.js', // angular-mocks.js is needed by default, I installed it as a node module, you can have it on vendor too.
        'tmp/karma/compiled_application.js', // This asset is the most important, it contains your manifests code compiled. In other words, it is your Angular app.
        'spec/javascripts/**/*_spec.js' // Your js specs/tests.
      ]
    });
  }
  1. Run the following rake task every time you want to run your tests:

      $ bundle exec rake karma:run

Contributing

  1. Fork it (https://github.com/efigarolam/rails_karma_sprockets/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request