samedi 28 mars 2015

Structure of Node.js app for convinient testing

I'm developing a Node.js service/app, but I am kind of stuck how to structure it to be suitable for easy testing with mocha and sinon. I have a couple of endpoints and everyone of them is structured in the following way:



controllers:
controllerA:
index.js
validate.js
process.js
doSomething1.js
doSomething2.js
controllerB:
index.js
validate.js
process.js
doOther1.js
doOther2.js
...
utils:
someUtil.js
otherUtil.js


Every file exports a function that is used by other function(ex. process.js exposes a function process(req, res) {...} than internally calls doOther1() {...}. On the other hand function doOther1() {...} calls function doOther2() {...} which uses someUtil).


Unfortunately when it comes to testing it's hard to isolate parts of the service. For example I want to test doOther1 isolating someUtil, but it seems impossible to stub someUtil with sinon, because require('utils/someUtil') returns a function.


What is a more test-friendly way to layout my app? While I was writing it seemed ok to have small separate functions to deal with specific things, but it's hard to write nice tests on every single function. Happy to link me to articles on the subject.


Aucun commentaire:

Enregistrer un commentaire