abepetrillo/evergreen

Is require async?

bradrobertson opened this issue · 3 comments

I'm a but stumped getting some helpers to work in my spec_helper.

I have spec_helper a file like so:

require("/javascripts/my_app.js");

console.log("myNamespace", myNamespace);

my_app.js defines the myNamespace

In my test however, I get an error:

Uncaught reference error myNamespace is not defined

So this leads me to believe that require is asynchronous. BUT, then I don't understand how any of my tests run. (well, they ran before I tried using the namespace in spec_helper) because they obviously make reference to my namespace all over the place.

Can someone explain what's going on here? Also, where do people place helpers that use the same namespace defined in your app source js. I also tried making a support folder and doing a require("./support/helpers.js"); but received some weird errors.

I'm basically trying to stub out an object that normally gets populated from a rails model.

Yes, require is async. Note that while the test code is loaded immediately, it isn't actually executed until the page has fully loaded (we're using an onload callback). So when your tests run, your JS file will have been loaded, but immediately after, it may not have. Try putting that console.log inside the outermost describe block and see if that fixes it.

P.S.: Evergreen has a mailing list, which is a better forum for questions like this than the issue tracker ;)

thanks I'll use the mailing list in the future.

console logging inside my outermost describe will definitely work, but that's not my ultimate goal. I want to include a file for all tests that stubs out a particular object from my src with some stub data.

I would suggest that you somehow make this not load order dependent. An easy way is to wrap the whole stub file in a function and call that at your convenience.