So, I'm new to testing in js and I'm trying to use mocha to test a node application.
Ideally I'd like to have it so that the tests are in a separate file and are run on the node script itself (perhaps by use of a require call at the top of the test script). At the moment I'm doing something which I know can't be right which is to include the tests in a block at the bottom of the script file. The problem with this being test code included in production code and the fact that it comes back with 'describe is not defined' when you try to start the node server.
I've read lots of tutorials on mocha, etc and it seems that you can export all of your methods individually by doing 'module.export.', but that seems costly and is also obtrusive.
What am I missing here? Surely you don't have to construct two scripts (one with and one without the test code?) - that would end up with version issues.
How is this best done (links to articles also gratefully accepted. :)
Thanks
James
So here's what I see people doing, and the method I've adopted in my own work:
test directory and place all of your test cases in it. Use whatever naming convention you like. I prefer to have one test file for each source module. At the start of each file, simply require the module under test.--recursive argument so it knows to examine the entire contents of your test directory.This means you are testing the modules through their interfaces, just as you would use them in normal code. The downside is, if you have non-exported functions with execution paths which are difficult to hit via the module's interface, you may find yourself a little frustrated.
On one hand, I agree with the notion that modules should be tested as they are used. On the other hand I think I'd sleep better at night knowing that I directly tested every one of the aforementioned tricky execution paths. Prior to doing node.js programming I spent several years writing erlang code, which has the exact opposite convention: (tests are placed at the end of the source files so they have direct access to every function in the module, and testing includes/exports are handled by ifdef directives). Personally, I like the erlang approach better.
Anyway, I hope this was helpful. If anyone reading this has a strong opinion to the contrary I'd love to hear it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With