How to unit-test?

84 views
Skip to first unread message

Honza Břečka

unread,
Oct 25, 2014, 1:36:06 PM10/25/14
to nod...@googlegroups.com
Hi,

I just want to know how do you unit-test that the following function, part of many node packages, returns expected result?

var http = require('http');
var https = require('https');

function getUrlLoader(url) {
  return url.indexOf('https') ? https : http;
}

Thanks!

Aria Stewart

unread,
Oct 25, 2014, 6:53:11 PM10/25/14
to nod...@googlegroups.com
Export it:

module.exports = getUrlLoader;

then try:

var test = require('tape');
var http = require('http');
var https = require('https');
var subject = require('./yourmodule');

test('check it out', function (t) {
t.equal(subject('https') == https);
t.equal(subject('http') == http);
t.end();
});


This exploits the fact that the return value from require is cached and consistent.

Also, just as a side note, please consider the url.parse method, rather than doing a naive indexOf on the URL for this purpose.

Aria

Floby

unread,
Oct 27, 2014, 5:10:04 AM10/27/14
to nod...@googlegroups.com
Aria got it right. Also on the fact that indexOf returns -1 when it does not match anything, your code doesn't work =) since -1 is truthy.
Reply all
Reply to author
Forward
0 new messages