Gem integration tests fail with jQuery not found
Asked Answered
D

1

12

I have a gem called "fantastic" with a dummy app inside to test. When running the dummy app, everything works well.

However, when I run the tests, unit tests works fine, but integration tests (Capybara + Poltergeist) fail with the message:

Sprockets::FileNotFound: couldn't find file 'jquery' with type 'application/javascript'.

Checked in these paths:

/fantastic/spec/dummy/app/assets/images, /fantastic/spec/dummy/app/assets/javascripts, /fantastic/spec/dummy/app/assets/stylesheets, /fantastic/app/assets/javascripts, /fantastic/app/assets/stylesheets, /fantastic/vendor/assets/javascripts

Some of my files:

fantastic.gemspec

# ...
s.add_development_dependency "capybara"
s.add_development_dependency "poltergeist"
s.add_development_dependency "jquery-rails"
# ...

spec/dummy/app/assets/javascripts/application.js

//= require jquery // Error is produced in this line
//= require jquery_ujs
//= require fantastic
//= require_tree .

The thing is that when running rails server everything works fine, and jQuery is loaded properly (I tested it in the browser's console).

Any idea of what's happening on tests?

EDIT

I tried creating a custom Capybara.app:

spec/spec_helper.rb

ENV["RAILS_ENV"] = 'test'
# Require dummy Rails app
require File.expand_path("../../spec/dummy/config/environment", __FILE__)

require 'database_cleaner'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

require 'capybara/dsl'

Capybara.app = Rack::Builder.new do
  map "/spec/dummy/" { run Rails.application }
end.to_app

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, {js_errors: false})
end
Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false
  config.infer_spec_type_from_file_location!
end

Given the dummy app is in spec/dummy/ folder.

And this is the error I get when I run the tests

Failure/Error: expect(page).to have_content "Users" expected to find text "Users" in "Not Found: /users". (However, it was found 1 time using a case insensitive search.) # ./spec/features/record_updated_spec.rb:6:in `block (2 levels) in '

Deodand answered 12/9, 2017 at 15:31 Comment(6)
Due to the unusual location of your app it’s possible the apps root isn’t be correctly detected. You may need to set a custom Capybara.app - see how Capybara defines the default - github.com/teamcapybara/capybara/blob/master/lib/capybara/…Angiosperm
@ThomasWalpole Thanks for your help, but I still can't make it work. I've just added more info, having used your input, can you take a look at my edit please?Deodand
Is this a publically accessible repo? (I don’t see a gem called fantastic in rubygems.org)Angiosperm
It's not a public repo, and I cannot publish it because it's from work. Is there any other file I can share to make it easier?Deodand
Not that I can think of, if it was accessible I was going to try it and debug a bit.Angiosperm
If you can create a repo, with the same setup but none of your companies code, that replicates your issue I’ll look at thatAngiosperm
D
7

This is what ended up fixing the issue.

module Fantastic
  class Engine < ::Rails::Engine
    require 'jquery-rails' # <-- this line

    isolate_namespace Megaphone
  end
end
Deodand answered 15/9, 2017 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.