JohnnyCurran

JohnnyCurran

Hello,

The problem: HTML special characters can cause =~ assertions to fail in LiveViewTest - Especially if used in conjunction with libraries that generate random data AKA Faker

Under the hood, all render_* functions in Phoenix.LiveViewTest eventually find their way back to the function Phoenix.LiveViewTest.render/1 which is essentially a wrapper around Floki.raw_html/2

raw_html/2 accepts a list of options (currently only 1 option): a boolean named :encode. If :encode is true (which it is by default), then things such as O'Connell become encoded and rendered as O'Connell in the test. This causes test assertions to fail intermittently.

What’s the current solution?

You can, in config/test.exs, pass config :floki, :encode_raw_html, false which will turn this off for all tests.

In the event we want to enable / disable this behavior for specific assertion(s), we are unable.

I would like to see the ability to do:

assert render(view, encode: false) =~ "O'Doyle"
assert render(view, encode: true) =~ "O&#39Doyle"

Currently, the only way I can think to do this is expand each of the render_* functions with an optional args \\ [] argument but that seems cumbersome - and there are lots of render functions already.

I welcome all thoughts for or against the idea as well as ideas on how this might be better implemented

Thank you

You can read more about the problem described at Fix Your Flaky Faker Tests - John Elm Labs

Showing Posts 1 to 1

sodapopcan

sodapopcan

I basically just stopped using test data with special chars since I found it too frustrating. A little tangentially I do find it a bit of a footgun since I generally think of LiveView tests of asserting what the user sees, but of course they live in this strange realm between e2es and integration tests.

The simplest option I can think of now that you’ve made me think of it again would be:

assert render(view) =~ Floki.raw_html("O'Doyle")

…which could be wrapped up in an encode or raw helper or something. It might be more flexible if it were:

def encode(string) do
  string
  |> Floki.parse_fragment!()
  |> Floki.raw_html()
end

though I haven’t thought through how that might mess with regexes :thinking:

Happy you brought this up!

— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews