A lightweight package for testing Laravel views.

v0.2.6 2020-11-02 09:29 UTC

This package is auto-updated.

Last update: 2020-11-02 09:30:16 UTC


Mojito example

Build Status Total Downloads Latest Version License

About Mojito

Mojito was created by, and is maintained by Nuno Maduro, and is a lightweight package for testing Laravel views in isolation.

Installation & Usage

Requires PHP 7.2.5+

Require Mojito using Composer:

composer require nunomaduro/laravel-mojito --dev

How to use:

class WelcomeTest extends TestCase
    // First, add the `InteractsWithViews` trait to your test case class.
    use InteractsWithViews; 

    public function testDisplaysLaravel()
        // Then, get started with Mojito using the `assertView` method.

Optionally, you can also perform view testing from your HTTP Tests:

class WelcomeTest extends TestCase
    public function testDisplaysLaravel()
        $response = $this->get('/');




Asserts that the view contains the given text.

$this->assertView('button')->contains('Click me');
$this->assertView('button', ['submitText' => 'Cancel'])->contains('Cancel');



Filters the view and returns only the first element matching the selector.

$this->assertView('welcome')->first('.links a')->contains('Docs');


Asserts that the view has the given selector.




Asserts that the view root element has the given attribute value.

$this->assertView('button')->hasAttribute('attribute', 'value');
$this->assertView('button')->hasAttribute('data-attribute', 'value');

$this->assertView('welcome')->hasAttribute('lang', 'en');


Asserts that the view has an element with the given class.


$this->assertView('welcome')->in('.content')->at('div > p', 0)->hasClass('title');


Asserts that the view has an element with the given link.


$this->assertView('welcome')->in('.links')->at('a', 6)->hasLink('');


Filters the view and returns only the elements matching the selector.

$this->assertView('welcome')->in('.links a')->contains('Laracast');


Filters the view and returns only the last element matching the selector.

$this->assertView('welcome')->last('.links a')->contains('GitHub');


Fell free to add your own macros to the ViewAssertion::class.

use NunoMaduro\LaravelMojito\ViewAssertion;

// Within a service provider:
ViewAssertion::macro('hasCharset', function (string $charset) {
    return $this->in('head')->first('meta')->hasAttribute('charset', $charset);

// In your tests:


Thank you for considering to contribute to Mojito. All the contribution guidelines are mentioned here.

You can have a look at the CHANGELOG for constant updates & detailed information about the changes. You can also follow the twitter account for latest announcements or just come say hi!: @enunomaduro

Support the development

Do you like this project? Support it by donating


Mojito is an open-sourced software licensed under the MIT license.