Skip to content

efigarolam/rails_karma_sprockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Makes your integration between Rails and Karma easier.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published