Testing
Install
Section titled “Install”pnpm add -D @dudousxd/nestjs-inertia-testingE2E with supertest
Section titled “E2E with supertest”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);});Unit tests with InertiaTestingModule
Section titled “Unit tests with InertiaTestingModule”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);assertInertia (plain payload)
Section titled “assertInertia (plain payload)”For testing codegen output or JSON fixtures directly:
import { assertInertia } from '@dudousxd/nestjs-inertia-testing';
assertInertia(jsonPayload).toRenderComponent('Dashboard');Full assertion API
Section titled “Full assertion API”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 assertionPartial reload testing
Section titled “Partial reload testing”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');Framework support
Section titled “Framework support”Import the matcher extension for your test framework:
// Vitestimport '@dudousxd/nestjs-inertia-testing/vitest';
// Jestimport '@dudousxd/nestjs-inertia-testing/jest';