testdouble/cypress-rails

after_state_reset hook isn't getting invoked

Closed this issue · 6 comments

TL;DR
For some reason, the after_state_reset hook isn't getting invoked, though all the other hooks are.

Long version
Rails version: 7.0.2.3
Ruby version: 3.1.1p18

Here is my initializers/cypress_rails.rb

return unless Rails.env.test?

CypressRails.hooks.before_server_start do
  puts "!!!!before_server_start\nCYPRESS_RAILS_TRANSACTIONAL_SERVER:#{ENV['CYPRESS_RAILS_TRANSACTIONAL_SERVER']}"
end

CypressRails.hooks.after_server_start do
  puts "!!!!after_server_start\n\n"
end

CypressRails.hooks.after_transaction_start do
  puts "!!!!after_transaction_start\n\n"
end

CypressRails.hooks.after_state_reset do
  puts "!!!!after_state_reset\n\n"
end

CypressRails.hooks.before_server_stop do
  puts "!!!!before_server_stop\n\n"
end

How I am starting my Rails server

RAILS_ENV=test CYPRESS_RAILS_TRANSACTIONAL_SERVER=true bundle exec rails s

How I am starting cypress, and also the initial log.

CYPRESS_RAILS_TRANSACTIONAL_SERVER=true rake cypress:open

cypress-rails configuration:
============================
 CYPRESS_RAILS_DIR....................."/Users/aaron.bartell.../my_app"
 CYPRESS_RAILS_HOST...................."127.0.0.1"
 CYPRESS_RAILS_PORT....................nil
 CYPRESS_RAILS_BASE_PATH..............."/"
 CYPRESS_RAILS_TRANSACTIONAL_SERVER....true
 CYPRESS_RAILS_CYPRESS_OPTS............""

!!!!before_server_start
CYPRESS_RAILS_TRANSACTIONAL_SERVER:true
!!!!after_transaction_start

Starting Puma...
* Version 5.6.4 , codename: Birdie's Version
* Min threads: 0, max threads: 4
* Listening on http://127.0.0.1:49814
!!!!after_server_start


Launching Cypress…

In my cypress spec I have the following so I can confirm the call to /cypress_rails_reset_state is in fact being made.

  beforeEach(() => {
    console.log('beforeEach')
    cy.request('/cypress_rails_reset_state').then(
    (response) => {
      console.log(response)
    })
  })

Here's the console output.
image

I've downloaded and run the example app in this project and it invokes the after_state_reset hook as expected.

I am curious if somebody could give me another idea of what to look at next. Thanks in advance.

If the example app is working, I'm kind of at a loss. Other than verifying the example app also works at whatever your version of Rails is, I think we'd need a reproducible example to help you debug this

Thanks for the response, @searls.

UPDATE:
I did a bundle open 'cypress-rails' and started adding puts all over the place to learn what was going on.

Turns out the Rails.application.executor.to_run (here) isn't getting invoked in my app like it is in this repo's example app. I have both my app and the example app on the same version of Rails and Ruby.

I've not used to_run before now and am learning its ins and outs.

I will continue debugging and update this issue with my findings. Stay tuned! And thanks for this very cool tool!

If you run

$ RAILS_ENV=test bin/rails middleware

Do you see:

use ActionDispatch::Executor

Near the top?

Yes, it shows up in the list. Here's the output of that command for my app.

$ RAILS_ENV=test bin/rails middleware
use ActionDispatch::HostAuthorization
use Rack::Sendfile
use ActionDispatch::Static
use ActionDispatch::Executor
use ActiveSupport::Cache::Strategy::LocalCache::Middleware
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use ActionDispatch::RemoteIp
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::ActionableExceptions
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::RedisStore
use ActionDispatch::Flash
use ActionDispatch::ContentSecurityPolicy::Middleware
use ActionDispatch::PermissionsPolicy::Middleware
use Rack::Head
use Rack::TempfileReaper
use ExceptionNotification::Rack
run MyApp::Application.routes

I ran the same for the cypress-rails/example app and below is a diff of the output of the aforementioned command.

Do you think any of those changes would cause the difference in functionality?

image

The issue has been discovered! It was between the keyboard and the chair 😬

In the README.md it says the following...

When using Rails, however, you'll also want your Rails test server to be running so that there's something for Cypress to interact with. cypress-rails provides a wrapper for running cypress open with a dedicated Rails test server.

In my brain, I was focusing on the fact that I needed a test server running and didn't focus on the last part of the sentence that declares that's already being done for me. 😛

Said another way, I had two terminals open with one running RAILS_ENV=test rails server and the other running rake cypress:open and that ended up causing the issue for why the one hook wasn't being called.

My co-worker Tim actually found the issue. I just figured I needed to own up to being bad at reading the instructions.

Thanks for a solid tool, @searls.

Glad you got sorted!