Replies: 5 comments 2 replies
-
|
That is framework dependent, for Django/Python, I use from django_htmx.middleware import HtmxDetails
def htmx_request(user=None,
get_url=None,
post_url=None,
post_data={},
trigger=None,
**kwargs):
headers = dict(HTTP_HX_Request="true", HTTP_HX_Trigger_Name=trigger)
if get_url is not None:
request = RequestFactory().get(
get_url, {x: y for x, y in kwargs.items() if y is not None}, **headers)
if post_url is not None:
kwargs.update(headers)
request = RequestFactory().post(post_url, data=post_data, **kwargs)
request.htmx = HtmxDetails(request)
request.user = user if user else AnonymousUser()
return request |
Beta Was this translation helpful? Give feedback.
-
|
Every professional framework has at least one chapter in his docs about how to write tests. I think there are three ways:
This is just a gentle reminder that a chapter in the docs would be useful. |
Beta Was this translation helpful? Give feedback.
-
|
Seconded. I just came here to ask this. I had been thinking of using Selenium/Playwright/Puppeteer but had a discussion with someone recently where they had a bad experience with e2e due to fragile tests and just pushed all testing to the unit test level. This vibes with my intuition. My backend is in Go so it's likely possible to do something similar to what @BoPeng does in Python but how are people testing their HTMX applications in general? |
Beta Was this translation helpful? Give feedback.
-
|
I personally use Playwright that was already mentioned above, but there's indeed no "offical" way to test htmx-based apps for now. Btw see #2360 for a related discussion on race-conditions and how to make reliable htmx-based tests. If someone feels like it, PRs improving documentation are always welcome! |
Beta Was this translation helpful? Give feedback.
-
|
I end up with a strategy which is… a little bit weird, but which works. But there is one important pre-condition: The template engine syntax you’re using should have an homologue in the JS world. On this specific project, which is in Kotlin / Spring Boot, we are using Pebble. It’s a "jinja-like" SSR template, like the ones of Django (Python) and twig (in PHP / Symfony). Happens there is a similar one for JS, which is called nunjucks The trick is simply to have nunjucks render the pebble (or jinja, or twig...) templates in a JSDOM environnement, and let the JS run naturally, and run assertion like you would do with any other frontend. That’s enough for tools like Alpine For htmx - which fires AJAX requests, asking the server for html fragments to update the current DOM - you need an additionnal trick: MSW With it, you can intercept those request, and render the appropriate partial templates, with nunjucks |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
There is a question on Stackoverflow about how to test htmx based applications.
I think there should be a small section about this in the docs.
Beta Was this translation helpful? Give feedback.
All reactions