Skip to content

Ignoring whitespace differences assert_dom_equal? #62

Closed
@chancancode

Description

@chancancode

I think in practice a lot of people who tries to use assert_dom_equal will run into whitespace issues.

For example, if you have this template:

<div class="header">
  <h1><%= @post.title.titlecase %></h1>
</div>

...and in your test:

assert_dom_equal rendered.at('.header').inner_html, "<h1>Rails Is Omakase</h1>"

The test will fail due to whitespace differences.

In this simple case you can fix it by doing inner_html.strip, but consider this template:

          <div class="header">
            <h1><%= @post.title.titlecase %></h1>
            <h2>Published on <%= format_date @post.published_at %></h2>
          </div>

In this case you will have to do something more involved, perhaps using strip_heredoc, etc.

While they do produce different DOM structures, because they browser doesn't actually render these differences, and that the spirit of the helper is to test "equivalency" (e.g. it ignore attributes ordering), I highly doubt that the current failure is useful (maybe we can give you a strict mode, for edge cases like <pre> content etc).

Unfortunately, it is not easy to come up with a simply fix for this. Ideally, we would "just do what the browser does", but that is actually not so simple – for one it depends on inputs that we don't have, e.g. the language of the text and things like CSS rules.

That said, I think it's still worthwhile/possible to come up with a Good Enough™ set of heuristics that work for the 99% cases (and if it doesn't work, you can use the strict mode).

Does that sound reasonable? Is this a behavior that we can just change, or would it require an opt-in, and/or going through a deprecation cycle, etc?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions