MSB MSB - 4 months ago 5x
Node.js Question

how to unit test third-party code in nodejs?

I'm currently in the process of writing an API for use in a private environment. Some of the functions introduced are promisified versions of third-party functions.


Third-party code:

function someFunction(argument, callback) {
var command = stuff;
return this.exec(command, parse_command(callback));

My code:

function promisifiedFunction() {
new Promise(function (resolve, reject) {
third-party.someFunction('argument', function (err, status) {
if (err) {
return reject(new Error("FOO"));
} else {
return resolve(status.result);

How do I properly unit test these functions?

(note that the third-party code has already been unit tested)


Sinon.js is tool which will help you to work third party libraries change their functionality and customize functions on you own. It's a big and complex subject, I can't explain in one comment or anwser, but I can give you url of articles which will help you to understand main concepts. This articles helped me when started testing using sinon.js hope it will be helpfull to you too.

1) Official Documentation (It covers all subjects, but hard to learn from here, not good documentation)

2) Article 1 (Great article explained with ilustrations)

3) Article 2 (From this article everything become clear for me)