Jasmine for Javascript Testing on Ec2

Using Ec2 for Javascript testing

In this post I'll show you how to install the Python version of Jasmine on Ec2 and configure it to run Javascript tests. There are a multitude of potential benefits here. This can help you get started with Javascript testing if that is a new concept for you. However, it also migrates testing to the cloud where it can be fault tolerant, highly available and self healing.

Launching your Instance

We aren't going to do anything too out of the ordinary to launch the instance but just to be different let's launch a free tier eligible Ubuntu Server like this

The only other detail we need to worry about for this tutorial is the security group. You can either make a new one while launching the instance or add an inbound rule to an existing security group with port 8888 open. Also be sure to enable a public ip address because we will use it to shell into the instance and to look at the Jasmine test runner in our host browser.

Getting Setup

Once you are shelled into your new instance run the following commands to get squared away.

  • sudo -i
  • cd /home/ubuntu
  • git clone https://github.com/jasmine/jasmine-py.git
  • apt-get update
  • apt-get install python-pip

Now change over to the jasmine-py directory that was created from your git clone and let's configure Jasmine. Run these commands to get set up Jasmine.

  • pip install -r requirements.txt
  • pip install jasmine
  • jasmine-install

After the last command you'll be here.

Be sure to enter "Y" to the installation questions so that an initial spec directory is created with default configurations. Rather than drag you through configuration run rm -rf spec and git clone https://github.com/jdav999/spec.git to check out my already configured spec folder. You are now configured to successfully run some tests. From /home/ubuntu/jasmine-py start your Jasmine server with jasmine -o You can now see your test running and passing in your local browser by pointing it to http://public-ip:8888. Now you'll see success

Awesome stuff ... but where are the tests?

Up until now we have really just been focused on getting successfully set up. Let's look at what is going on. I've put all test, code and config in the git repo you cloned earlier. This makes it easy to brows via github. Let's look at the test first here. It's intuitive to follow.

describe('demonstrates a basic jasmine test', function () { it('returns a calculation result', function () { var result = calculate(4,2); expect(result).toEqual(6); }); });

You can see this test looks at a function called calculate that takes in two arguments. The expect function is the assertion and asserts that calculate should return the value of 6. This is a typical Jasmine test. The actual calculate function is here. Clearly, we can make the test fail by asserting it would return an incorrect value. Indeed, changing 6 to 7 in the toEqual function of the above code will cause the test the fail and it would look like this.

Test Driven Development

Part of the goal of this post is to show people that you should write tests for your code. Specifically it shows you how to test javascript code. Having tests for your code exercises it's functionality and shows that it gives a desired output for particular input. The philosophy of test driven development says that you should have a failing test before your write any code. When you have code that makes the test pass then you have completed your development task. This is a philosophically sound idea but life is rarely this transparent. In complex code baselines you are likely to encounter situations where writing a test before the code is difficult because you may not have the context to write the test before wrestling with the code to accomplish what you want.

Before you live and die on the dictum of "write a failing test first" you might want to take measure of the fact that it doesn't matter if you start with a failing test as long as you end up with a test that demonstrably fails in a predictable way. In the above example I started with a passing test and showed that it could fail. Everything still turned out fine. If you are one of those people that like to insist that writing the test first is a best practice I encourage you to read this.

Where to go from here ...

I hope that you can take what I have shown you here and start testing your Javascript layer. From here you can incorporate Jasmine into your Jenkins builds or start experimenting with it's various plugins. The possibilities are endless. Have a good time.