Ignore whitespace differences in `assert_dom_equal`

I like writing test for helpers that output html, but the fact that assert_dom_equal requires the whitespace to be the same always surprises me the first time I add a test to a new app. There are a few open issues and patches related to this but none seem to be getting reviewed or merged:

https://github.com/rails/rails-dom-testing/issues/62 https://github.com/rails/rails-dom-testing/pull/71 https://github.com/rails/rails-dom-testing/pull/66

I’ve put the following patch into my test_helper.rb for the last few projects and it works well enough, but it would be nice to not copy and paste this into each app.

class ActionView::TestCase
  private
  # Used by assert_dom_select
  # Patch strips newlines and space around them so assertion is less brittle
  def fragment(text)
    Nokogiri::HTML::DocumentFragment.parse(text.gsub(/[\s]*\n[\s]*/, ''))
  end
end

I’ve written a similar helper myself a lot of times.

How would you feel about upstreaming this work?

Sure! I’ll put up a PR later today and link it here.

1 Like

Submitted a PR with the changes I patch in as well as an opt out option.

https://github.com/rails/rails-dom-testing/pull/83

Let me know if you need anything else to get it merged!

1 Like