Warning: This is a bit hack-ish because it depends on your development server running while you run your tests. :)
Set Up Karma
package.json, which will tell
npmwhat to install for you:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
unit.coffee, the configs for Karma:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
run, a bash script to start the runner easily:
1 2 3 4
$ chmod +x spec/karma/run.
.gitignoreso you’re not pushing around tons of Node modules with your project.
- Install Karma locally with
npm install spec/karma.
You should be able to start the runner now with
$ spec/karma/run. It will open a Chrome window if it’s working.
Write a Spec
I put my Batman.js specs in
spec/batman with names corresponding to their location in
app/assets/batman. You can do it however you want, but make sure you’re loading the right files with
unit.coffee above. Open up your first spec file, maybe
spec/batman/test_spec.coffee, and put a jasmine spec in it:
1 2 3 4 5 6 7
If those pass, you’re in business!
Batman.TestCase is a Batman extra, you’ll need to include it in your project yourself. An easy way to do that is to include the
src/extras/ directory from Github in your Rails app. For example, in
/app/assets/batman/extras. Now, in your development server, visit
/assets/extras/batman.test_case.js. Do you see the code for
Now, just add that path to
1 2 3 4 5 6 7
And if you want, make sure it’s loaded with a jasmine spec:
1 2 3
- Did you make
runexecutable by with
$ chmod +x spec/karma/run?
- This is kind of a hacky setup – it depends on your development server (either
$ rails serveror Pow) running. Is it?
filesshould list your tests, but it should also list compiled JS assets from your development server, including
http://and so on. Check the paths there in your browser. Do they contain everything you expect them to contain? You might need to add files to that list or add sprockets directives to those files so that they
Ok, well it ain’t perfect but it works. Hope it helps!