Mocha/PhantomJS/enyojs example?

Hello,

I would like to test some of the JavaScript for my application using Mocha and PhantomJS. I'm wondering if someone has an example of an application written using Enyo 2.7 that also has JUnit tests. I'm struggling to create unit tests that run against my production code that "requires" enyo modules.

I've tried substituting require.js and testing my code directly, but I found that I would have had to include a lot of enyo.js files to make that work. The tests would have been really difficult to configure.

Right now I'm trying to include my unpacked JavaScript code and define enyo, but the enyo require can't find my modules. Here is my test.html file for Mocha and Phantom.js.

<html>
  <head>
    <meta charset="utf-8">
    <!-- encoding must be set for mocha's special characters to render properly -->
    <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
    <link rel="stylesheet" href="../../bin/enyo.css"/>
  </head>
  <body>
    <div id="mocha"></div>
    <script src="../../node_modules/mocha/mocha.js"></script>
    <script src="../../node_modules/chai/chai.js"></script>
    <script src="../../bin/enyo.js"></script>
    <script src="../../bin/layout.js"></script>
    <script>
      mocha.setup('bdd');
      var assert = chai.assert;
      var expect = chai.expect;
      var should = chai.should();
      //trying to define enyo here so that I can test my source files without packing.
      var   enyo     = enyo || (enyo = {})
		, manifest = enyo.__manifest__ || (defineProperty(enyo, '__manifest__', {value: {}}) && enyo.__manifest__)
		, exported = enyo.__exported__ || (defineProperty(enyo, '__exported__', {value: {}}) && enyo.__exported__)
		, require  = enyo.require || (defineProperty(enyo, 'require', {value: enyoRequire}) && enyo.require);
      var module = {};
    </script>    
    <!-- These are my source code files for the application -->
    <script src="../../src/HomePage.js"></script>
    <script src="../../src/AppRouter.js"></script>
    <script src="../../src/index.js"></script>
    <script>
        if (window.mochaPhantomJS) { 
            mochaPhantomJS.run(); 
        } else { 
              mocha.run(); 
        }
    </script>
   </body>
</html>
So when I run this test, I get the following output.

Error: Could not find module "./HomePage" in enyoRequire at file:///home/chad/NetBeansProjects/PlayConnect360/bin/enyo.js:40 at file:///home/chad/NetBeansProjects/PlayConnect360/src/index.js:8 chad@chad-700Z3C-700Z5C:~/NetBeansProjects/PlayConnect360$ /home/chad/.nvm/versions/node/v7.5.0/bin/node /home/chad/.nvm/versions/node/v7.5.0/bin/mocha-phantomjs tests/unit/test.html Error: Could not find module "./HomePage" in enyoRequire at file:///home/chad/NetBeansProjects/PlayConnect360/bin/enyo.js:40 at file:///home/chad/NetBeansProjects/PlayConnect360/src/index.js:8

So module HomePage is required in index.js like this.
var ready = require('enyo/ready')
    , kind = require('enyo/kind')
    , dom = require('enyo/dom')
    , Application = require('enyo/Application')
    , HomePage = require('./HomePage')
Am I going at this the wrong way? I would prefer to not pack the application each time I want to run my tests, and even if i pack the application I'm still not sure how I would write my unit tests to access the packed code. Can I modify the way enyo looks for my modules?

Comments

  • did you take a look at the way Enyo loads its tests for Mocha? That might be a good place to start. There are mocha tests that run on phantomjs in the core repo.
Sign In or Register to comment.