'How to test react-rails components in rspec feature tests using Cuprite & Webdrivers

I am updating an older app's Ruby and Rails versions and attempting to get all of the feature tests passing. Most of them were failing due to still relying on capybara-webit. I have replaced that with webdrivers and cuprite and that fixed about half of them. Still, all of the injected react components (using react-rails) aren't rendered when running tests (screenshots prove this).

I have added the gems and am using this config in my spec_helper.rb file:

Capybara.register_driver(:cuprite) do |app|
  Capybara::Cuprite::Driver.new(
    app, 
    window_size: [1200, 800],
    inspector: true
  )
end
Capybara.default_driver = Capybara.javascript_driver = :cuprite

I've also tried to get this working with a :headless_chrome driver, and that worked even less. With that config, the page also didn't render the erb parent container of the injected react component. I also tried adding in a sleep where components were rendering, which didn't work. Is there a way to get react components to render in test with this setup?



Solution 1:[1]

Do they render in dev mode? If so the issue is likely the difference in asset handling between the two modes. In test mode (and production) the assets are packaged to reduce the number of requests. This means an error in one JS file can prevent the rest of the JS from executing. Check your browser console for JS errors and fix them all

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Walpole