In my previous post I showed how to create unit tests for coffeescript. I also included a link to Eli Thompson’s coffeescript unit testing runner which allows you to easily gather all the
.coffee files and the unit tests together in one place thus allowing you to keep your unit tests in separate files (rather than in-lining it in the test-runner as I showed in my previous example).
So far, so good. However, you cannot run this from the local file system as the browser’s security will complain (see the console panel in the screenshot below). The files are loaded using jQuery’s get method.
So, in order to get it to run you need to run it from within the context of a web server. You can use IIS if you are running windows, however for this example, I’m using Linux so I’ll use Apache.
Getting Apache up and running on Linux
To install Apache, in a terminal type:
sudo apt-get install apache2
By default, the newly installed server will serve from the
/var/www/ which will contain an
In order to create a new site that points to your development environment so that you can run the unit tests locally you need to modify apache.
Start by opening the
/etc/apache2/apache2.conf file. (You’ll need to use
sudo or run as root in order to write this back as your user won’t have the permission by default.) And add the following to the end of the file:
This tells Apache that you’ll be creating named web sites on the IP/port specified. This is most useful if you have a server that hosts multiple sites. In our case we are simply using it to create a development site on our local machine. Because we’ve specified the loopback address it won’t be visible outside of the machine it is running on. (More info on NameVirtualHost)
Next we have to create a file in
/etc/apache2/sites-available/ directory. As far as I can see, the convention is to use the host name as the name of the file. So a site running a
http://www.example.com would have a file of the same name. In this case, as it is a development site running only on localhost I like to name it something along the lines of
myproject-localhost so that it is obvious that it is running on the loop back address.
For this example, I’ll create a file called
/etc/apache2/sites-available/coffee-tests-localhost with the following content:
<VirtualHost 127.0.0.1:80> ServerName coffee-tests-localhost ServerAlias www.coffee-tests-localhost ServerAdmin email@example.com DocumentRoot /home/colinmackay/hg/blog-and-demos/three-of-a-kind </VirtualHost>
Since this file is in
sites-available that means it is not yet enabled, so the server won’t be serving it up. In order to get it served up there needs to be a duplicate in
/etc/apache2/sites-enabled/. You don’t need to create a duplicate in Linux as you can create a symbolic link to the original file. To do that, type the following at a terminal:
cd /etc/apache2/sites-enabled/ sudo ln -s ../sites-available/coffee-tests-localhost .
(Note the dot at the end of the second line!)
Since the host name does not really exist, no DNS will resolve it, this is the point that you need to edit the
/etc/hosts file so that your local browser can go to the web site. Add the following line to the
Finally restart the Apache server:
sudo /etc/init.d/apache2 restart
You should now have your web site up and running and displaying the tests to you now.
The running tests
When we go to
http://coffee-tests-localhost/tests/test-runner.htmlall the tests now run and there is no error in the browser’s console: