// JavaScript - Unit Testing with Jasmine:
# Global installation
npm install -g jasmine
jasmine init // initialize a project for Jasmine
jasmine examples // seed your project with some examples
jasmine // run your test suite
After executing the above commands, we will find the lib and spec folders in our
current working directory.
Customize spec/support/jasmine.json to enumerate the source and spec files you
would like the Jasmine runner to include. You may use dir glob strings.
Alternatively, you may specify the path to your jasmine.json by setting an
environment variable:
jasmine JASMINE_CONFIG_PATH=relative/path/to/your/jasmine.json

How can we install jasmine using npm?

npm install -g jasmine-node

How can we write tests for our application?

Let's create a tests directory which will hold our tests. The first thing that we are going to check is our configuration setup. The spec files must end with .spec.js, so the file should be called config.spec.js.

The model is what will be handling the data that's in our application. It should have access to a db object, returned by MongoClient. Our model should also have a method for extending it, because we may want to create different types of models. For example, we might want a BlogModel or a ContactsModel. So we need to write a new spec: /tests/base.model.spec.js, in order to test these two model features. And remember, by defining these functionalities before we start coding the implementation, we can guarantee that our module will do only what we want it to do.

Instead of a real db object, I decided to pass a mockup object. That's because later, I may want to test something specific, which depends on information coming from the database. It will be much easier to define this data manually.

The implementation of the extend method is a little bit tricky, because we have to change the prototype of module.exports, but still keep the original constructor. Thankfully, we have a nice test already written, which proves that our code works. A version which passes the above, looks like this:

In order to test the rendering, I had to create a mockup. In this case, I created an object which imitates the Express's response object. In the second part of the test, I created another View class which inherits the base one and applies a custom render method. Here is the /views/Base.js class.

The express(1) command line tool creates a directory named routes, but in our case, it is better for it to be named controllers, so I changed it to reflect this naming scheme.

Since we're not just building a teeny tiny application, it would be wise if we created a base class, which we can extend. If we ever need to pass some kind of functionality to all of our controllers, this base class would be the perfect place. Again, I'll write the test first, so let's define what we need:

it should have anextend method, which accepts an object and returns a new child instance

the child instance should have a run method, which is the old middleware function