jejacks0n / teaspoon

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

Include relative URL root in JS setting Teaspoon.root if present. #166

Closed kowal closed 10 years ago

kowal commented 10 years ago

Currently we can use Teaspoon in application deployed under custom context root (see #133) - let's say http://localhost:3000/some-app/teaspoon/default. However when I want to return from filtered specs, using 'Remove' link - it returns to http://localhost:3000/teaspoon/default, which is wrong.

Simplest solution is to alter Teaspoon.root in the template.

What do you think? Any idea at which level this should be tested?

jejacks0n commented 10 years ago

Do path helpers utilize this custom context root? after dealing with this in a different way, I think I may make some adjustments to the javascript / views to have more of this work being done in rails -- e.g. using path helpers.

I initially avoided that to some degree, because I wasn't sure the directions it might go, but at this point it seems like we should consider a better approach.

Just probing for some thoughts.

kowal commented 10 years ago

See https://github.com/modeset/teaspoon/pull/133#issuecomment-33458040 for all path helpers in teaspoon which uses context root.

On the other hand, there are https://github.com/rails/rails/issues/9058, https://github.com/rails/rails/issues/4308 or https://github.com/rails/rails/issues/5122 - which indicate that this is problem in all rals engines and it might be fixed in Rails some day..

jejacks0n commented 10 years ago

Yeah, I came across a lot of the same info. I'm planning on doing this in areas where it's possible for now, and doing this by other means (which I wanted to fix because they're ugly) where that's not possible (for the time being).

Thanks for the info. Would you mind giving the 0.8 branch a look over to make sure this is handled well when I feel that's ready to be looked at? I would appreciate the help.

jejacks0n commented 10 years ago

Or help by providing a some steps to setup the spec/dummy app in this way so it can be tested easily and we can make sure everything passes with relative_url_root enabled?

jejacks0n commented 10 years ago

I fixed this in 0.8 by using window.location.pathname. I want to move away from this concept of "context" as much as possible, so thanks for bringing it to my attention and helping me sort out what it solves. Your time is much appreciated.

jejacks0n commented 10 years ago

There was considerably more to this, and so I've adjusted the dummy app to be setup in such a way that this works. The key here (in general) is that asset paths have to be adjusted as well.

And then if you need teaspoon to be within that same relative_url_root (it's not needed) you can set the mount_at configuration to "/relative/path/teaspoon"

kowal commented 10 years ago

Cool. I'll check 0.8 branch and let you know if everything works fine.

kowal commented 10 years ago

@jejacks0n, I update my test rails application (it's pushed to https://github.com/kowal/rails4_teaspoon_test_app) and indeed hitting http://localhost:3000/app/teaspoon/ works fine (I can see test suites list, no errors), however hitting http://localhost:3000/app/teaspoon/default ends up with errors in JS console:

Uncaught ReferenceError: jasmine is not defined teaspoon-jasmine.js:1153
(anonymous function) teaspoon-jasmine.js:1153
(anonymous function) teaspoon-jasmine.js:1335
Uncaught ReferenceError: describe is not defined application_spec.js:2
(anonymous function) application_spec.js:2

Any idea why this isn't working?

jejacks0n commented 10 years ago

Ah, are you using the new "use_framework" directive? Check the readme for it.

jejacks0n commented 10 years ago

https://github.com/modeset/teaspoon/blob/master/spec/dummy/config/application.rb#L30

Also, in general you have to configure the asset prefix along with the relative URL root, but assume you knew this.

kowal commented 10 years ago

Yes, I've regenerated teaspoon files and now I can successfully run specs in browser. Yay!

However, I'm getting this only when I do not set config.assets.prefix. My setup in Rails is:

# development.rb
..
config.relative_url_root = "/app"

# config.ru
require ::File.expand_path('../config/environment',  __FILE__)
map TeaspoonTest::Application.config.relative_url_root || "/" do
  run Rails.application
end

Also, running via CLI still doesn't work (either rake or binary):

$ bundle exec teaspoon
Starting the Teaspoon server...
Teaspoon running default suite at http://127.0.0.1:56797/teaspoon/default
SyntaxError: Parse error
SyntaxError: Parse error
ReferenceError: Can't find variable: Teaspoon
  # http://127.0.0.1:56797/teaspoon/default?reporter=Console:13
SyntaxError: Parse error
...
SyntaxError: Parse error
ReferenceError: Can't find variable: Teaspoon
  # http://127.0.0.1:56797/teaspoon/default?reporter=Console:25

Failed to load: http://127.0.0.1:56797/teaspoon/default?reporter=Console
kowal commented 10 years ago

Everyting is pushed to https://github.com/kowal/rails4_teaspoon_test_app

kowal commented 10 years ago

See also my https://github.com/modeset/teaspoon/pull/133#issuecomment-33458040 - about error in command line.

jejacks0n commented 10 years ago

Ok, so a few things.. this is obviously confusing, and I think you have some basic rails setup incorrect.. so let me know if this isn't the case -- it took me some toying around to get it sorted.

First, you'll notice that the spec/dummy app in teaspoon doesn't do the wrap that you have in your config.ru.. this seems like it's a legacy thing, and is incorrect so I believe it should be removed.

Second, the asset prefix MUST be set, it's not even related to Teaspoon, that shit just seems wonky and can't be resolved by Teaspoon alone. Literally, your asset paths will be wrong everywhere if you set relative_url_root and not the asset.prefix. I dug into the code for this, and it makes some sense for when the assets are precompiled, but it doesn't seem to take into account when assets aren't precompiled. I wasn't compelled to fix it it sprockets-rails.

Ok, so with all of that said, it looks like your project is doing too much.. I found that proper setup (at least with rails 4, and that's all I'm caring about here), is to set config.relative_url_root in your application -- or via the ENV["RAILS_RELATIVE_URL_ROOT"], and then simply set your assets.prefix to relative_url_root + "/assets".

That's it in a nutshell. There should be nothing else you need to, so remove the extra stuff as I bet your project won't work properly, Teaspoon or not.

As a follow up, if you want Teaspoon to be nested within that same path, just adjust your config.mount_at to be relative_url_root + "/teaspoon". I almost put this into the routes file, but liked the fact that you can do it yourself -- or not.

kowal commented 10 years ago

Thanks for your patience with this one :) Correct me if I'm wrong:

I think we can close it for now and I have to investigate something else than wrapping in config.ru, which does not break teaspoon in command line.

Again, thanks!

jejacks0n commented 10 years ago

point 1: partially correct. a. the dummy app is not nested within a subdirectory, though all urls act as though it were, so it doesn't matter where the root of it is. b. as mentioned a few times in documentation, as well as this thread, teaspoon can be, placed within this relative url root by adjusting the mount_at configuration.. I chose not to do it because as I work on it I don't feel like typing /relative/teaspoon.

point 2: you can also wrap your routes within a scope, or just configure it how it seems like it wants to be configured via http://guides.rubyonrails.org/configuring.html. I went by that, and I did come across the config.ru thing, but it seemed like that was a bit legacy.

The problem, after adjusting the dummy app to be setup like yours, is that teaspoon doesn't fire up your server using the config.ru file.. it fires it up directly through rack, and that's why I feel like putting that map call inside of config.ru is wrong -- at least it complicates things to a pretty insane degree from teaspoons perspective -- in how it starts the server. And this is why I ended with the solution I provided. Can you explain to me where my suggested configuration and setup falls down for you so I can better understand why you want to do it that way?

jejacks0n commented 10 years ago

The only configuration that I could get to work for both the console, and in the browser was to set the asset.prefix conditionally based on if Teaspoon::Console was defined. This class is only loaded in the case that it's being run via the console, and thus allows for it to be different between the two environment.

config.assets.prefix = "/relative/assets" if defined?(Teaspoon::Console)

Though, what I recommend is to remove the map call in the config.ru file and if for some reason you need some additional behavior that the recommended configuration doesn't provide, wrap your routes in a scope:

scope Rails.application.config.relative_url_root do
   # your routes
end

It should also be mentioned that I haven't used relative_url_root in rails, nor have I ever needed to -- and really have no expertise or deep understanding of this aspect. I just prefer simple solutions, and the one I came to fit the bill.

kowal commented 10 years ago

I've tried again on fresh rails 4 application, this time I simply used Trinidad server to run application in sub directory. Almost everything works fine :)

To sum up:

config.relative_url_root = "/relative"
# this must be set for any asset paths to be correct!
config.assets.prefix = "/relative/assets" if defined?(Teaspoon::Console)
---
  context_path: /relative

Results:

SyntaxError: Parse error
ReferenceError: Can't find variable: Teaspoon
 # http://127.0.0.1:57969/teaspoon/default?reporter=Console:13
...

Let's forget legacy config.ru hack. The only thing left to fix is the difference between rake task and teaspoon binary.

I've pushed all this into new repo at https://github.com/kowal/rails4_on_trinidad.

jejacks0n commented 10 years ago

Configure your server type?


Jeremy Jackson

On Mar 6, 2014, at 2:58 PM, Marek Kowalcze notifications@github.com wrote:

I've tried again on fresh rails 4 application, this time I simply used Trinidad server to run application in sub directory. Almost everything works fine :)

To sum up:

Rails 4.0.1, Teaspoon branch 0.8 Set up rails in config/application.rb config.relative_url_root = "/relative"

this must be set for any asset paths to be correct!

config.assets.prefix = "/relative/assets" if defined?(Teaspoon::Console)

Set up Trinidad in config/trinidad.yml

context_path: /relative Run app via rails s trinidad Results:

Web UI at http://localhost:3000/relative/teaspoon/default - OK teaspoon / bundle exec teaspoon - OK rake teaspoon - ERROR: SyntaxError: Parse error ReferenceError: Can't find variable: Teaspoon

http://127.0.0.1:57969/teaspoon/default?reporter=Console:13

... Let's forget legacy config.ru hack. The only thing left to fix is the difference between rake task and teaspoon binary.

I've pushed all this into new repo at https://github.com/kowal/rails4_on_trinidad.

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

kowal commented 10 years ago

Configure your server type?

Sorry, but I don't get what you are asking about?

jejacks0n commented 10 years ago

Ok, so you start rails with trinidad, right? Teaspoon also starts rails behind the scenes from the command line. How is teaspoon to know what server type you want it to start rails with?

So, check the configuration for server. Set it to trinidad. I have no idea if this works, but it should in theory. :)


Jeremy Jackson 303-507-6846

On Mar 7, 2014, at 12:42 AM, Marek Kowalcze notifications@github.com wrote:

Configure your server type?

Sorry, but I don't get what you are asking about?

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

kowal commented 10 years ago

I'm getting SyntaxError: Parse error only when running rake teaspoon. In browser everything is OK, teaspoon binary also works fine.

Both Teaspoon::CommandLine and teaspoon.rake invokes at some point Teaspoon::Console.new(options).failures? with same arguments and somehow rake is doing things differently - is this expected behaviour? Are you saying that server might be configured differently in those two cases?

Also:

So the issue I'm complaining about now is inconsistency between running rake and bin/teaspoon script when rails are configured with relative_url_root (this is important to reproduce it).

I was trying to debug it more closely, unfortunately without any results... :(

jejacks0n commented 10 years ago

It's the same code path for both rake and the cli. I'm not sure why it would behave differently. I'd be happy to accept a pull request that fixes it, or you can create project that exhibits the behavior.