Remove Laravel Mix during tests

 Reading time ~3 minutes

Heads up: this article is over a year old. Some information might be out of date, as I don't always update older articles.

Introduction

Laravel Mix is a handy Webpack wrapper for frontend asset building pipelines. It definitely helps any Laravel developer which wants to avoid the pain of Webpack setup and configuration.

For example, the following 2 lines:

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css');

will apply

  • module bundling, minification, and concatenation using the app.js JavaScript file as entry point, writing the output in public/js
  • sass compilation of the app.scss file into public/css

Then in your Blade templates you can use the global mix helper to get the URL of the versioned asset from the mix-manifest.json Manifest file.

The Manifest file

The Manifest file is a JSON file that is created by Laravel Mix to map the URL of your assets to their most recent versions. As you probably already know, browsers are able to cache assets files so they are loaded from the local disk instead of the server, speeding up the rendering of the page. However when you want to force browsers to reload your files you will have to provide them with a unique token, with a timestamp or with a different filename.

Laravel Mix can handle this process for you using the version method. The generated Manifest will then look like the following

{
    "/js/app.js": "/js/app.3fbeeaed8340a72d2a4e.js",
    "/css/app.css": "/css/app.56df966576b7a6d2bb71.css"
}

but in your Blade templates you don’t have to change mix('/js/app.js') because Mix handles the conversion for you.

During development however there’s no need to version assets, because you have full control of your browser, therefore the Manifest will simply map each asset to itself

{
    "/js/app.js": "/js/app.js",
    "/css/app.css": "/css/app.css"
}

But what will happen if we try visit a page that includes an asset with the mix helper, but the Manifest file doesn’t exist? The answer is simple: it will inevitabely fail with this error The Mix manifest does not exist., remembering you that you have to compile assets before visiting the page.

Testing

However, when testing your application with an automatic test suite, maybe you don’t really want to register a failure simply because you forgot to compile assets.

<?php

$response = $this->get('/');

$response->assertStatus(200);

Here, for example, I simply want to check that the home page can be visisted successfully. I’m not checking that CSS rules are applied or if my fancy React app can load.

Luckily since Laravel 5.8 the mix helper is binded to the IOC container, which means that its implementation (Illuminate\Foundation\Mix) can be swapped during tests.

<?php

$this->app->swap(Mix::class, function () { });

$response = $this->get('/');

$response->assertStatus(200);

Even better, Laravel 6.0 adds a nice and handy helper method that allows us to refactor the previous code:

<?php

$this->withoutMix();

$response = $this->get('/');

$response->assertStatus(200);
comments powered by Disqus

Parse log files with AWK

Introduction

Not everyone can afford (or have the need of) an ELK stack for analyzing logs. Sometimes reading files from the …