Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vasyanya's avatar

Laravel mix and phpUnit

Is there any way to run phpunit tests with Laravel Mix? Something like

gulp tdd

in Elixir

0 likes
11 replies
webtechnick's avatar

This is a feature I used with gulp, and would like to continue to use with the new laravel mix via webpack.

JeffreyWay's avatar

No. It doesn't make much sense for a tool like Webpack.

Vasyanya's avatar

may be it does not. but we want it :) it's nice to run one command and get all watchers and compilers in one go.

working solution... (a little modified from here: https://github.com/webpack/webpack/issues/3798 )

npm install cross-env

package.json

....
 "scripts": {
    ...
    "tdd": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=testing node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },

webpack.mix.js

const { mix } = require('laravel-mix');
const exec = require('child_process').exec;

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix
    // .js('resources/assets/js/app.js', 'public/js')
    .less('resources/assets/less/app.less', 'public/css');



mix.webpackConfig({
    plugins: [
        new mix.plugins.BrowserSyncPlugin(Object.assign({
            host: 'localhost',
            port: 3000,
            proxy: 'ik.dev',
            files: [{
                match: [
                    'app/**/*.php',
                    'routes/**/*.php',
                    // '_legacy/**/*.php',
                    'resources/views/**/*.php',
                    'public/mix-manifest.json',
                    'public/css/**/*.css',
                    'public/js/**/*.js'
                ],
                fn: function(event, file) {
                    const browserSync = require("browser-sync").get('bs-webpack-plugin');
                    if (event === 'change') {
                        browserSync.reload();
                        if (process.env.NODE_ENV === 'testing') {
                            exec('php vendor/bin/phpunit', (error, stdout, stderr) => {
                                // if (error) { // for debugging run issues
                                //     console.error(`exec error: ${error}`);
                                //     // return;
                                // }
                                console.log(`${stdout}`);
                                console.log(`${stderr}`);
                            })
                        }
                    }
                }
            }]
        }, {name: "bs-webpack-plugin"}))
    ]
});
LasseHaslev's avatar

I just created an fast and simple "watch and execute" package in node.

https://github.com/LasseHaslev/executor

I am using it to execute phpunit in my laravel projects, but as you see in the documentation, the package is not limited to that.

Usage

Install in your project:

npm install @lassehaslev/executor --save-dev

package.json:

{
  "scripts": {
    "tdd": "executor './vendor/bin/phpunit' --watch='**/*.php' --ignore='node_modules/' --ignore='vendor/'"
  },
}

Usage:

npm run tdd
8 likes
J5Dev's avatar

Wow, really nice package @LasseHaslev Short & sweet, exactly how it should be!

Thank you :)

cfort's avatar

@LasseHaslev Thanks for putting that together. Is there any way to get the --color option to work, or maybe even call terminal-notifier? I'd love to get some visual feedback so it's easier to parse the output.

sam.martins's avatar

Thanks @LasseHaslev

@cfort you can set this to color work in your terminal:

package.json:

{
  "scripts": {
   "tdd": "executor './vendor/bin/phpunit --color=always' --watch='**/*.php' --ignore='node_modules/' --ignore='vendor/'"
  },
}
LasseHaslev's avatar

Hi @J5Dev, @cfort, @sam.martins, @djekl and sorry for delayed answer.

I am glad you liked the package :)

@djekl I don't think this package should be handling terminal-notifier cause of this package main purpose is not to work as a tdd-only, but to watch for any file changes and execute any command the user want.

Personally i achieved the same result as you asking for on OSX through iTerm2 triggers: Preferences > Profiles > Advanced > Triggers

One regular expression for OK (.+)$ with a action of Run Command... and a parameter of osascript -e 'display notification "Test successful!" with title "Green!"' and the same approach for ERROR and FAILURES!.

1 like
lara5153's avatar

Hi, on Windows (Cmder>bash) use command below:

executor .\\vendor\\bin\\phpunit --watch=**/*.php --ignore=node_modules/ --ignore=vendor/

executor .\\vendor\\bin\\phpunit '--filter TestSomethingTest' '--color=always' --watch=**/*.php --ignore=node_modules/ --ignore=vendor/
1 like

Please or to participate in this conversation.