Poni
Poni

Reputation: 11317

Run mocha from phantomjs

I'd like to do some BDD using Mocha and PhantomJS only.
Define a few functional paths and test 'em.

In short, I'd love to run the command phantomjs test.js while test.js contains something like this:

var mocha = require("mocha"); // This one fails with "ReferenceError: Can't find variable: process"..... "/node_modules/mocha/index.js:2"

var page = require('webpage').create();

describe('Empty', function(){ // Obviously this one fails too with "ReferenceError: Can't find variable: describe"
  describe('tests are', function(){
    it('very nice', function(done){
      page.open("http://www.phantomjs.org/", function (status) {
          console.log('Loading a web page');
          console.log("Page is loaded");
          phantom.exit(); // Or, just call `done()`, doesn't matter right now
      });
    })
  })
})

Can you recognize the code pieces of the two?

How can I make this work? I've read on some page that I could require() "mocha.js" of the browser version, but it doesn't seem to work very well.

------------------More details ------------------------------------

I've tried CasperJS but besides providing some really nice stuff, I miss Mocha's appealing test design (server-side).

All the examples I've seen explain how to run your Mocha tests on the webpage (browser-side) (with a script tag), rather than the NodeJS test script.
I don't like it, I want to keep the test pure-console, in addition to the fact that this approach encourages me to add testing code to my webpages.

I've seen a few projects trying to ease that approach yet I'm not content with it - it seems like they've done 95% of the requirement by bringing Mocha and PhantomJS, but the last 5% are still undone, the stuff that really connect it together.

Upvotes: 2

Views: 2256

Answers (2)

esp
esp

Reputation: 7687

Running phantomjs via the bridge from node may not be the best option.

There is phantomjs-nodeify module (I forked from http://github.com/jgonera) that makes phantomjs environment more like node environment - it adds modules process, events, etc. to phantomjs.

There is webspecter framework that achieved exactly that, with old phantomjs and old mocha though.

It seems possible to make mocha tests run in phantom 2.0.

Upvotes: 0

Michael Tang
Michael Tang

Reputation: 4896

You may want to run your tests still in a Node context (node test.js), but controlling phantomJS from node with something like the node-phantom library.

Something TJ Holowaychuk does in all of his libraries is this process.env.COV check (which I personally don't understand), but it's likely that PhantomJS won't support the process variable.

You might be able to trick Mocha by setting window.process and window.process.env to empty objects, though I'd guess you'll run into more PhantomJS/Node issues later on.

Upvotes: 3

Related Questions