1

I have a module that I am trying to test. It is laid out like this:

exports.doThings = function(args, callback) {
    async.series([
        function doThisFirst(next) {....},
        function doThisSecond(next) {....}
    ], .....
}

I would very much like to individually test the functions doThisFirst and doThisSecond, so I tried to export them like so:

exports.doThings = function(args, callback) {
    async.series([
        exports.doThisFirst = function(next) {....},
        exports.doThisSecond = function(next) {....}
    ], .....
}

but alas, I get this error when I run the test: TypeError: Object # has no method 'doThisFirst'

It works if I change the code to this:

exports.doThings = function(args, callback) {
    async.series([
        exports.doThisFirst,
        exports.doThisSecond
    ], .....
}
exports.doThisFirst = function(next) {....}

But I think that is bad for the code readability. Is there any way to get around this issue?

Ryan Quinn
  • 1,175
  • 1
  • 15
  • 28
  • 1
    when you do async export, you should try a different strategy. http://stackoverflow.com/questions/6425290/is-it-ok-to-initialize-exports-asynchronously-in-a-node-js-module – Daiwei Dec 26 '13 at 21:02
  • Ah, I see the problem now. Thank you! – Ryan Quinn Dec 26 '13 at 21:09

1 Answers1

1

To increase readability, maybe you could look into defining the inner methods first:

module.exports = exports = {
  doThisFirst: function(args, callback){...},
  doThisSecond : function(args, callback) {...},
  doThings : function(args, callback) {
    async.series([
        exports.doThisFirst,
        exports.doThisSecond
    ], function(err){});
}

Obviously, the problem with this method is that you end up exposing doThisFirst and doThisSecond externally.

verybadalloc
  • 5,768
  • 2
  • 33
  • 49
  • yeah, but i don't know of any other great way to expose these functions for testing anyway. and it is just an internal tool, so it doesn't matter to me if they are exposed externally. – Ryan Quinn Dec 27 '13 at 05:30