0

Every developer advocates test-driven development, but I am coding a website in NodeJS and I am not testing my app.

This is not because I don't see any reason. I know it allow me to check at each commit whether my code is right, thus avoiding security flaws.

I don't do any tests because I don't know how or what to test. I coded a MVC app with express + mongoose + passport, and I have no idea what to test or how to test. I know it is possible to mock express requests or mongodb, but I don' find that clean and flexible.

So, what and how to test?

  • 2
    Every developer advocates test-driven development - citation please? – Jimmy Hoffa Apr 17 '14 at 16:20
  • Sorry, a lot. But almost every project has a .travis.yml thus unit testing – Vinz243 Apr 17 '14 at 16:33
  • Every developer advocates test-driven development ... but only a few does! – Mahdi Apr 17 '14 at 16:34
2

If you want to test your code independently of NodeJS, create a service layer or repository layer that contains an API you can call on one side, and Node.JS calls on the other. You can use your stubs or mocks on the methods in that layer, or even swap out the entire implementation of the service layer if that works for you.

What you test is the same as it ever was: methods.

  • Thanks you I think I understand what you mean: my website is based on another layer in node.js, and the routes parses requests and call method with args (ie: addUser(username, password)). How to structure that ? a folder api/ and controllers/ ? Also, do you have any example please ? – Vinz243 Apr 17 '14 at 16:16
  • You mock it or stub it, same as any other method. When the time comes for integration/acceptance testing, test it for real. The makeup of the folder structure is almost irrelevant. – Robert Harvey Apr 17 '14 at 16:18
  • stub ? I'm sorry I'm french and my english is awful – Vinz243 Apr 17 '14 at 16:20
  • do you have any example please ? – Vinz243 Apr 17 '14 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.