Back to blog home

Testing Javascript: Get started with Jasmine

Javascript has come a long way since its humble beginnings of making things underlined when a cursor passes over them. The 'modern' website uses a whole bunch of libraries and tools to provide both snappy UX and connectivity-tolerant usage, allowing both mobile and desktop users far more usability than ever before. Such a new dependency on sites to elevate Javascript to a first-class citizen in the application comes with a proportional need to test that everything works in all the browsers the audience is using. Tools like Jasmine have played a big part in Javascript’s maturation, allowing software to be written with greater certainty. This article will introduce unit testing, show you how to set up Jasmine, and cover how automation with Karma can streamline your work.

Jasmine allows you to define behaviour specifications, or specs for short, for your software and tests to determine whether your software meets your specification. The whole process is automated, meaning that any tests you write with Jasmine are tests that you no longer have to spend time conducting manually. You can run these tests whenever you like, giving you feedback for the smallest of code changes. The more you automate, the less work you have to do. The more you test, the greater insight you have into changes you make to your code.

Why Jasmine?

Jasmine follows the principles of Behaviour Driven Development, or BDD for short, which starts with behavioural specifications as the basis for all your tests. If you use user stories in your software development process, behaviour specifications will look familiar to you as they contain natural language definitions of the behaviours you are testing for. Dan North, creator of BDD, has an excellent introductory article, and I recommend it as background reading if this is a new topic for you.

Jasmine’s web-based test runner also makes it a great fit for testing JS written specifically for the web. If you’re considering also writing tests for Node.JS, you might be interested in an alternative JavaScript testing framework such as Mocha.