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
- Include the gem in your Gemfile:
group :development, :test do
gem 'rails_karma_sprockets'
end
- Install the new gem:
$ bundle install
- Run the generator to create the configuration file:
$ bundle exec rails g rails_karma_sprockets:init
- 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
- 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.
]
});
}
-
Run the following rake task every time you want to run your tests:
$ bundle exec rake karma:run
Contributing
- Fork it (https://github.com/efigarolam/rails_karma_sprockets/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request