Reliable order of `:global` attributes in OTP26?

In our project it’s quite common that we’re doing assertions on certain attributes while testing components:

html = render_component(&MyComponent.render/1, %{})

assert html =~ ~s(<form phx-change="change" phx-submit="submit" phx-target="1">)

Since upgrading to OTP26 where map keys for smaller maps are no longer ordered the same way they were before we’ve started seeing very flaky tests. It’s quite often a test fails because the order of the custom attributes falling under changes order between runs

attr :rest, :global

I understand that it’s somewhat expected behaviour since the change but what would be the best way to remedy the the situation (we’ve ruled out using floki and removing the assertions at the moment)?

Using folki or similar to parse the attributes and assert is your best bet. You can parse the attributes into a map and match or build your own assertion helper

2 Likes

And xpath query selector may be enough to test what you need as well, but you’ll need an html parser like floki either way

1 Like

You’re right, it’s not a big deal after all, a small has_selector? helper took me all the way and it will also allow being more selective in what attributes to compare against (so changed classes wont cause failing tests unless explicitly wanted).

Sometimes it’s hard to see the forest because the trees are in the way :smiley:

3 Likes