travisjeffery / mocha-teamcity-reporter

MIT License
40 stars 48 forks source link

Running inside mocha-webpack fails #45

Closed fungus1487 closed 5 years ago

fungus1487 commented 6 years ago

The specific line https://github.com/travisjeffery/mocha-teamcity-reporter/blob/master/lib/teamcity.js#L18 decides whether to pull the reporters base from mocha import or phantomjs. When running tests inside mocha-webpack (e.g. using Vue.js) the reporter fails with cannot resolve module require('./base')

The fix should be in the test typeof window === 'undefined' to check if the environment being run in is mocha-webpack and if so simply use the first part of the if statement thus assuming mocha.

Currently I have simply copied the code into our project and replaced:

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

with

// running in Node
Base = require('mocha').reporters.Base;
log = console.log;
logError = console.error;

This will work as a fix for us but an actual fix to support mocha-webpack would be greatly appreciated and open up this tools use to vue.js projects on 2.0+

iamdudeman commented 6 years ago

I just came across this issue today and I came to the same conclusion as fungus1487. For a little more context I believe in my case it was because I am using jsdom instead of phantomjs

jamie-sherriff commented 6 years ago

Ah yep I can see how this is a problem. Could someone provide a simple end to end example pulling in mocha webpack and this reporter for me?

lreading commented 6 years ago

I ran into this issue after setting up a new project using the Vue.js CLI.

@jamie-sherriff - I put together a simple vue client that reproduces it for you. https://github.com/lreading/mocha-webpack-with-teamcity-reporter

Run the following:

You'll get the error noted above.

In case you happen to notice... I had to specify node_modules/mocha-teamcity-reporter because there's a separate issue with the mocha-webpack plugin with finding reporters, not at all related to this project.

jamie-sherriff commented 6 years ago

@lreading Awesome thanks for the easy example, i will take a look it.

jamie-sherriff commented 6 years ago

@lreading Sorry for the delay. Have published a workaround in the branch fix-mocha-webpack Can install using npm install travisjeffery/mocha-teamcity-reporter#fix-mocha-webpack) it works on your sample so hopefully solves your problem?

With phantomjs now being deprecated i am tempted to remove the specific logic for it but will probably have to wait for a major version bump.

lreading commented 5 years ago

@jamie-sherriff - Works great! Thanks for taking this on!

I think it'd be wise to wait for the next major version bump, as there's likely some co-dependencies hanging around in some projects.

Unless you need it for anything else, I'll probably remove that git repo I set up to reproduce.

Thanks again! :tada:

jamie-sherriff commented 5 years ago

No problem! @lreading thanks for your help as well now closing and released under 2.5.1