nathanboktae / mocha-casperjs

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

Third party reporters reporting errors on require #58

Closed thekevinscott closed 9 years ago

thekevinscott commented 9 years ago

This is pretty minor, but I just bumped my version of Mocha to 2.2.5 and am receiving some warnings when trying to load a third party reporter.

Here's a repo showing what I mean.

Calling Mocha directly produces:

./node_modules/mocha/bin/mocha test.js --reporter=foo

pass: foo bar
end: 1/1

Calling Mocha-Casperjs produces:

./node_modules/mocha-casperjs/bin/mocha-casperjs test.js --reporter=foo

"foo" reporter blew up with error:
Error: failed to require "foo"
    at require (/Users/kscott/Development/exploringmocha/node_modules/mocha/mocha.js:8)
    at /Users/kscott/Development/exploringmocha/node_modules/mocha/mocha.js:33
    at /Users/kscott/Development/exploringmocha/node_modules/mocha/mocha.js:1551
    at ./node_modules/mocha-casperjs/bin/cli.js:94
pass: foo bar
end: 1/1

The reporter is still included perfectly fine, but it looks like this commit adds in some console.warn statements. Not sure if there's a way around these?

nathanboktae commented 9 years ago

Good find. Maybe during this try/catch while we attempt to load the reporter, we can capture stderr and stdout.

thekevinscott commented 9 years ago

Tried temporarily capturing console.warn which works, but what do you think about trying to require the reporter first (and catching the error in cli.js)? Something like:

https://github.com/nathanboktae/mocha-casperjs/pull/59

nathanboktae commented 9 years ago

I'll put comments in that PR. Pro tip: you can attach a pull request to an existing issue via the github cli hub.

thekevinscott commented 9 years ago

Oh nice, I did not know! Thanks for that.

nathanboktae commented 9 years ago

Closed since #59 got merged.

sspringer82 commented 9 years ago

I tried this just as you described and with the dot reporter everything works fine. But there's a problem with 3rd party reporters for mocha such as mocha-teamcity-reporter.

Do you plan to solve that stuff or is this beyond the scope of the project? And if there should be a solution, how can i assist? ;)

nathanboktae commented 9 years ago

Do you plan to solve that stuff or is this beyond the scope of the project?

The core issue is that the reporter is not running in node.js nor the browser, but in phantomjs. That environment supports CommonA/JS require syntax, but doesn't use npm paths to load and is not node so process is not there, streams, etc. fs is very different for example. So writting a generic node.js api to phantomjs api is very beyond the scope of the project and not reasonably achievable anyway IMO.

Oddly though once people become aware of it, it usually takes very little effort to support mocha-casperjs. looking at the teamcity reporter:

if (typeof window === 'undefined') {
  // running in Node
  Base = require('mocha').reporters.Base;
  log = console.log;
} else {
  // running in mocha-phantomjs
  Base = require('./base');
  log = function(msg) { process.stdout.write(msg + "\n"); };
}

should go to this:

if (typeof Mocha !== 'undefined') {
  // running in mocha-phantomjs or mocha-casperjs that use the pre-compiled version of mocha
  Base = Mocha.reporters.Base;
  log = function(msg) { process.stdout.write(msg + "\n"); };
} else {
  // running in Node
  Base = require('mocha').reporters.Base;
  log = console.log;
}

So try that and submit a PR to them?