Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha/Node.js/PostgreSQL integration testing

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', '[email protected]', '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.

like image 447
Chaim Eliyah Avatar asked Feb 26 '26 01:02

Chaim Eliyah


1 Answers

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', '[email protected]', 'change_me', true)", {}, 
            function (err, stdout, stderr) { 
                console.log(err || ""); 
                console.log(stdout || ""); 
                console.log(stderr || ""); 
                done();
            }
        );
    });
}); 
like image 146
Yuri Zarubin Avatar answered Feb 28 '26 14:02

Yuri Zarubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!