mardi 2 juin 2015

Evaluating/validating all app JavaScript in a minitest unit test

Is this a crazy thing to attempt? :)

sprockets.context_class.class_eval do
  def image_path(dummy); end
  def asset_path(dummy); end
end

sprockets.append_path(Rails.root.join('app/assets/javascripts').to_s)
asset = sprockets['application.js']
jquery = File.read(File.join(Rails.root, "app/assets/javascripts/vendor/jquery.js"))
js = ExecJS.exec jquery + asset.to_s

My premise is quite simple. I want a test that loads and validates all JavaScript assets in my app. Many are js.erb so I need to push them through the asset pipeline first (or otherwise evaluate them).

My thinking is that just like when I load a page in Chrome it validates all JS for my app (because it's all crammed into application.js), I should be able to do this with a unit test. Only this absolutely doesn't work -- I keep gettiing the most random errors, like:

ERROR (0:00:01.874) JS files should be using correct escaping
      TypeError: Cannot read property 'defaultView' of undefined

But what I'm trying to do really isn't all that special - I'm sure other apps also validate all JS as part of their tests... right? What am I missing here?

To put this into a clearer question: How can I evaluate all of my JS assets in a Rails unit test?

Thank you! :)

Aucun commentaire:

Enregistrer un commentaire