LaravelPackages.net
Acme Inc.
Toggle sidebar
stevebauman/laravel-husk

A Laravel Dusk environment for your Javascript only applications.

64
88
v1.1.0
About stevebauman/laravel-husk

stevebauman/laravel-husk is a Laravel package for a laravel dusk environment for your javascript only applications.. It currently has 88 GitHub stars and 64 downloads on Packagist (latest version v1.1.0). Install it with composer require stevebauman/laravel-husk. Discover more Laravel packages by stevebauman or browse all Laravel packages to compare alternatives.

Last updated

Laravel Husk

Laravel Husk is a thin and light scaffolded Laravel Dusk environment.

It allows you to test your JavaScript applications with PHP using Pest, without having to scaffold an entire Laravel application.

Examples

| JS Framework | Tests | | ---------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- | | NuxtJS | Nuxt Tests | | NextJS | Next Tests | | Svelte | Svelte Tests | | Gatsby | Gatsby Tests | | Gridsome | Gridsome Tests | | Showcode (NuxtJS) | Gridsome Tests |

Installation

Inside of your JavaScript application folder, run the below command to scaffold the Laravel Husk environment:

Note: This will create the folder named browser which will contain your Laravel Husk test environment.

composer create-project stevebauman/laravel-husk browser

After scaffolding the test environment, you should have the below folder structure;

javascript-app/
├── ...
└── browser/
    ├── bootstrap/
    ├── config/
    ├── storage/
       ├── log/
       ├── screenshots/
       └── source/
    └── tests/
        ├── ...
        ├── ExampleTest.php
        ├── DuskTestCase.php
        └── Pages/
            └── ExamplePage.php

Then, navigate into the browser directory and install the Chrome driver by running the below command:

php application dusk:chrome-driver --detect

Usage

Before running your dusk tests, be sure to set the proper base URL to where your JavaScript application will be served from:

// tests/DuskTestCase.php

protected function setUp(): void
{
    parent::setUp();

    $this->setupBrowser('http://localhost:3000');
}

After setting the base URL, serve your JavaScript application:

npm run dev

Then, inside of another terminal, navigate into the browser directory:

cd browser

And run the below command:

Important: Make sure you've installed the Chrome driver first, via php application dusk:chrome-driver --detect

php application pest:dusk

With arguments:

php application pest:dusk --order-by=random --filter="it can load"

Note: You may also insert the below JSON into the scripts section of your package.json file to run your browser tests from your root project directory:

"scripts": {
    "test": "cd browser && php application pest:dusk"
}
npm run test

GitHub Actions

You may use the below GitHub action as a template to run your Laravel Dusk tests:

name: run-tests

on:
    push:
    pull_request:
    schedule:
        - cron: "0 0 * * *"

jobs:
    run-tests:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - uses: actions/setup-node@v2
              with:
                  cache: "npm"

            - name: Install Javascript Dependencies
              run: npm install

            - name: Start JavaScript Application
              run: npm run dev &

            - name: Install Composer Dependencies
              working-directory: ./browser
              run: composer install --no-progress --prefer-dist --optimize-autoloader

            - name: Upgrade Chrome Driver
              working-directory: ./browser
              run: php application dusk:chrome-driver `/opt/google/chrome/chrome --version | cut -d " " -f3 | cut -d "." -f1`

            - name: Run Dusk Tests
              working-directory: ./browser
              run: php application pest:dusk

            - name: Upload Screenshots
              if: failure()
              uses: actions/upload-artifact@v2
              with:
                  name: screenshots
                  path: browser/storage/screenshots

            - name: Upload Console Logs
              if: failure()
              uses: actions/upload-artifact@v2
              with:
                  name: console
                  path: browser/storage/console

Star History Chart