Skip to content

Testing

@stratal/inertia/testing augments the TestResponse object with assertion methods for verifying Inertia pages, props, flash data, and precognition responses.

Import the testing side-effect in your test files or global setup:

import '@stratal/inertia/testing'

Once the testing module is imported, the following methods are available on every TestResponse:

MethodDescription
assertInertia(callback?)Assert the response is an Inertia page. Optionally pass a callback to inspect the page object.
assertInertiaComponent(name)Assert the rendered component matches the given name.
assertInertiaProp(path, expected)Assert a prop value at the given path. Supports dot notation.
assertInertiaPropExists(path)Assert a prop exists at the given path.
assertInertiaPropMissing(path)Assert a prop does not exist at the given path.
assertInertiaUrl(url)Assert the page URL matches the given string.
assertInertiaVersion(version)Assert the asset version matches the given string.
assertInertiaFlash(key, value)Assert flash data contains the given key and value.
assertInertiaDeferredProp(prop, group)Assert a deferred prop exists and belongs to the given group.
assertInertiaMergeProp(prop)Assert a prop is marked as mergeable.
assertInertiaSharedProp(prop)Assert a shared prop is present on the page.
assertSuccessfulPrecognition()Assert the response is a successful precognition response (204 status).
assertPrecognitionValidationErrors(errors?)Assert the response contains precognition validation errors (422 status). Optionally verify specific error fields.
import { Test } from '@stratal/testing'
import '@stratal/inertia/testing'
import { PostsModule } from '../src/posts.module'
describe('PostsController', () => {
it('renders the index page', async () => {
const module = await Test.createTestingModule({
imports: [PostsModule],
}).compile()
const client = module.createHttpClient()
const response = await client.get('/posts')
await response
.assertInertia()
.assertInertiaComponent('posts/Index')
.assertInertiaPropExists('posts')
})
})

Example: testing flash data and validation

Section titled “Example: testing flash data and validation”
import { Test } from '@stratal/testing'
import '@stratal/inertia/testing'
import { PostsModule } from '../src/posts.module'
describe('PostsController', () => {
it('validates post creation', async () => {
const module = await Test.createTestingModule({
imports: [PostsModule],
}).compile()
const client = module.createHttpClient()
const response = await client.post('/posts', { body: {} })
await response
.assertInertiaFlash('errors', {
title: expect.any(String),
})
})
})

Precognition lets you validate form data before submission. Use the precognition assertions to verify that your validation rules work correctly:

it('validates with precognition', async () => {
const module = await Test.createTestingModule({
imports: [PostsModule],
}).compile()
const client = module.createHttpClient()
const valid = await client.post('/posts', {
headers: { 'Precognition': 'true' },
body: { title: 'My Post', content: 'Hello world' },
})
await valid.assertSuccessfulPrecognition()
const invalid = await client.post('/posts', {
headers: { 'Precognition': 'true' },
body: {},
})
await invalid.assertPrecognitionValidationErrors({
title: expect.any(String),
})
})