nathanboktae / mocha-casperjs

Write CasperJS tests using Mocha
MIT License
120 stars 30 forks source link

Error when calling with reporter=xunit at 0.5.4 #68

Closed ardi-n closed 8 years ago

ardi-n commented 9 years ago

This error happens when running mocha-casperjs although it goes fine with version 0.5.3. ./node_modules/.bin/mocha-casperjs --reporter=xunit test/test.js

"[object Object]" reporter blew up with error:
TypeError: 'undefined' is not a function (evaluating 'p.charAt(0)')
    at /Users/user/Documents/test/node_modules/mocha/mocha.js:33
    at /Users/user/Documents/test/node_modules/mocha/mocha.js:1551
    at /Users/user/Documents/test/node_modules/mocha-casperjs/bin/cli.js:126
wouterbulten commented 8 years ago

I get the same error when trying to use the xunit reporter:

mocha-casperjs --reporter=xunit tests/tests.js

results in

"[object Object]" reporter not found

Everything works fine when using json (i.e. --reporter=json --file=out.json). Any ideas what can go wrong?

nathanboktae commented 8 years ago

Any ideas what can go wrong?

yeah mocha runs in PhantomJS in the API side for mocha-casperjs, which is not the browser, and not Node.js, so a lot could. What version of mocha is mocha-casperjs using?

Sorry @ardi-n I let this one slip so long. I can take a look at it this week hopefully.

wouterbulten commented 8 years ago

From my package.json:

    "mocha": "^2.3.4",
    "mocha-casperjs": "^0.5.4"
    "casper-chai": "^0.2.1",
    "casperjs": "^1.1.0-beta3",
    "chai": "^3.4.1",

Let me know if I can provide you with more info! :)

wouterbulten commented 8 years ago

I did a new test with the example from the mocha-casperjs homepage (i.e. the google search test). My full command is then:

mocha-casperjs --reporter=xunit --file=log.xml test.js

And output:

"[object Object]" reporter not found
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///<path to project>/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

The samen happens when I drop the file argument and just pipe the output to an xml file. (I saw another issue where you stated that file the file flag cannot be used in combination with xunit).

wouterbulten commented 8 years ago

I did some more experimenting:

First of all the error is thrown in the Mocha.prototype.reporter function as for some reason the reporter variable is not set correctly. It should load the xunit from its built-in reporter list:

    // Try to load a built-in reporter.
    if (reporters[reporter]) {
      _reporter = reporters[reporter];
    }

But, it doesn't as the reporter variable is not set to xunit (it is for other reporters such as tap or json).

There is also nothing wrong with the xunit reporter itself. When I hardcode Mocha to use the xunit reporter, like:

Mocha.prototype.reporter = function(reporter, reporterOptions) {
  reporter = 'xunit';

//rest of function

then the reporter is loaded and the output is correct (as in, I get xml testcases as output).

wouterbulten commented 8 years ago

I think I'm almost there, any feedback would be great!

The problem originates from cli.js:

if ( opts.reporter ) {

  // check to see if it is a third party reporter
  try {
    // I don't want to use isAbsolute here as it could be a node module or a relative path
    if (opts.reporter.indexOf('.') === 0) {
      opts.reporter = fs.absolute(opts.reporter)
    }
    reporter = require(opts.reporter) <---------- This does not fail for 'xunit'
  } catch (e) {
    reporter = opts.reporter
  }
}

I'm not sure why but apparently require('xunit') does not throw an exception but returns an empty object ({}) instead. Other reporters correctly fail this require step.

wouterbulten commented 8 years ago

Found the source of the error:

CasperJS patches the require function which first tries to find modules in the casperjs/modules directory. In that directory is already an xunit module as used by the XUnit testing options of CasperJS itself. This module is then loaded but is not compatible with the Mocha (xunit) reporter. It doens't fail for other reporters as, for now, CasperJS does not support them.

Easiest solution, in my view, would be to add a whitelist of built-in Mocha reporters and skip the require-test for those. Will do a pull request with a candidate solution!

nathanboktae commented 8 years ago

Awesome writeup @wouterbulten ! Yeah CasperJS's patched require has been the bane of this project. It needs to die horribly.

wouterbulten commented 8 years ago

@nathanboktae any plan on releasing a new version to NPM? :) Would love to have this fix available!

nathanboktae commented 8 years ago

Excuse the delay - 0.5.5 is now publishied on npm.

wouterbulten commented 8 years ago

Great! Thanks for the work :)