> For the complete documentation index, see [llms.txt](https://paazmaya.gitbook.io/modern-web-tools-with-node-js/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://paazmaya.gitbook.io/modern-web-tools-with-node-js/lectures/07-unit-testing.md).

# Unit testing, Jasmine, PhantomJS

> Date 07/10/2014

[Second assignment is given.](/modern-web-tools-with-node-js/assignments/2-module.md)

## Unit testing in general

Unit testing, as the name suggests, is a way of testing units or portions of code.

## Popular unit testing frameworks

* [Jasmine](http://jasmine.github.io/)
* [Mocha](http://visionmedia.github.io/mocha/)
* [Qunit](http://qunitjs.com/)
* [nodeunit](https://github.com/caolan/nodeunit)

## PhantomJS, a headless WebKit browser

In an environment where there is no availability of screens, such as a server, continuous integration server specifically, is a need for tools which do not require the screen. One such tool specifically made for browser based testing, is [PhantomJS](http://phantomjs.org/).

However the latest release of PhantomJS, `v1.9.8` from October 2014, is based on a [WebKit](http://www.webkit.org/) from 2011. The next major release (`v2.0.0`) should be out soon and it will be based on a much newer [WebKit](http://www.webkit.org/), via [Qt](http://qt-project.org/) `v5.3` which is used underneath.

## Tasks for the day

1. Add unit tests to your `hello-node-js` repository by using `nodeunit`
   * Unit test spec files are placed in a directory called `tests`
   * Each unit test file named as its targeting source file and `_spec.js` suffix
2. Add a task runner task for running the unit tests
   * Make sure that tests can be run after a fresh clone of the repository,

     followed by `npm i` and `[task runner] test`, for example `grunt test`

## Links related to the lecture subject

* [Testing with Jasmine](http://blog.codeship.io/2013/07/30/testing-tuesday-16-javascript-testing-with-jasmine.html)
* [PhantomJS quick how to](http://www.sitepoint.com/headless-webkit-and-phantomjs/)
* [Video: PhantomJS and Jasmine how to](https://www.youtube.com/watch?v=p5w6oNFT4ks#t=1550)
* [Testing Private Functions in JavaScript Modules](http://engineering.clever.com/2014/07/29/testing-private-functions-in-javascript-modules/)

## Examples for the tasks

### 1. Unit tests with nodeunit

Install dependencies first:

```bash
npm i nodeunit --save-dev
```

Create directory for tests and initial empty test file:

```bash
mkdir tests
touch tests/last-mod_spec.js
```

The test can be written as the following JavaScript, assuming that the `LICENSE` file exists in the repository and its modification date is in October 2014.

```javascript
// last-mod_spec.js

var lastMod = require('../last-mod');

exports.lastMod = function(test){
  test.expect(4);

    // Non existing file
    var non = lastMod('not-to-be-found.js');
    test.strictEqual(non, false, 'False returned when file not existing');

    // Existing file
    var yes = lastMod('LICENSE'); // Date object
    test.equal(yes instanceof Date, true, 'Date object returned when file exists');

    // Correct year and month
    test.equal(yes.getFullYear(), 2014, 'Year is matching');
    test.equal(yes.getMonth(), 9, 'Month is October');

  test.done();
};
```

Run the tests with:

```bash
node_modules/.bin/nodeunit last-mod_spec.js --reporter verbose
```

### 2. Task runner for the unit tests

```bash
npm i grunt-contrib-nodeunit --save-dev
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://paazmaya.gitbook.io/modern-web-tools-with-node-js/lectures/07-unit-testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
