railsのフィーチャースペックをpoltergeistを用いてjenkins上で回しているのですが、

> エラーメッセージ
>
> One or more errors were raised in the Javascript code on the page. If you
> don't care about these errors, you can ignore them by setting js_errors:
> false in your Poltergeist configuration (see documentation for details)....
>
> スタックトレース
>
> One or more errors were raised in the Javascript code on the page. If you
> don't care about these errors, you can ignore them by setting js_errors:
> false in your Poltergeist configuration (see documentation for details).
>
> ReferenceError: Can't find variable: $td  
>  ReferenceError: Can't find variable: $td  
>  at h ttp://127.0.0.1:38079/assets/admin/application.js:14065  
>  [object Object]  
>  [object Object]  
>  at h ttp://www.googletagmanager.com/gtm.js?id=HOGE:51  
>  at h ttp://www.googletagmanager.com/gtm.js?id=HOGE:59
> (Capybara::Poltergeist::JavascriptError)  
>  Capybara::Poltergeist::JavascriptError:  
>  One or more errors were raised in the Javascript code on the page. If you
> don't care about these errors, you can ignore them by setting js_errors:
> false in your Poltergeist configuration (see documentation for details).
>
> ReferenceError: Can't find variable: $td  
>  ReferenceError: Can't find variable: $td  
>  at h ttp://127.0.0.1:38079/assets/admin/application.js:14065  
>  [object Object]  
>  [object Object]  
>  at h ttp://www.googletagmanager.com/gtm.js?id=HOGE:51  
>  at h ttp://www.googletagmanager.com/gtm.js?id=HOGE:59  
>  ./spec/features/admin/hoge_spec.rb:237:in `block (4 levels) in '

のように、javascriptのエラーが出て、落ちてしまいます(hoge_specの名前およびurlのidを変更しています。また、urlを含めるなと言われたので、httpをh
ttpとしています)。上の情報は一例で、エラーが起きる場所は毎回やるたびに変わっています。  
spec_helper.rbの内容は以下です。

```

    # This file is copied to spec/ when you run 'rails generate rspec:install'
    ENV["RAILS_ENV"] ||= 'test'
    require File.expand_path("../../config/environment", __FILE__)
    require 'rspec/rails'
    require 'rspec/autorun'
    require 'simplecov'
    require 'simplecov-rcov'
    SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
    SimpleCov.start do
      add_filter '/vendor/'
    end
    
    # Requires supporting ruby files with custom matchers and macros, etc,
    # in spec/support/ and its subdirectories.
    Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
    
    # Checks for pending migrations before tests are run.
    # If you are not using ActiveRecord, you can remove this line.
    ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
    
    include Warden::Test::Helpers
    Warden.test_mode!
    
    RSpec.configure do |config|
      # ## Mock Framework
      #
      # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
      #
      # config.mock_with :mocha
      # config.mock_with :flexmock
      # config.mock_with :rr
    
      # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
      config.fixture_path = "#{::Rails.root}/spec/fixtures"
    
      # If you're not using ActiveRecord, or you'd prefer not to run each of your
      # examples within a transaction, remove the following line or assign false
      # instead of true.
      config.use_transactional_fixtures = !true
    
      # 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
    
      # Run specs in random order to surface order dependencies. If you find an
      # order dependency and want to debug it, you can fix the order by providing
      # the seed, which is printed after each run.
      #     --seed 1234
      config.order = "random"
    
      config.include FactoryGirl::Syntax::Methods
    
      config.before(:suite) do
        DatabaseCleaner.strategy = :truncation
        DatabaseCleaner.clean_with :truncation
      end
    
      config.before :each do
        DatabaseCleaner.start
      end
    
      config.after :each do
        DatabaseCleaner.clean
      end
    
      config.before :all do
        FactoryGirl.reload
      end
    
      config.include Devise::TestHelpers, type: :controller
      config.include Capybara::DSL
      config.include Login
      config.include NewWindow
    
      config.before do
        if page.driver.try :headers
          page.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' }
        end
      end
    
      config.after do |example|
        example = example.example
        if example.metadata[:type] == :feature and Capybara.javascript_driver == :poltergeist and example.exception.present?
          page.save_screenshot "spec/screenshot/#{example.full_description}.png"
        end
      end
    end
    
    Capybara.default_wait_time = ENV['WAIT_TIME'] || 3
    
    require 'capybara/rspec'
    require 'capybara/rails'
    # require 'selenium-webdriver'
    require 'capybara/poltergeist'
    Phantomjs.path
    
    # Capybara.javascript_driver = :selenium_chrome
    # Capybara.javascript_driver = :selenium_firefox
    Capybara.javascript_driver = :poltergeist
    
    Capybara.register_driver :poltergeist do |app|
      driver = Capybara::Poltergeist::Driver.new(app, js_errors: false, timeout: 10000, phantomjs: Phantomjs.path, phantomjs_options: ['--ignore-ssl-errors=yes','--ssl-protocol=any'])
      driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' }
      driver
    end
    
    Capybara.register_driver :selenium_chrome do |app|
      Capybara::Selenium::Driver.new(app, :browser => :chrome)
    end
    
    Capybara.register_driver :selenium_firefox do |app|
      profile = Selenium::WebDriver::Firefox::Profile.new
      ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
      profile["general.useragent.override"] = ua
      opts = { browser: :firefox, profile: profile }
      Capybara::Selenium::Driver.new(app, opts)
    end
    
```

以上のように、js_errors: falseをつけているにもかかわらず、javascriptのエラーが起こったらテストが落ちます。  
javascriptのエラーを治そうにも、tagmanager由来のエラーもあるようで、難しいかなと思いっています。  
ローカル環境で同一のテストを回したところjavascriptのエラーでテストが落ちなかったので、jenkins環境によるのかなと思っています。  
どうすればjavascriptのエラーを無視して、rspecのテストを落ちないようにできるでしょうか。

環境:  
CentOS release 6.4 (Final)  
ruby 2.1.5p273  
Rails 4.0.5  
Jenkins ver. 1.552  
phantomjs 1.9.2  
poltergeist (1.6.0)
CC BY-SA 3.0
2015-12-04T02:39:43.917
19618
2015-12-04T23:57:30.343
13515
question
ruby-on-rails ruby rspec jenkins poltergeist
jenkinsでrspecのテストを回した時に、js_errors: falseを指定しているにもかかわらず、jsのエラーが出る
根本的な対策にはなりませんが、テスト実行中のみ読み込まない、というアプローチがあります。

```

    <%= javascript_include_tag('//example.com/foo_bar.js') unless Rails.env.test? %>
    
```

自分が書いたJSのコードなら警告やエラーを解消すべきですが、GoogleやFacebook等、外部のスクリプトは自分では制御しがたいものが多いので、何度か試行錯誤してムリだったら諦める、というのも一つの手です。（実際過去にそうしたプロジェクトもあります）

ただし、当然ですがエラーの原因となっているJSがテストの実行に必要不可欠なものであれば、このアプローチは使えません。
CC BY-SA 3.0
2015-12-04T23:57:30.343
19644
2015-12-04T23:57:30.343
85
19618
answer