I’ve stopped using assertSee in my Laravel tests because it’s a trap (unrelated: I once owned this domain). Instead, I reach for assertSeeText and assertSeeHtml.
Using the appropriate Laravel assertions to test HTML
Stack for this post:
- Laravel 13.38.0
- PHP 8.5.12
Laravel assertSeeText contains a trap: sometimes the content you expect to find in the tested HTML is there, but the test still fails. Without understanding why, you fall back on assertSee and your test passes again:
// tests/Feature/Http/Controllers/WhateverControllerTest.php
it('visits whatever page and finds whatever text in it', function () {
$this
->get('/whatever-page')
->assertSeeText('whatever text');
->assertSee('whatever text');
});Why assertSee works where assertSeeText doesn’t
The reason the test failed is that assertSeeText first tries to turn the HTML into a plain text string. To do this, strip_tags is called and may trash invalid DOM parts, so if your content is inside a removed section, it can no longer be found and the test fails:
<<article>
<h1>I contain whatever</h1>
<p>
The <code>article</code> tag is invalid
and <em>whatever text</em> in it will be trashed
during the test.
</p>
</article>When falling back on assertSee, it leaves the HTML untouched and the test passes again, but you are now missing an opportunity to discover and fix a potential markup issue. On the long run, you may end up wondering why some tests uses assertSee and others don’t.
Avoid assertSee
Validate your HTML. Use assertSeeText to find content, and assertHtml to find HTML tags and attributes, this way intent is clear:
// tests/Feature/View/Components/BannerTest.php
describe('<x-banner>', function () {
test('default (no params)', fn () => $this
->blade('<x-banner>Banner content</x-banner>')
->assertSeeHtml('<div class="banner')
->assertSeeText('Banner content')
);
});In fact, assertSeeHtml is a currently undocumented but convenient alias for assertSee('<div class="banner', false): passing false as second parameter disables character encoding, preventing <div class="banner in the tested HTML to become <div class="banner, which would cause the test to fail.
Related assertions: assertDontSee, assertDontSeeHtml, assertDontSeeText, assertSeeHtmlInOrder, assertSeeInOrder, assertSeeTextInOrder.