jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

require javascript file does not seem to add available functions #416

Closed terevos closed 9 years ago

terevos commented 9 years ago

When requiring javascript files that are outside the application.js rails pipeline, js functions do not seem to be available. My application.js does not reference users/edit.js.erb, so I require it manually.

--- user_edit_spec.coffee ---

#= require users/edit
describe "edit_user", ->
  it "check for js functions defined in assets", ->
    expect( new applicationJsFunction ).toBeDefined()
  it "check for js functions required manually", ->
    expect( new userValidate ).toBeDefined() 

--- edit.js.erb ---

function userValidate() {
  var someStuff = true;
  return someStuff;
}

Failures:

1) edit_user check for js functions required manually Failure/Error: ReferenceError: Can't find variable: userValidate in http://127.0.0.1:51657/dev-assets/user_edit_spec.js?body=1

terevos commented 9 years ago

Not sure what other info you might need, but I'm happy to provide more.

mikepack commented 9 years ago

Requiring files from a spec follows the same rules as requiring files from implementation. They both use the asset pipeline.

I noticed your file has a .erb extension. Is it in app/assets/javascripts or somewhere in you config.assets.paths?

Since the test actually runs, it looks like the asset pipeline is functioning and you have something wrong within your code. You're calling new userValidate without parenthesis. Do you mean to say new userValidate()?

jejacks0n commented 9 years ago

I think it has to be within the asset path -- there should be an exception if the file fails to load (90% sure on this).


Jeremy Jackson

On Oct 3, 2015, at 9:22 AM, Mike Pack notifications@github.com wrote:

Requiring files from a spec follows the same rules as requiring files from implementation. They both use the asset pipeline.

I noticed your file has a .erb extension. Is it in app/assets/javascripts or somewhere in you config.assets.paths?

Since the test actually runs, it looks like the asset pipeline is functioning and you have something wrong within your code. You're calling new userValidate without parenthesis. Do you mean to say new userValidate()?

— Reply to this email directly or view it on GitHub.

terevos commented 9 years ago

Thanks for getting back to me so quickly.

mikepack,

users/edit.js.erb is under app/assets/javascripts. It's not in the assets pipeline (for various reasons).

The test does run, yes, but it doesn't matter whether I use parens for userValidate or not. I get the same result. table_prep test returns true and userValidate gives the exception. Those two functions are very similar in the assets.

I've even tried copy and pasting userValidate function into the table_prep.js - it then loaded up fine and returned true.

So only when it's outside the assets pipeline do I have trouble. If I manually require a javascript, it doesn't seem to make the functions in that js asset file available in the spec.

mikepack commented 9 years ago

Thanks a ton for submitting this issue. Teaspoon is used pretty heavily and this has never come up. Since the asset seems to be loaded properly (you're not getting a asset pipeline compilation error), this feel like it's something with your code. I'm happy to keep thinking about this issue, but will close for now.

If you could set up a new Rails app, add the Teaspoon gem, reproduce this issue in the app, open source it, and point me to your app, I would be happy to review.