TheLudd / mocha-gwt

Given When Then for mocha
5 stars 2 forks source link

mocha in browser #8

Open jasonkarns opened 8 years ago

jasonkarns commented 8 years ago

Does anyone have experience using custom interfaces in the browser? We're getting errors when mocha-gwt is loaded at https://github.com/TheLudd/mocha-gwt/blob/de04ed1fceb491f18383afd4312a15e1def3df14/lib/mocha-gwt.coffee#L118 because interfaces doesn't exist on that Mocha object.

And then when we attempt to configure mocha to use mocha-gwt, it throws an error about an unknown UI (since mocha-gwt failed to register itself as a UI).

jasonkarns commented 8 years ago

I found the underlying issue with the interfaces registration and I'm working on a browserify branch to build a browser-friendly distribution. I have the interface registration working well using browserify-shim to expose window.Mocha via require('mocha'). Further, I've changed all the global.* references to use context. as provided by mocha in the suite setup callback.

At this point, mocha-gwt is being registered correctly and the page loads without errors. However, none of the tests are being recognized by mocha (they are not being saved into the mocha suites/tests arrays so mocha.run() doesn't execute anything). Any pointers?

TheLudd commented 8 years ago

I actually have very little insight in how mocha works and I have never used it in the browser. This interface was written mostly by working my way through trial and error to its current state.

If you look at the code for mocha-gwt you'll see that it listens to hoooks. All tests are registerd pre-require and they are added to mocha post-require by doing Suite.add.

I don't know about the browser but "require" seems quite nodish. Perhaps those hooks are not fired in the browser environment?

jasonkarns commented 8 years ago

pre-require is triggered but post-require isn't. (mocha-given and the built-in UIs still use these hooks, even in browser)

I attempted simply emitting post-require at the end of pre-require, which at least executes the handler, but still no worky. Unless there's any more interest in getting mocha-gwt to work in browser, I'm probably just going to bail back to mocha-given :/

On Fri, Sep 25, 2015 at 4:46 PM, Ludwig Magnusson notifications@github.com wrote:

I actually have very little insight in how mocha works and I have never used it in the browser. This interface was written mostly by working my way through trial and error to its current state.

If you look at the code for mocha-gwt you'll see that it listens to hoooks. All tests are registerd pre-require https://github.com/TheLudd/mocha-gwt/blob/master/lib/mocha-gwt.coffee#L25 and they are added to mocha post-require https://github.com/TheLudd/mocha-gwt/blob/master/lib/mocha-gwt.coffee#L71 by doing Suite.add https://github.com/TheLudd/mocha-gwt/blob/master/lib/mocha-gwt.coffee#L107 .

I don't know about the browser but "require" seems quite nodish. Perhaps those hooks https://github.com/mochajs/mocha/blob/master/lib/mocha.js#L216 are not fired in the browser environment?

— Reply to this email directly or view it on GitHub https://github.com/TheLudd/mocha-gwt/issues/8#issuecomment-143349276.

TheLudd commented 8 years ago

I did a very quick search through the mocha code and if this is where mocha emits events in the browser then it seems that mocha-gwt is incompatible with mocha in the browser. I don't know if the implementation is a mistake or if it needs to be that way. Only the pre-hook is emitted and the file is not included.

jasonkarns commented 8 years ago

Right. And the loadFiles implementation (in node) simply emits the three events one after the other. pre-require, then the file is required, then require event, then post-require. So to simulate that, I temporarily emitted post-require as the very last statement in the pre-require handler. But there's still something else wrong.

I should also point out that I set the file argument to 0 as mocha-given doesn't even use the file param, but instead hardcodes 0.

On Fri, Sep 25, 2015 at 4:54 PM, Ludwig Magnusson notifications@github.com wrote:

I did a very quick search through the mocha code and if this https://github.com/mochajs/mocha/blob/ee0a55b572d7da7dd8b7abc418d594602692bbed/support/browser-entry.js#L112 is where mocha emits events in the browser than it seems that mocha-gwt is incompatible with mocha in the browser. I don't know if the implementation is a mistake or if it needs to be that way. Only the pre-hook is emitted and the file is not included.

— Reply to this email directly or view it on GitHub https://github.com/TheLudd/mocha-gwt/issues/8#issuecomment-143352249.

TheLudd commented 8 years ago

Yes, but in mocha-gwt the file parameter is used. Can you make sure that the code inside the if statement here is executed? https://github.com/TheLudd/mocha-gwt/blob/master/lib/mocha-gwt.coffee#L109

jasonkarns commented 8 years ago

I have a proof of concept working and have opened #9 as the results of my work. (partially dependent on https://github.com/mochajs/mocha/pull/1905)