{"id":940,"date":"2012-11-27T10:59:29","date_gmt":"2012-11-27T09:59:29","guid":{"rendered":"https:\/\/www.unicoda.com\/?p=940"},"modified":"2012-11-27T11:06:49","modified_gmt":"2012-11-27T10:06:49","slug":"mocha-path-to-mocha-opts-with-f","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=940","title":{"rendered":"Mocha: path to mocha.opts with -f"},"content":{"rendered":"<p><a href=\"http:\/\/visionmedia.github.com\/mocha\/\">Mocha<\/a> is a wonderful (:D) test framework which allows you to write really understandable tests in JavaScript. I&rsquo;ve already introduced it in a previous article, so today I&rsquo;ll focus on a pull request I&rsquo;ve proposed.<\/p>\n<p>Like other programs, you can pass arguments to mocha, like for instance &#8211;reporter used to specified a way to display test results. By the way, you should try once the nyan reporter. Concerning mocha, it&rsquo;s also possible to set arguments in a file named \u00ab\u00a0mocha.opts\u00a0\u00bb. Interesting feature, but the file must absolutely be stored in your test directory.<\/p>\n<p>So the idea is to add an argument: <strong>-f, &#8211;fileconf &lt;path&gt;<\/strong>. Furthermore, the &#8211;fileconf option allows you to specify the configuration file that will be used, by default \u201c\/test\/mocha.opts\u201d; allowing you to store your configuration file anywhere you wanted. This modification is very useful if you make tests using the same configuration file in many directories. Without &#8211;fileconf, you have to copy your file in each directory, and even a small modification must be done on each file. Using -f, you store the mocha.opts file in a unique place, so that any modifications of the file would immediatly impact all tests.<\/p>\n<h1>About the code<\/h1>\n<p>Add the parameter with commander:<\/p>\n<pre>.option('-f,--config\u00a0&lt;path&gt;','specify\u00a0the\u00a0path\u00a0to\u00a0configuration\u00a0file')<\/pre>\n<p>Load the file using commander to parse the command line:<\/p>\n<pre>\/\/-f, --config\r\nprogram.parse(process.argv);\r\nvar pathConf = 'test\/mocha.opts';\r\nif (program.config &amp;&amp; exists(program.config)) {\r\n\u00a0 pathConf = program.config;\r\n}\r\ntry {\r\n  var opts = fs.readFileSync(pathConf, 'utf8')\r\n    .trim()\r\n    .split(\/\\s+\/);\r\n\r\n  process.argv = process.argv\r\n    .slice(0, 2)\r\n    .concat(opts.concat(process.argv.slice(2)));\r\n} catch (err) {\r\n  \/\/ ignore\r\n}<\/pre>\n<p>In fact, this code is not available in mocha yet since \u00ab\u00a0parsing twice might have some strange side-effects\u00a0\u00bb&#8230;<\/p>\n<p>An other way to augment argv without using commander:<\/p>\n<pre>\/\/-f, --config\r\n\r\nvar pathConf = 'test\/mocha.opts'\r\n  , fIndex = process.argv.indexOf('-f')\r\n  , configIndex = process.argv.indexOf('--config');\r\n\r\nif(fIndex !== -1 &amp;&amp; exists(process.argv[fIndex + 1]) ) {\r\n  pathConf = process.argv[fIndex + 1];\r\n}\r\n\r\nif(configIndex !== -1 &amp;&amp; exists(process.argv[configIndex + 1]) ) {\r\n  pathConf = process.argv[configIndex + 1];\r\n}<\/pre>\n<p style=\"text-align: left;\">And I&rsquo;ll finished this article by quoting visionmedia:<\/p>\n<blockquote>\n<p style=\"text-align: center;\">Maybe it&rsquo;s ok but that&rsquo;s still pretty hacky.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Mocha is a wonderful (:D) test framework which allows you to write really understandable tests in JavaScript. I&rsquo;ve already introduced it in a previous article, so today I&rsquo;ll focus on a pull request I&rsquo;ve proposed. Like other programs, you can pass arguments to mocha, like for instance &#8211;reporter used to specified a way to display &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=940\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Mocha: path to mocha.opts with -f&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[20,34],"class_list":["post-940","post","type-post","status-publish","format-standard","hentry","category-code","tag-mocha","tag-pull-request"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=940"}],"version-history":[{"count":6,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions"}],"predecessor-version":[{"id":958,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/940\/revisions\/958"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}