Sequelize unit testing using Grunt and Nodeunit

Posted by: Seth Lakowske

If you want to persist Javascript objects (data) to a database using Node, then it is a good idea to test this mechanism is working.  Below you will learn how to accomplish testing your data persistence mechanism.  Sequelize, Nodeunit and Grunt are popular tools to accomplish this task.  Sequelize is an Object Relational Mapper (ORM), Nodeunit is a unit testing framework and Grunt is a common test runner that will run all the tests you create.

A project containing the result of this tutorial is available at https://github.com/lakowske/sequelize-grunt-nodeunit-example

Getting Started

First, be sure you have installed Node and npm.  Next, do the following:

Install grunt command line interface

sudo npm -g install grunt-cli

Now go to your project directory. If you don’t already have a project directory, create one. If you don’t have a package.json file, create a basic one using

npm init

Next add dependencies to your project

npm install sequelize --save
npm install sqlite3 --save
npm install nodeunit --save-dev
npm install grunt --save-dev
npm install grunt-contrib-nodeunit --save-dev
npm install async --save
npm install uuid --save

Create a Gruntfile.js configured to run nodeunit tests.


module.exports = function(grunt) {

    // Project configuration.                                                                                                                                                                
    grunt.initConfig({
        nodeunit: {
            all: ['./**/*Test.js']
        }
    });

    // Load nodeunit task                                                                                                                                                                
    grunt.loadNpmTasks('grunt-contrib-nodeunit');

};

Next I created a SequelizeDatabase.js implementation. It contains a function that returns a sequelize sqlite instance.

var Sequelize = require('Sequelize');

exports.testDB = function testDB() {
    return new Sequelize('database', 'username', 'password', {
        // sqlite! now!                                                                                                                                                                
        dialect: 'sqlite',

        // the storage engine for sqlite                                                                                                                                                                
        // - default ':memory:'                                                                                                                                                                
        storage: __dirname + '/database.sqlite'
    });
}

I make the unit test SequelizeDatabaseTest.js.

var db = require('./SequelizeDatabase');
var uuid = require('uuid');

exports.testSequelize = function (test) {
    var  sequelize= db.testDB();

    sequelize.query("DROP TABLE COMMANDLINES").success(function(myLines) {
        console.log('drop table COMMANDLINES');
    });

    sequelize.query("CREATE TABLE IF NOT EXISTS COMMANDLINES (ID varchar(255), MYLINE TEXT)").success(function(myLines) {
        console.log('create table COMMANDLINES');
    });
    // Generate a v1 (time-based) id                                                                                                                                                                                                                                                                                           
    var id = uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'                                                                                                                                                                                                                                                           

    sequelize.query("INSERT INTO COMMANDLINES (ID, MYLINE) VALUES ('" + id + "', 'aline')").success(function(myLines){
        console.log('insert ' + id + " into commandlines");
    });

    sequelize.query("SELECT * FROM COMMANDLINES").success(function(myLines) {
        var cmdLine = myLines[0];

        test.equal(id, cmdLine.ID,"should be " + id);

        test.equal("aline", cmdLine.MYLINE,"should be aline");

        console.log(cmdLine.ID);
        console.log(cmdLine.MYLINE);
        console.log(myLines);
        test.done();
    });
}

Finally, to establish the data persistence mechanism is working, run the unit test.

nodeunit SequelizeDatabaseTest.js

Or run all your tests using Grunt.

grunt nodeunit