Samson
Samson

Reputation: 2821

Mocha-phantomjs testing no output

I have successfully installed the node module "mocha-phantomjs" here. I wrote the following test case:

test1.js

describe("DOM Tests", function () {
    var el = document.createElement("div");
    el.id = "myDiv";
    el.innerHTML = "Hi there!";
    el.style.background = "#ccc";
    document.body.appendChild(el);
    var myEl = document.getElementById('myDiv');
    it("is in the DOM", function () {
        expect(myEl).to.equal(null);
    });
    it("is a child of the body", function () {
        expect(myEl.parentElement).to.equal(document.body);
    });
    it("has the right text", function () {
        expect(myEl.innerHTML).to.equal("Hi there!");
    });
    it("has the right background", function () {
        expect(myEl.style.background).to.equal("rgb(204, 204, 204)");
    });
});

TestCase.html

<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="mocha.js"></script>
    <script src="chai.js"></script>
    <script>
      mocha.ui('bdd');
      mocha.reporter('html');
      expect = chai.expect;
    </script>
    <script src="test1.js"></script>
    <script>
      if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
      else { mocha.run(); }
    </script>
  </body>
</html>

What I'm getting is absolutely no output, even if I do a typo inside my js file.

The main point here is I have to use some automated dom and js tests in a headless server and to integrate that with Jenkins, and I thought this using TAP reports was alright. Any other suggestions are welcome.

EDIT

I updated my phantomjs version and now I get the following:

TypeError: 'undefined' is not a function (evaluating 'mocha.ui('bdd')')

  file:///home/seventeenlive/site/tests/TestRunner2.html:11
ReferenceError: Can't find variable: describe

  file:///home/seventeenlive/site/tests/test1.js:20
ReferenceError: Can't find variable: process

  mocha-phantomjs/core_extensions.js:21
  mocha-phantomjs/core_extensions.js:82
ReferenceError: Can't find variable: process

  phantomjs://webpage.evaluate():2
  phantomjs://webpage.evaluate():8
  phantomjs://webpage.evaluate():8
ReferenceError: Can't find variable: process

  phantomjs://webpage.evaluate():3
  phantomjs://webpage.evaluate():17
  phantomjs://webpage.evaluate():17
Failed to start mocha.

Upvotes: 1

Views: 3684

Answers (2)

Jared Forsyth
Jared Forsyth

Reputation: 13162

I had this issue too, and the problem was my version of mocha -- going back to 1.9.0 fixed it.

Upvotes: 2

Alan Andrade
Alan Andrade

Reputation: 180

I solved this by serving mocha.js using the relative path to node_modules/mocha.

<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>

This html should be under '/test' directory. If not, adjust the path.

Upvotes: 7

Related Questions