Since this book is all about JavaScript, we are going to create a very simple Node.js server to be used by the specs. Node.js is a platform that allows the development of network applications, such as web servers, using JavaScript.
In Chapter 6, Light Speed Unit Testing, we are going to see alternative solutions to test AJAX requests without the need for a server. And in Chapter 8, Build Automation, we are going to see how to use Node.js as a foundation for an advanced build system.
If you already have Node.js installed, you can skip to the next section.
There are installers available for Windows and Mac OS X. Perform the following steps to install Node.js:
To check other installation methods and instructions on how to install Node.js on Linux distributions, check the official documentation at https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager.
Once you are done, you should have the node
and npm
commands available on your command line.
For the purpose of learning how to write asynchronous specs, we are going to create a server that returns some fake data. Create a new file in the project's root folder called server.js
and add the following code to it:
var express = require('express'), var app = express(); app.get('/stocks/:symbol', function (req, res) { res.setHeader('Content-Type', 'application/json'), res.send({ sharePrice: 20.18 }); }); app.use(express.static(__dirname)); app.listen(8000);
To handle the HTTP requests, we use Express, a Node.js web application framework. By reading the code, you can see that it defines a route to /stocks/:symbol
, so it accepts requests such as http://localhost:8000/stocks/AOUE
and responds with JSON data.
We also use the express.static
module to serve the spec runner at http://localhost:8000/SpecRunner.html
.
There is a requirement to circumvent the SOP. This is a policy that dictates, for security reasons, that AJAX requests aren't allowed to be performed on domains different than the application.
This issue was first demonstrated while using HTML fixtures in Chapter 3, Testing Frontend Code.
Using the Chrome browser inspector, you can see errors in the console while opening the SpecRunner.html
file with a file://
protocol (basically, the way you've been doing until now):
By serving the runner, and all of the application and test code under the same base URL, we prevent this problem from happening and are able to run the specs on any browser.
To run the server, first you need to install its dependencies (Express) using Node's package manager. Inside the application root folder, run the npm
command:
$ npm install express
This command will download Express and place it inside a new folder called node_modules
inside the project folder.
Now you should be able to run the server by invoking the following node
command:
$ node server.js
To check whether it is working, hit http://localhost:8000/stocks/AOUE
on your browser, and you should receive the JSON response:
{"sharePrice": "20.18"}
Now that we have our server dependency working, we can get back to writing the spec.