milovanderlinden
milovanderlinden

Reputation: 1194

How to integratei sequelize in nodeunit test

I have an express-sequelize application, it uses nodeunit to test routes and for this I use the wonderfull nodeunit-express module.

All works fine, except sequelize creating the database. In my express app I do a sequelize.sync({force: true}) during the start. My tests do work when I run

npm start (node server.js)

first, followed by

npm test (nodeunit test)

But when the database is not present and I run

npm test (nodeunit test)

Alone, the is not created errors.

Where in the nodeunit testing process should I place the sync sequelize to have it available for my tests?

var request = require('nodeunit-express');
var server = require('../server');
var models = require('../models');
var express = request(server.app);

exports["Rest interfaces"] = {
    setUp: function (callback) {
        //insert some stuff in the database?
        callback();
    },
    tearDown: function (callback) {
        callback();
        //cleanup();
    },
    "/api/categories": function (test) {
      express.get('/api/categories').expect(function (response) {
          // response is the response from hitting '/'
          var result = JSON.parse(response.body);
          test.equal(response.statusCode, 200, '/api/categories is not available');
          test.equal(result.length, 0, 'no categories should be returned');
          test.done();
      });
  }
};
function cleanup() {
    //wait a little
    setTimeout(function () {
        express.close();
    }, 500);
}

Upvotes: 2

Views: 130

Answers (0)

Related Questions