Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can write tests just like you would test a JSON API. But instead of testing the JSON structure, you can test the HTML structure. Depending on how deep you want to go, you can parse the HTML response and check for the components, but sometimes just checking for bare strings is enough. For example, taking the FastAPI testing docs[0], instead of:

  assert response.json() == {"detail": "Item already exists"}
You can write:

  assert f"<b>Item already exists</b>" in response.render().decode()

One thing I found using HTMX (or just HTML) is that the apps are less fragile. I know that if I return a chunk of HTML, the browser will render it the same way every time. With a JSON-based SPA, maybe that chunk of JSON ends up triggering multiple side effects, re-renders, etc. which can behave differently depending on the current application state. This all affects the amount of testing that I feel comfortable with.

[0]: https://fastapi.tiangolo.com/tutorial/testing/?h=testing#ext...



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: