PhoenixTest foot guns

Writing LiveView feature tests is an occasionally frustrating experience, not helped by the inconsistent, opaque, and generally horrible API of the official Phoenix.LiveViewTest module.

Thankfully, German Velasco, a man who knows a thing or two about testing LiveView, is here to save us all with his splendid PhoenixTest library. Using it has turned testing LiveViews from a miserable chore into a genuine pleasure.

That said, it’s still doing something which looks an awful lot like a browser integration test, but is really communicating directly with the LiveView process; no browser required. This can lead to some unexpected behaviour1.

This is a note-to-self about the pitfalls I have encountered, and their workarounds.

  • The phx-change event only fires when the value of a field changes if the event is attached to the form
    As far as I can tell, the only workaround is to accept this limitation and attach the event to the form.

    Unfortunately, that leads us to the next item in the list.

  • The phx-change event does not include the _target attribute
    This appears to have been (perhaps partially) fixed, but the changes have not yet been released.

    For the time being, you have to forgo any dreams of pattern matching on the _target.

Footnotes

  1. A euphemism for violent swearing.

Sign up for my newsletter

A monthly round-up of blog posts, projects, and internet oddments.