Thursday, April 23, 2015

Karma–“browser-is-not-defined” error

(Unit) Testing really made the difference on any project I did. So every tool that can help me simplify or improve my testing experience, is a welcome addition to my tool belt. Recently I started using Karma, a test runner created by the Angular team.

image

From the website:

The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don't have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests.

Karma runs on Node.js and is available as an NPM package. Karma is essentially a tool which spawns a web server that executes source code against test code for each of the browsers connected. The results for each test against each browser are examined and displayed via the command line to the developer such that they can see which browsers and tests passed or failed.

After installing Karma through NPM (npm install karma --save-dev ), the first thing you need to is create a config file(karma init karma.conf.js). This command walks you through a set of questions to generate a config file specific for your application.

image

Next step is starting karma(karma start) to run your (Jasmine) tests. However my tests failed with the following error message:

ReferenceError: browser is not defined

Turns out that to get it working against Angular.js, there is one extra step you need to do. In the frameworks section you have to add ‘ng-scenario’ before ‘jasmine’:

No comments: