AlignMinds Technologies logo

Why Testing with Jasmine is Fun?

What is Jasmine?

Jasmine is a behaviour-driven development framework, used for testing JavaScript code. To know about behaviour-driven development, we must know about test-driven development.

Test-driven development, as per definition, is a software development process that relies on the repetition of a very short development cycle: first, the developer writes an automated test case that defines a desired improvement or new function, then produces a minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.

Behaviour-driven development, on the other hand, is a software development process that is emerged from test-driven development. The behaviour-driven development process can be called as a combination of general techniques and principles of test-driven development.

Why use Jasmine for testing?

Jasmine is an independent software as it does not depend on any other software development frameworks. It does not require a Document Object Model (DOM). A basic advantage that can be called of Jasmine is that its syntax is so obvious that it’s easy to understand. It also helps you to write your tests easily.

Working with Jasmine

Function

Let’s start with an example code that you want to test using Jasmine. We all know, in every programming language, we start with a Hello World program. Here also, let’s begin with a helloworld.js. A JavaScript function that returns “Hello World”.

Spec

Initially, you need to grab the latest standalone version of Jasmine on your computer. They are easily available on Google. All you need to do is search for it and download it. Unzip the downloaded file. The /src and /spec directories will have many files in it. You’ll have to empty them out as they are just examples which you probably won’t require.

Now, the function or let’s call the helloworld.js file should be put into the /src directory. We now have created the src. What we must do next is to create the spec.

The code has two parts.

The ‘describe’ part and the ‘it’ part. The ‘describe’ part will contain the main codes or functions that do the tests. ‘Describe’ is followed by the suite, which is just the English language and not any code, which helps to understand what it describes. Inside of ‘describe’, you have the ‘it’ part of the code. ‘It’ part is generally called as a ‘spec’.

The whole code might look like classes written in a function. ‘It’ describes what the code must do in general English language and in JavaScript code. You can have any number of specs in a suite.

Matchers

When a code is needed to be tested, you will definitely require a matcher. A matcher is something that will do the checking whether your code provides the required output. For beginners, we can use expect() and toEqual() as matchers. In our Hello World example program, we need the code to return the expected output – “Hello World”.

To test this, the matchers will run as expect (helloworld()).toEqual(“Hello World”); If that comes true, the program is successfully tested with no errors reported. There are many other matchers too. Matchers are selected according to the requirement of what is to be tested and what should be returned. In addition to this, you can make your own matchers too.

Example code

describe(“The ‘toEqual’ matcher”, function() { it(“works for simple literals and variables”, function() { var a = 12; expect(a).toEqual(12); }); it(“should work for objects”, function() { var foo = { a: 12, b: 34 }; var bar = { a: 12, b: 34 }; expect(foo).toEqual(bar); }); }); it(“The ‘toMatch’ matcher is for regular expressions”, function() { var message = “foo bar baz”; expect(message).toMatch(/bar/); expect(message).toMatch(“bar”); expect(message).not.toMatch(/quux/); }); it(“The ‘toBeDefined’ matcher compares against `undefined`”, function() { var a = { foo: “foo” }; expect(a.foo).toBeDefined(); expect(a.bar).not.toBeDefined(); }); it(“The `toBeUndefined` matcher compares against `undefined`”, function() { var a = { foo: “foo” }; expect(a.foo).not.toBeUndefined(); expect(a.bar).toBeUndefined(); }); it(“The ‘toBeNull’ matcher compares against null”, function() { var a = null; var foo = “foo”; expect(null).toBeNull(); expect(a).toBeNull(); expect(foo).not.toBeNull(); }); it(“The ‘toBeTruthy’ matcher is for boolean casting testing”, function() { var a, foo = “foo”; expect(foo).toBeTruthy(); expect(a).not.toBeTruthy(); }); it(“The ‘toBeFalsy’ matcher is for boolean casting testing”, function() { var a, foo = “foo”; expect(a).toBeFalsy(); expect(foo).not.toBeFalsy(); }); it(“The ‘toContain’ matcher is for finding an item in an Array”, function() { var a = [“foo”, “bar”, “baz”]; expect(a).toContain(“bar”); expect(a).not.toContain(“quux”); }); it(“The ‘toBeLessThan’ matcher is for mathematical comparisons”, function() { var pi = 3.1415926, e = 2.78; expect(e).toBeLessThan(pi); expect(pi).not.toBeLessThan(e); }); it(“The ‘toBeGreaterThan’ matcher is for mathematical comparisons”, function() { var pi = 3.1415926, e = 2.78; expect(pi).toBeGreaterThan(e); expect(e).not.toBeGreaterThan(pi); }); it(“The ‘toBeCloseTo’ matcher is for precision math comparison”, function() { var pi = 3.1415926, e = 2.78; expect(pi).not.toBeCloseTo(e, 2); expect(pi).toBeCloseTo(e, 0); }); it(“The ‘toThrow’ matcher is for testing if a function throws an exception”, function() { var foo = function() { return1 + 2; }; var bar = function() { return a + 1; }; expect(foo).not.toThrow(); expect(bar).toThrow(); }); it(“The ‘toThrowError’ matcher is for testing a specific thrown exception”, function() { var foo = function() { thrownew TypeError(“foo bar baz”); }; expect(foo).toThrowError(“foo bar baz”); expect(foo).toThrowError(/bar/); expect(foo).toThrowError(TypeError); expect(foo).toThrowError(TypeError, “foo bar baz”); }); });

(Code was taken from http://jasmine.github.io/2.0/introduction.html)

Some advantages of using Jasmine

  • Jasmine is independent. It does not depend on any other JavaScript frameworks.
  • It does not require a DOM.
  • It has a clean, obvious syntax.
  • Help maintainers understand the intention behind the code.
  • Brings validation and proper data handling concerns to the forefront.

Conclusion

There is plenty more you can do with Jasmine. Overall, Jasmine makes your testing fun. So, if you’re not yet into testing, now is an excellent time to start your JavaScript testing. It makes your testing pretty simple with Jasmine’s fast and simple syntax.

– Shekhar R

JavaScript: Evolution, Dominance and the Future

JavaScript is the popular choice for frontend developers. Web development is ever-changing and if we take the list of programming language that has kept up with the latest trends, JavaScript will rank in the first position in the list.

Why JavaScript?

JavaScript was mainly developed as a client-side scripting language to be used across multiple web browsers in various web applications. JavaScript has the inbuilt behaviour that lets developer validate each field as the user inputs data in it and provide immediate feedback for incorrect entries without needing to load another web page to process their request.

Using JavaScript, you can provide an immediate response to various actions taken by the user without the need to load new web pages to respond. This interpreted programming language also helps web pages to be more interactive using animations without involving forms. You can even load new images, objects, or scripts into the web page without reloading the entire page.

JavaScript can efficiently handle requests being passed to the server and interpret results coming from the server. Most common areas where JavaScript can be used are Form Validation, Alerts, Image rollovers, advertising pop-ups and AutoComplete feature. With the advent of Node.js framework, JavaScript has become even more powerful and is widely used for server-side and networking applications.

Origin of JS

JavaScript originally known as Mocha was developed in May 1995 by Brendan Eich, then working at Netscape. The name Mocha was chosen by Marc Andreessen, founder of Netscape.  In September, the name was changed to LiveScript and in December, upon receiving Trademark License from Sun and for marketing reasons, the name was changed to JavaScript.

In 1996-1997, JavaScript was handed over to ECMA (European Computer Manufacturers Association) to set standards that could be used by other vendors. Series of standards were applied to this language and ECMAScript 3 was introduced in 1999, which is the baseline for today’s JavaScript.

While all these events happening, in 2005, with the combined efforts by Open Source Developer Communities, JavaScript got a new touch by the introduction of a new concept called AJAX –set of technologies, using which data can be loaded in the background without the need to reload the entire page resulting in more dynamic applications. This resulted in the popularity of JavaScript and several libraries such as Prototype, jQuery, Dojo, MooTools, etc were released.

In July 2008, various browser vendors and other interested parties met to hammer out a new language based on JavaScript. Work began on a proposal called ECMA4 and ultimately got renamed to ECMA 5 and got standardized pretty quickly. As soon as ECMA5 was finalized, work began around code called Harmony. Harmony brought some basic changes to ECMAScript and closed a few of the open gaps in functionality. Main goals of Harmony included improve language for writing complex application and libraries used by those applications, keep versioning as simple as possible and adopt de facto standards (as much as possible).

Popularity of JavaScript

The different website provides a different ranking of JavaScript based on different measures like popularity, Search Index, Code hosted etc.

Tiobe Programming Community Index has rated JavaScript at 11th position based on the language’s popularity compared to other popular languages indicating only 1.37% internet searches were made for JavaScript language. Github Code Sharing site indicates that JavaScript is the most popular language accounting for 21% of JavaScript code hosted on the site.  PYPL Popularity of Programming Language index has ranked JavaScript at 7th position.

JavaScript is more about client-side scripting language. JavaScript code can be written using any text editor (like TextEdit) and just a supporting browser is needed to run the code. Such simplicity of code is really useful for beginners to write their code. With the advanced framework of JavaScript called Node.js (server-side), it can now be used to develop an entire website.

Different Framework of JS/Popularity of JS

Some of the popular JS Framework are Node.js, Angular JS, Backbone.js, CanJS, Ember.js, SproutCore, Knockout.js, Spine, Meteor.js, Yahoo!Mojito, etc.

Node.js

Node.js is a cross-platform run-time environment for server-side networking applications. Node.js Framework is gaining high adoption rate as it can now be used to develop real-time applications. Big Giants like Microsoft, PayPal, Walmart, LinkedIn, SAP etc. are popularly using Node.js to create websites.

Node.js was created and published by Ryan Dahl in 2009. It was initially developed only to run on Linux, but after his presentation at JSConf EU Conference, it gained momentum, the package manager for Node.js libraries-npm was developed in 2011. In June 2011, Microsoft partnered with Joyent (where Dahl worked while creating Node.js) and released first build to support Windows in the month of July 2011.

JavaScript has no unified API for I/O providing ability to build modern I/O interface in its best way and this was considered the main factor that Ryan chose JavaScript to define series of asynchronous, event-driven I/O. The major advantage of Node.js is that it can keep alive many socket connections alive and can accept a request for new connections as well.

Node.js application runs in single-threaded environment; multi-threaded execution also supported in Node.js 0.10+ versions. Node.js applications maximize throughput and efficiency using the non-blocking I/O and asynchronous events. Node.js applications have great performance, and huge flexibility to implement both high end and low-end functionality.

Future of JavaScript

Third-party plugins that were used to develop the videos and other UI visuals using Flash are now replaced by JavaScript giving the same user experience. This programming language has made a unique position on the browser side, dominating almost everything a client computer does. With addition like Node.js, jQuery, JSON, JavaScript is conquering the server-side and the internet of things. In the world of mobile apps, JavaScript using Node.js help customers build incredible and responsive cloud-powered mobile apps.

With all the innovation and standardization applied to JavaScript, the JavaScript is prepared for a completely new cycle of evolution and for an exciting future. With the development of Node.js platform, making JavaScript to be used on the server-side, we can have web applications with two-way communications – both client and server can initiate communication and data can be exchanged between them freely.

– Susan B. John