Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter way to dynamically change require statement for code-coverage on tests?
    text
    copied!<p>I have a node.js library which I've setup for BDD with <a href="http://visionmedia.github.io/mocha/" rel="nofollow"><strong>mocha</strong></a> and <a href="http://chaijs.com/" rel="nofollow"><strong>chai</strong></a>. I've also added code coverage with <a href="http://gotwarlost.github.io/istanbul/" rel="nofollow"><strong>istanbul</strong></a>. And everything runs via <a href="http://gruntjs.com/" rel="nofollow">grunt</a> tasks. </p> <p>I struggled for a while and I finally got it working after I based my setup on this repository: <a href="https://github.com/morkai/h5.buffers" rel="nofollow">https://github.com/morkai/h5.buffers</a>. It works but I don't like the implementation.</p> <p>The grunt-istanbul plugin instruments the files, an environment variable is set, and the tests use the environment variable to find the location of instrumented tests. It is this last part that I don't like.</p> <pre><code>//Gruntfile.js // ... env: { "default": { LIB_FOR_TESTS_DIR: srcLibForTestsDir }, coverage: { LIB_FOR_TESTS_DIR: lcovLibForTestsDir } } // ... </code></pre> <p>Then in the test file, the code is: </p> <pre><code>var FooClient = require((process.env.LIB_FOR_TESTS_DIR || '../lib') + '/foo-client.js'); </code></pre> <p>I'm also using a helper in the test for any initialization required before running the test file. So I had two thoughts;</p> <ol> <li><p>Use the test helper for pulling in the files and hide the details there:</p> <pre><code>var foo = require('./test-helper.js').foo; var FooClient = foo.FooClient; var ThatOtherThing = foo.ThatOtherThing; </code></pre></li> <li><p>Use a test runner? I've seen references to this but I have not found much information about it and best practices.</p></li> </ol> <p>Is there a better way?</p> <p>P.S. If somebody has a suggestion for a better title for this question, I'm open for ideas.</p> <p>P.P.S. Please don't tell me to switch to <strong>blanket</strong> in your answer. I evaluated both and decided on istanbul because it gives more code coverage insights (e.g., branching). If the istanbul branch of blanket is ever completed, then I'll consider switching.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload