2

After migrating from jquery_ujs to rails-ujs, certain JS tests are failing and proving very difficult to debug.

The problem appears to be that Jquery objects are only initialized for the first test in a sequence. Everything works fine in my development environment.

After upgrading to Rails 5.1 everything was working with jquery_ujs. I then made only one change in application.js.

#added this
//= require rails-ujs
# removed this
//= require jquery_ujs

Here's an example. Two almost identical system tests for interaction with a select2 field.

it "selects recognized search strings", :js do
  expect(MyRemoteSelectBuilder).to receive(:search_query).
    with("recognized_string").once.
    and_return(
      [
        id:   1,
        name: "Recognized Name"
      ]
    )

  visit new_my_object_path 
  # triggers the select2 
  is_expected.to have_selector('.select2-container', visible: false)
  first('.select2-container', minimum: 1).click
  # do some more stuff
end
it "does not select unrecognized search strings", :js do
  expect(MyRemoteSelectBuilder).to receive(:search_query).
    with("unrecognized_string").once.
    and_return(
      [ ]
    )

  visit new_my_object_path 
  # triggers the select2 
  is_expected.to have_selector('.select2-container', visible: false)
  first('.select2-container', minimum: 1).click
  # do some more stuff
end

When run together, one of these tests will fail with

expected to find css ".select2-container" but there were no matches  

If I focus and run only one or other of these tests, they will pass.

I considered whether this might be a turbolinks issue (I don't think it is as the tests pass sometimes). Nonetheless I have tried various ways to initialize Select2 with Turbolinks but without success.

(($) ->
  $(document).on 'turbolinks:load', ->
    InititializeSelect2.init()
    return
  return
) jQuery

# or 
$ ->  
  InititializeSelect2.init()
# etc

Is there a fundamental difference between rails-ujs or jquery_ujs that could affect jquery initialization between tests? Have I overlooked some configuration? What would be logical steps to debug this issue?

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
  • Turbolinks can definatly effect js execution as the `ready` event is only fired on the initial page load and not when you click a link and turbolinks replaces the contents of the document. – max Mar 23 '18 at 12:07
  • This could be a timing issue though as the expectation may run before the javascript has a chance to be executed. You can try using `wait_until` which will wait until the default capybara timeout. This can often lead to flopping tests. – max Mar 23 '18 at 12:11
  • Thanks @max, unfortunately increasing the timeout, or indeed sleeping before this expectation, does not fix the issue. Though as I test this more, it is inconsistent. Sometimes passing, sometimes failing. Are you aware of any upgrade guides that describe the `rails_ujs` config? I’ve not found much details, and must have set something incorrectly. – Andy Harvey Mar 24 '18 at 07:38
  • No sorry I was was not even aware that they moved jquery_ujs back into the core. I would try disabling turbolinks as it can cause plenty of issues - especially when used with javascript libs that are not idempotent. – max Mar 27 '18 at 13:46
  • Did you find an answer to this question? – Marklar Jul 03 '18 at 06:27

0 Answers0