Software Development

Setting up Jest with Laravel 8.x, Jetstream, and Inertia

I’ve been really starting to place emphasis on testing in my projects lately. This led me to try out Jest with a project where I use Laravel 8.x, Laravel Jetstream, and Inertia. This stack is super powerful and malleable and fits our use case perfectly.

However, being new to Jest and JS testing in general, I was a bit confused about how to go about setting up the dependencies that Inertia injects such as $page, $inertia, etc. I added a ticket on Inertia’s GitHub, but realized what I was really asking for was less about Inertia and more about Jest and my lack of knowledge in that tool.

So I spent some time playing around with it, reading documentation, and finally arrived at this solution.

import Vue from 'vue'
import { shallowMount } from '@vue/test-utils'
import { InertiaApp } from '@inertiajs/inertia-vue'
import { InertiaForm } from 'laravel-jetstream'
import Create from './../../Pages/Space/Create.vue'


test('should mount without crashing', () => { 
    const wrapper = shallowMount(Create)

I’ll point out a couple of things. First, I have to import Vue because I’m using Vue.use statements. Second, I’m importing InertiaApp and InertiaForm from their respective modules as this gives me access to the Inertia variables I need. Lastly, I’m using shallowMount because I didn’t want to inject the layout dependencies I’m using nor mock them.

There you have it. Doing this allowed my component test to pass and got me started with testing between Laravel, Jetstream, Inertia and Jest.

By Matt Pritchett

Matt is a Christian, a husband, a father to four, and a software engineer at Saturday Drive, the makers of products like Ninja Forms, Caldera Forms, and SendWP. He also helps clients solve complex problems with code, consulting, and more. He occasionally blogs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.