nathanboktae / mocha-phantomjs

:coffee: :ghost: Run client-side mocha tests in the command line through phantomjs
MIT License
954 stars 112 forks source link

sometimes it breakes. #8

Closed frede closed 11 years ago

frede commented 11 years ago

hello!

i really enjoy your work, but it breaks sometimes :) its not the best, when its not reliable... i use the following tools an plugins:

requirejs loads (via config.js):

and sometimes i get my specs done, and when not i get this message:

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

seems to me, that sometime node don't come up right or sth like it. but when i do loop sth like

if (process) {
    console.log(0);
} else {
    console.log(1);
}

i dont get failures.. any ideas?

metaskills commented 11 years ago

You are likely requiring the wrong mocha.js, you want the root mocha.js from their project root which is built for the browser. Not the lib/mocha.js. Hope that helps.

metaskills commented 11 years ago

Did you solve this?

jeffbski commented 11 years ago

@metaskills it is not the mocha.js that is causing the problem, I am seeing the same behavior.

I am definitely using the right mocha, I use the current version of mocha for both my server and browser tests 1.6.0 and yes, the one being used for browser tests is the browser version.

My browser tests running in the browser all work fine.

From what I can tell, it looks like process is sometimes not setup by the time your core_extension.js is trying to use it. So it fails some of the time. Rerun again and it might work or might not.

Also it seems like it fails more often if you try to specify a reporter. It is the same error regardless, always complaining about process not existing.

So it has something to do with process not being created yet, when core extensions is trying to use it.

jeffbski commented 11 years ago

I am still trying to debug, but it seems that if I put this in your core_extensions.js above the first reference to process that it fixes the problem

  // create these if not already created
  if (typeof(process) === 'undefined') process = {};
  if (!process.stdout) process.stdout = {};

I will run some more tests, and if successful will send you a pull request

metaskills commented 11 years ago

Yea, that would solve the symptom right? Still do not know the core issue? I wish I had some code that duplicates so I can test too. Thoughts?

jeffbski commented 11 years ago

I think I might have another clue, I believe the problem is caused by 'use strict'; in one of the scripts I am loading

'use strict'; changes the js engine so that it doesn't access globals and I bet that this is causing the problem, that sometime one script loads that has this in the global scope (and should have been inside the function).

So when process is being setup, it is not setting the global window.process because of use strict.

I will look into more tomorrow.

metaskills commented 11 years ago

Good call! Looking forward to it and thanks for your help!

jeffbski commented 11 years ago

I was wrong the 'use strict'; issue wasn't the problem because after I finally removed it everywhere, it still breaks some of the time.

I looked at the code and I believe I understand what the issue is.

I think that when we are loading mocha-phantom.coffee, that sometimes it loads before mocha is done, since it is being kicked off by phantomjs and not governed the order in the HTML.

I see in your code where it sets up the reporter immediately, then goes into a wait loop until window.mochaPhantomJS.started is true.

I am thinking that if we didn't do the reporter setup, until mochaPhantomJS.run() is called and then immediately do the runMocha() everything will be fine.

I think everything can kick off from the run() method other than the minimal setup needed to create the window.mochaPhantomJS variable and that way we know things are all loaded and ready to execute.

Otherwise you just don't know if things are ready or not.

I will see if I can change the code as I indicated and if that resolves things.

jeffbski commented 11 years ago

I believe this random breaking will be fixed by my pull request #13

metaskills commented 11 years ago

Close this one and focus on #13