Testing

Using Karma With RequireJS

Since I was trying to dig deep into AngularJS, I thought I’d give tests a go. Then I ran into Jasmine. And Intern. And Karma. And it wails about no timestamps.

One of my colleagues suggested I have a look at Intern. I did. The comparison table they did was pretty impressive, but after a while a decided I didn’t love it enough:

  • Documentation is poor. Whose is good? Jasmine.
  • And this is a mortifying name for a testing framework! What, are tests only for interns? You wound me good sirs.
  • Which leads to… Bad name for SEO. Intern has other meanings. Though the framework has already been out for quite a while, it’s still hard sifting through piles of totally irrelevant information. Either this, or it’s still severely under-used. Community’s vital too.

For now, let’s Karma + Jasmine.


 

First, install Karma:

1. Install Karma and plugins, locally:

$ sudo npm install karma --save-dev && sudo npm install karma-jasmine karma-chrome-launcher --save-dev

2. If you’re running Karma for the first time, do this to be able to use the command Karma for local Karma npm packages:

npm install -g karma-cli


 

Then, RequireJS. This is when Karma starts complaining:

1. There is no timestamp for…

One thing that stumbled me is the configuration with Requirejs–I thought I was supposed to use the same requireJS configuration file as the one I use in my own app. It isn’t. Karma complaints that “There is no timestamp for… [insert file name]”.

As it turns out, you should exclude the requirejs config file of your app, because “we don’t want to actually start the application in our tests. [LINK]“.

The test-main.js config file is a separate file from the requirejs config file your app uses, which in your case might be config.js or main.js, depending on where you config your requirejs.

They both configures path and dependencies (could be specifying about the same ones), but the former is to provide requirejs support for the tests you write. This whole requirejs setup is a separate one from the requirejs you use in your app. So don’t include the latter, it confuses Karma.

And the link above is a working Karma with its requirejs demo, check it out.


2. WARN [web-server]: 404: /base/dev/vendors/…

Another thing that might cause error: The file including system of Karma.

  1. The order you include files in karma.conf.js matters. It should be the order of their dependence on each other.
  2. You shouldn’t include irrelevant files such as /vendors/**/*.js, because they might cause problems. Only include hand-picked files that are going to be needed in the test.
Standard