Skip to content

Testing

Terminal window
pnpm add -D @dudousxd/nestjs-inertia-testing

The most complete approach — spins up the real HTTP stack:

import * as request from 'supertest';
import { expectInertia } from '@dudousxd/nestjs-inertia-testing';
it('renders Dashboard with user prop', async () => {
const res = await request(app.getHttpServer())
.get('/dashboard')
.set('X-Inertia', 'true');
expectInertia(res)
.toRenderComponent('Dashboard')
.toHaveProp('user.id', 42);
});

For fast unit tests that don’t spin up HTTP:

import { Test } from '@nestjs/testing';
import {
InertiaTestingModule,
createFakeInertiaRequest,
expectInertiaRequest,
} from '@dudousxd/nestjs-inertia-testing';
const moduleRef = await Test.createTestingModule({
imports: [InertiaTestingModule.forTest({})],
controllers: [DashboardController],
providers: [{ provide: UserService, useValue: mockUsers }],
}).compile();
const controller = moduleRef.get(DashboardController);
const fakeReq = createFakeInertiaRequest({ method: 'GET', url: '/dashboard' });
await controller.show(fakeReq);
expectInertiaRequest(fakeReq).toRenderComponent('Dashboard').toHaveProp('user.id', 42);

For testing codegen output or JSON fixtures directly:

import { assertInertia } from '@dudousxd/nestjs-inertia-testing';
assertInertia(jsonPayload).toRenderComponent('Dashboard');
expectInertia(res)
.toRenderComponent('Dashboard') // component name
.toHaveUrl('/dashboard') // page URL
.toHaveProp('user.name', 'Alice') // deep prop with value
.toHaveProp('count') // prop exists (any value)
.toMissProp('hidden') // prop absent
.toHaveExactProps({ user, count }) // full props equality
.toShareProp('auth.id', 1) // shared prop
.toHaveDeferredProp('activity') // prop marked defer()
.toHaveMergeProp('transactions') // prop marked merge()
.toHaveAlwaysProp('user') // prop marked always()
.toHaveErrors({ email: 'required' }) // validation errors
.toRedirectTo('/login', 302); // redirect assertion
const res = await request(app.getHttpServer())
.get('/dashboard')
.set('X-Inertia', 'true')
.set('X-Inertia-Partial-Component', 'Dashboard')
.set('X-Inertia-Partial-Data', 'stats');
expectInertia(res)
.toHaveProp('stats')
.toMissProp('activity');

Import the matcher extension for your test framework:

// Vitest
import '@dudousxd/nestjs-inertia-testing/vitest';
// Jest
import '@dudousxd/nestjs-inertia-testing/jest';