3

I have been trying to get this to work for days. I've looked around the internets and on StackOverflow. There are examples of how to test APIs using MongoDB and how to write Mocha tests that execute PSQL commands. That's not what I want.

I created a wrapper for pg, called db.js from the instructions in this SO question (note my comments in the calls to console.log():

pg = require("pg");
config = require("./../config.js");

module.exports = { 
    query: function(text, values, cb) {
        console.log("I get to this in Mocha");
        pg.connect(config.connectionString, function(err, client, done) {
            console.log("I never get here");
            if (err) return console.error("error connecting to postgres: ", err);
            client.query(text, values, function(err, result) {
                console.log("I most certainly never get here");
                done();
                cb(err, result);
            })
        });
    }
}

With that, I can do the following:

$ node
$ var db = require ("./path/to/db.js");
$ db.query("insert into sometable(id, value) values(1, \"blah\")", {}, function (err, result) {
            if (err) { console.error ("db errored out man"); }
            console.log("no error...");
            console.log(result);
        });

Believe it or not, that works without a hitch!

What I can't do is the same thing in a mocha test (i.e., db.spec.js):

var db = require("./../../../Data/db.js");

// These tests assume you have run the scripts in the -SQL repo
describe("module: db", function() {
    it("provides a wrapper for the execution of queries", function () {
        db.query("insert into employer.profile \
            (id, returncustomer, receiveupdates, name, email, password, active) \
            values (4, true, true, 'someNameLol', 'ce@spam.org', 'change_me', true)", {}, 
            function (err, stdout, stderr) { 
                console.log(err || ""); 
                console.log(stdout || ""); 
                console.log(stderr || ""); 
            }
        );
    });
}); 

Help! I want to be able to write integration tests using my database connection. Are there components I'm missing? Required libraries?

This is all hand-rolled, I'm not using an IDE, because I want to understand how it's supposed to work by myself.

Thanks in advance.

Community
  • 1
  • 1
Chaim Eliyah
  • 2,743
  • 4
  • 24
  • 37

1 Answers1

4

You need to include the done parameter, and call it at the end of your test.

describe("module: db", function() {
    it("provides a wrapper for the execution of queries", function (done) {
        db.query("insert into employer.profile \
            (id, returncustomer, receiveupdates, name, email, password, active) \
            values (4, true, true, 'someNameLol', 'ce@spam.org', 'change_me', true)", {}, 
            function (err, stdout, stderr) { 
                console.log(err || ""); 
                console.log(stdout || ""); 
                console.log(stderr || ""); 
                done();
            }
        );
    });
}); 
Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33