AnsweredAssumed Answered

Automated Unit Testing Problem

Question asked by donnieh on Oct 10, 2013
Latest reply on Oct 11, 2013 by donnieh
I've managed to get unit testing up and running in my project using a combination of mocha, chai, and sinon (our company standard JS testing libraries). This all works perfectly fine when launching unit tests from within a regular browser. However, things go awry when i try to run these tests from within a headless browser (PhantomJS using mocha-phantomjs). In this case, i get the following error:

ReferenceError: Can't find variable: require

Here's what I'm doing to run the tests:

<html>     <head>         <title>Unit tests</title>                                 <link href="../../node_modules/mocha/mocha.css" rel="stylesheet" type="text/css" />                             </head>     <body>         <script src="../../config/dojo.test.config.js"></script>          <script src="//js.arcgis.com/3.7compact/"></script>          <div id="mocha"></div>                 <script>             require(["../../node_modules/chai/chai.js",                     "../../node_modules/mocha/mocha.js",                     "../../node_modules/sinon/lib/sinon.js"], function (chai) {                         mocha.setup('bdd');                         mocha.reporter('html');                         expect = chai.expect;                         require(["test/unit/MapApp"], function () {                             if (window.mochaPhantomJS) {                                 mochaPhantomJS.run();                             } else {                                 mocha.run();                             }                         });                     });                     </script>     </body> </html>


Why are the testing libraries pulled in through a require? Originally I had chai, mocha, and sinon referenced in script tags, but I kept getting multipleDefine errors from the dojoLoader. The dojo documentation says this about the error:

[INDENT]AMD define was called referencing a module that has already been defined. The most common cause of this problem is loading modules via <script> elements in the HTML document. Use the loader; don't use <script> elements. The second most common cause is passing explicit module identifiers to define; don't do this either.[/INDENT]

After moving these scripts to a require call, everything worked fine in IE, FF, Chrome, etc.

It seems like something very fundamental is going wrong here since require is undefined when the code is run from phantomjs. Does anyone have any experience with running automated unit tests using this setup? Any suggestions from the ESRI folks? I'd be open to trying another solution that works w/ mocha. Thanks.

///Donnie

Outcomes