background preloader

UnitTesting

Facebook Twitter

How to Test a Website in Different Browsers: Internet Explorer, Firefox, Safari, Chrome. Which JavaScript Test Library Should You Use? QUnit vs Jasmine vs Mocha. Whether you’re writing javaScript for the browser or for nodeJS, the question exists: what unit test library should I use to ensure my javascript code is working as expected? There are plenty to choose from, and several that are popular. If you were considering jUnit, Jasmine, or Mocha, then I’ve got some information you might be interested in: the good, the bad, and the ugly. qUnit is definitely the oldest of the three on my list, having its first official release in 2008.

Because of this, it has picked up a good userbase over the years. How does it stack up? Pros: Lots of support across the board, from Q&A to CI server support Cons: Lacks fluent syntaxConfiguration is a headache, and must constantly be maintainedMakes including 3rd party libraries (like assertion libraries) relatively difficultAsynchronous testing can be a bit of a headacheNo baked-in headless run support Jasmine is slightly newer to the pack, having been released 2 years after qUnit in 2010. How’s it look? Pros: Cons: Unit Test Your JavaScript Using Mocha and Chai. This article was peer reviewed by Panayiotis «pvgr» Velisarakos, Mark Brown and Tom Greco. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be! Have you ever made some changes to your code, and later found it caused something else to break?

I’m sure most of us have. This is almost inevitable, especially when you have a larger amount of code. One thing depends on another, and then changing it breaks something else as a result. But what if that didn’t happen? What if you had a way of knowing when something breaks as a result of some change? That’s where unit tests shine. In this article, I’ll show you how to get started unit testing your JavaScript code. The code for this tutorial is available from our GitHub repo. What Is Unit Testing When you test your codebase, you take a piece of code — typically a function — and verify it behaves correctly in a specific situation.

In practice, tests can sometimes be more complex. Setting up the Tools <! Below: A Journey Through Client-Side Testing with JavaScript -Telerik Developer Network. Having tests is important. They allow us to extend and refactor our code with ease. Many developers follow test driven development as a workflow. I believe that writing tests makes software development much more interesting and generally leads to better code. Well-designed and tested systems are easier to maintain. Over the last few years, developers have begun to put a lot of application logic in the browser. We started writing more and more JavaScript. The Testing Setup Let’s discuss the types of tools that make testing possible. Testing framework The framework consists of functions like suite, describe, test or it. Describe('Testing user management', function () { it('should register a new user', function(done) { }); it('should remove a user', function(done) { });}); We split the logic of our application in blocks.

Popular testing frameworks for JavaScript are QUnit, Jasmine or Mocha. Assertion Library We use assertion libraries to perform the actual checks. Expect(5).to.be.a('number'); <! Introduction To JavaScript Unit Testing.