-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
after
hook for system tests has no access to the page
#2526
Comments
After some debugging, I found a workaround. I added this to my rails_spec.rb file so it sets the before_teardown function of the system specs so when it executes the
|
By looking at the adapter, and that after { dump_js_coverage } in the example itself, it would execute after
It boils down to the order of hook definitions on what runs first, your One way around this I can think of is to use a shared context: RSpec.shared_context "dump JS coverage" do
after { dump_js_coverage }
end
RSpec.configure do |config|
config.include_context "dump JS coverage", type: :system
end Monkey-patching # spec/rails_helper.rb
module DumpJSCoverage
def before_teardown
dump_js_coverage
ensure
super
end
end
RSpec.configure do |config|
config.include DumpJSCoverage, type: :system
... Unfortunately, I failed to quickly validate any of those theories due to webdrivers failing to find Chrome binary. Do you think there's anything actionable left? |
Thank you for the quick reply and great suggestions! The shared context worked (page still have access to the Adding Including the DumpJSCoverage module for system tests didn't work (page is blank and I like the shared content workaround/solution, the monkey patch felt too hacky. So I'm wondering, is the current behavior the expected one? or is it still a bug? maybe I was expecting the wrong things and this can be closed if that's the case, I understand that at some point in time the |
It is indeed confusing that This would be a breaking change, though, as now users' config-level I suggest adding documentation for this case when you need to run something before |
We document the order of hooks, but I don't think we document where in that order we integrate Rails helpers which makes this confusing, I do sort of think this is a bug but as we use RSpec to integrate Rails here and RSpec Core has no distinction that matches before / after teardown its sort of luck of the draw, we could possibly use prepend_after for Rails integrations which would sort of emulate these options. |
The narrow definition of this issues given in the title (after hooks have no access to page) should now be resolved in #2596. (However the issues around documentation are not resolved. The after_teardown hooks are now being run as late as possible (in an around hook). This also means that a user who wants to run something after those after_teardown hooks may still find that tricky.) |
What Ruby, Rails and RSpec versions are you using?
Ruby version: 2.6.5
Rails version: 6.1.4.1
RSpec-core: 3.10.1
RSpec-rails: 5.0.2
Observed behaviour
When trying to get the JavaScript code coverage data from the window object on an after hook (shown here https://jtway.co/collecting-javascript-code-coverage-with-capybara-in-ruby-on-rails-application-d0cb83a86a90), when the hook is executed, the page is blank.
The same code using MiniTest works fine (the before_teardown runs before the test is done so the page is at the last state) and it seems like at some point this worked fine on RSpec too since that blogpost describes the process and I've seen the same in other places.
It seems like rspec's
after
is equivalent to theafter_teardown
instead of thebefore_teardown
hook from minitest?Expected behaviour
I would expect the hook to have access to the page as it was at the end of the execution of the example to be able to get the code coverage information from the
window
object.Can you provide an example app?
https://github.com/fastruby/js-coverage-sample-app this sample app has both MiniTest and RSpec configured with some code related to getting the JS test coverage.
The only way I'm able to get the JS coverage data is calling the
dump_js_coverage
method at the end of the test (inspec/system/index_spec.rb
). If I try to use an after hook inspec/rails_helper.rb#65
, runningpage.evaluate_script("window.__coverage__")
returns nil instead of the coverage data.You can run
COVERAGE=true rails spec
to try this.Same function is being used as a
before_teardown
hook for MiniTest attest/application_system_test_case.rb
.You can run
COVERAGE=true rails test:all
to try this.The text was updated successfully, but these errors were encountered: