Skip to content

Testing ​

What this page covers

Every choice of --Persistence, --Auth, --IncludeTests and --Aspire: a label says which options a part is for. The copy in your app's docs/ folder has only the parts for yours. ApiHost stands for your app's name.

SuiteWhereRun
With the tests API integration teststests/ApiHost.Testsdotnet test (Docker must be running)
Client unit testssrc/Client/src/**/*.test.tsxnpm test in src/Client (npm run test:watch while working)
With the tests End-to-end teststests/e2enpm install once, then npm test in tests/e2e, with the app running

With the tests

API integration tests ​

The tests start their own throwaway PostgreSQL in Docker (Testcontainers), run the real app, and call it over HTTP with Alba. They use xUnit v3, which runs on Microsoft.Testing.Platform: global.json opts dotnet test in to it. To run some of them:

bash
dotnet test --filter-class ApiHost.Tests.TodoEndpointsTests    # one class
dotnet test --filter-method "*complete*"                       # methods matching a pattern

A test that waits on something slow can pass TestContext.Current.CancellationToken, so a cancelled run stops at once (the xUnit analyzers point these out).

They cover:

  • the Todo endpoints and validation, health checks, security headers and the API reference,
  • the background TodoCompleted handler and scheduled reminders (with Wolverine's message tracking),
  • SignalR notifications, received by real SignalR clients,
  • Marten the Todo event stream,
  • With accounts registration and email confirmation (following the links in captured emails), login, token refresh and reuse detection, logout and per-user access,
  • With accounts account settings, email changes, password reset, account deletion, lockout and rate limiting,
  • With accounts roles, the admin endpoints and invitations, Google/GitHub sign-in rules, two-factor authentication, password rules, and passkeys (with a software authenticator, so ASP.NET Core Identity's real WebAuthn checks run),
  • With accounts every endpoint's security attributes (EndpointSecurityTests).
  • With organisations organisations kept apart, roles, invitations and switching; plans, trials, limits and Stripe payments (with a fake provider); API keys, the MCP endpoint and webhooks; and the back office.

Writing one:

  • Put it next to the existing tests, in the [Collection("api")] collection, which shares one app and database (AppFixture).
  • For messages, wrap the call in _fixture.Host.ExecuteAndWaitAsync(...) and assert on the tracked session; scheduled messages can be played at once with PlayScheduledMessagesAsync.
  • With accounts _fixture.CreateUserAndGetAccessTokenAsync() gives you a signed-in user. Emails are captured in _fixture.Emails; follow a link with _fixture.Emails.LinkQuery(email, "/confirm-email").
  • With accounts A new endpoint under /api/account/ or /api/admin/ must require a login (and the Admin role for admin endpoints), and one that checks a password, sends email or creates an account must be rate limited, or EndpointSecurityTests fails. Add anonymous or rate-limited endpoints to its lists.

Client unit tests ​

With accounts

They use Vitest and React Testing Library in a simulated browser (jsdom), with the API faked by MSW at the network level, so requests go through the real API client. They sit next to the code they test and cover the Todo page, the API client's token refresh, the login, registration, email-link, invitation and external sign-in pages, route guards, Settings and the admin page.

Without accounts

They use Vitest and React Testing Library in a simulated browser (jsdom), with the API faked by MSW at the network level, so requests go through the real API client. They sit next to the code they test and cover the Todo page and the API client.

Writing one:

  • Fake the API responses the test needs with server.use(http.get('*/api/...', () => HttpResponse.json(...))). A request without a fake fails the test.
  • Render with renderPage or renderRoutes from src/test/render.tsx (a fresh query cache and an in-memory router per test).
  • Find elements the way users do (role, label, text), and interact with userEvent.
  • With accounts features/auth/testing.tsx has helpers to sign in, fake /api/auth/me, and fake the browser's passkey API (jsdom has none).

With the tests

End-to-end tests ​

They drive the running app in Chrome with Playwright, using the Chrome installed on your machine (no browser download).

With accounts

They sign up and confirm accounts with the real emails in Mailpit, use Todos with live updates across tabs, change settings, reset a password, trip the lockout, use the admin page and invitations, and log in with a passkey (Chrome's virtual authenticator).

To run them against the Development setup (docker compose up -d for PostgreSQL and Mailpit), start the API with a higher sign-in rate limit, since every test comes from your machine, and the Vite dev server, then run them:

bash
Accounts__AuthAttemptsPerMinute=1000 dotnet run --project src/ApiHost   # terminal 1
npm run dev                                                              # terminal 2, in src/Client
E2E_BASE_URL=http://localhost:5173 E2E_ADMIN_EMAIL=demo@example.com E2E_ADMIN_PASSWORD='Demo123!' npm test   # in tests/e2e

Against a published app instead, set E2E_BASE_URL to its address, MAILPIT_URL if Mailpit isn't on http://localhost:8025, and list E2E_ADMIN_EMAIL (default admin-e2e@example.com) in the app's Accounts:AdminEmails.

After logging in, page.goto and reloads keep you signed in (the page signs back in with the refresh cookie as it loads), but navigating with the app's links is closer to what users do.

Without accounts

They check the home page, and that Todos are added, completed and shown live in other tabs. Run the API and the Vite dev server, then in tests/e2e: E2E_BASE_URL=http://localhost:5173 npm test.

specs/accessibility.spec.ts checks every page against WCAG 2.2 AA with axe, in light and dark, and keyboard use of the header and its menus. Add each page you build to its lists. axe finds what can be checked automatically (contrast, names, labels, roles, headings), about half of what WCAG asks: also try new pages with only the keyboard, and with a screen reader.

A failed test keeps a trace in tests/e2e/test-results/; open it with npx playwright show-trace <path>/trace.zip.

Documentation for v0.10.0. Released under the MIT licence.