organizationId filter, because the mock returns whatever the test prepared regardless of where. Only an integration test on a real database with two organizations catches that.
Test types
npm run verify is the gate you run before handing in work: lint, typecheck, npm test, the production build, workspace package checks and hook tests. It does not run integration, e2e or mutation tests, because they need a database or a browser, and a full mutation run takes about an hour. Integration and e2e tests run in separate CI jobs; mutation tests run locally or when you trigger the workflow manually. So before you hand in a change to a query, a command or the sign-in flow, run them yourself.
Unit and component tests
npm test runs Vitest with vitest.config.ts:
- jsdom environment so component tests can render React. A file that needs plain Node starts with
// @vitest-environment node. vitest.setup.tsadds the Testing Library DOM matchers (such astoBeInTheDocument) and loads.env.localand.env. Tests seeDATABASE_URLbut never connect, because Prisma is replaced.- Files:
src/**/*.test.ts(x),scripts/**/*.test.tsande2e/**/*.test.ts, plus project packages inpackages/*/src.*.integration.test.tsfiles are excluded. globals: truegives youdescribe,itandexpectwithout imports;clearMocks: trueresets mock call history before each test.server-onlyis aliased to an empty module, so server files can be imported in tests.
auth.api. Keep the file containing the security rule under test real. If you mock the file that checks the admin role, the test can no longer see that rule.
There are two ways to replace Prisma:
vi.mock("@/shared/lib/prisma", …)withvi.fn()functions, when the test checks the order of steps and the data passed (seesrc/features/users/services/commands/__tests__/invite-user-command.test.ts).createFakePrismafromsrc/test/fake-prisma.ts, when the test must prove thatwherereally filters. It is an in-memory Prisma for theusers,organizations,membersandsessionstables that evaluateswhere(AND,OR,NOT,equals,in,notIn,not, relation filters),orderBy,select,include,takeandskip. An unsupported operator throws, so a test never passes silently.
npx vitest run <path>, or watch mode with npm run test:watch.
Integration tests
Integration tests exercise queries, commands and guards on real Postgres. They live in*.integration.test.ts files and are not part of npm test. The core has one, the organization isolation test; kit modules add their own.
vitest.integration.config.ts:
nodeenvironment, filessrc/**/*.integration.test.ts;- a
globalSetupthat creates the test database if missing and syncs its schema withprisma db push; - a setup file that points
DATABASE_URLat the test database and sets the variables the app needs (auth secret,APP_URL,EMAIL_FROM,API_KEY_PEPPER, S3 settings for MinIO,MULTI_ORG=false,AUTH_PUBLIC_SIGNUP=false); fileParallelism: false, because every file shares one database and every test starts by truncating tables;testTimeout: 20_000andhookTimeout: 60_000.
src/test/integration/database.ts:
Run them
1
Start the database
uploads module, also start MinIO and create the bucket (these services use the s3 profile):2
Run the tests
postgresql://postgres:postgres@localhost:5432/wa_app_test, and globalSetup creates it if needed. If your Postgres runs on another port, set the URL in the shell:.env into process.env here, so a value in .env is ignored and the tests fall back to port 5432.3
Check where they ran
Prisma prints
Datasource "db": PostgreSQL database "wa_app_test", schema "public" at "localhost:5432" first. Check the database name and port.npm run test:integration -- <path>. Plain npx vitest run <path> uses the unit config, skips integration files and exits with No test files found, exiting with code 1.
End-to-end tests
E2E tests check flows that involve every part at once in a browser: invitation by email, setting a password, sign-in, sign-up and creating an organization.npm run test:e2e:
- runs
npm run build(standalone output) and copies.next/staticinto the standalone folder; - starts two
node .next/standalone/server.jsservers with different settings and waits for/sign-into respond (up to 120 seconds); - runs
e2e/global-setup.ts: checks the database name, drops and rebuilds the e2e database, creates the admin account and empties the mailpit inbox; - runs two Playwright projects, each against its own server.
Tests run serially with one worker and no retries; traces are kept only for failures. Tests find elements by Polish UI labels, most of them in the
LABELS constant in e2e/helpers.ts.
Before the first run
1
Start the database and mailpit
docker compose up -d. The wa_app_e2e database is created by Prisma.2
Install Chromium once
3
Free ports 3000 and 3001
Stop
npm run dev. Playwright doesn’t reuse a running server and fails before touching the database if a port is busy. Use E2E_CORE_PORT and E2E_MULTI_ORG_PORT to change them.4
Pass custom ports in the shell
The e2e config reads ports only from environment variables, not from
.env:Prisma refuses
db push --force-reset and migrate reset when it detects it was started by an AI agent (for example inside Claude Code), unless PRISMA_USER_CONSENT_FOR_DANGEROUS_AI_ACTION is set. So npm run test:e2e launched by an agent stops at the database reset. Run e2e tests yourself, or let an agent run them only with your explicit consent. Integration tests usually work under an agent, because a plain prisma db push only asks when a schema change would drop data.Commands that reset a database
npm test, npm run verify, npm run test:hooks, npm run test:mutation, npm run test:mutation:task and npm run test:mutation:gate never touch a database.
What the name check does not cover:
- The server. Every GatoJS project uses the same default names
wa_app_testandwa_app_e2e, and both commands default to port 5432. If you moved your Postgres to another port because 5432 belongs to another project, and didn’t pass the port in the shell, the tests will wipe that project’s test database. Checkecho "$DB_PORT $DATABASE_URL $INTEGRATION_DATABASE_URL"before running. - What the database is for. Any name with the suffix passes, such as
orders_test. Don’t give those names to databases you want to keep. - Manual Prisma commands, which work on
DATABASE_URLwith no name check.
MAILPIT_URL (default http://localhost:8025), which is the same mailpit npm run dev sends to.
Organization isolation test
Prisma does not filter by organization on its own. Every query and command adds theorganizationId filter from getOrgContext(), and the kit’s runtime tenant-scope guard throws when a scoped model is queried without one. What neither the guard nor a mocked unit test can tell you is whether the filter uses the right organization. That takes a test on real Postgres with two organizations, and the project rules require one for every new query and command on domain data. See Organizations for the data model.
Add an isolation test when you add:
- a query that reads organization data;
- a command that creates, updates or deletes organization data (the most common leak is updating a record by an
idfrom the input withoutorganizationIdinwhere); - a change to
wherein an existing query or command; - a fix for a leak. The test must fail without the fix.
How to build one
Create a separate file such assrc/features/<x>/services/__tests__/<x>-isolation.integration.test.ts.
1
Copy the stubs and signInAs
The session stub (If your command goes through
currentSession, readMockedSession in a vi.hoisted block), the vi.mock calls and signInAs are defined in src/features/organizations/services/__tests__/organization-isolation.integration.test.ts itself, so your file needs its own copy. They replace what doesn’t exist outside an HTTP request: headers, locale, notFound, redirect and auth.api.getSession. signInAs creates a real session in the database and returns it from the stub:handleCommandError, give the redirect error a digest (NEXT_REDIRECT;replace;<href>;307;) and mock next/cache. The feature tutorial explains why.2
Seed two organizations in beforeEach
Call
resetDatabase(), create users and two organizations with the helpers, then create feature records in both organizations with literal values. Call prisma.$disconnect() in afterAll.3
Test the query
Sign in as a user of organization A, call the query and compare the result with a literal that contains only A’s records. Repeat for B.
4
Test the command
Sign in as a user of A and call the command with the
id of a record in B. Assert that it refused (a literal OperationResult), then read B’s record straight through prisma and compare it with the seeded values.5
Test a missing membership
Sign in as user A with B as the active organization and assert the query throws
NoOrganizationError.6
Make sure it can fail
Temporarily point the
where filter at the other organization and run the test. It must go red. npm run test:mutation:integration does this automatically for every file under src/features/*/services/.resetDatabase() truncates the core tables with CASCADE, so a feature table with a foreign key to organizations or users is cleared too. A table without such a key must be cleared in your own beforeEach with prisma.<model>.deleteMany().
Mutation testing
Stryker creates mutants (copies of your code with one deliberate change, such as a flipped condition) and runs the tests covering each one. A mutant no test catches points at behavior without a test. Mutation testing is central to how GatoJS works with AI agents, so it is covered in detail in Built for AI agents. The short version:
Never run two Stryker processes at once: they share report files and the test database.
Run times and CI
Indicative local timings on an 8-core machine:
Incremental runs are much shorter, because Stryker only retests mutants in changed code.
Mutation tests don’t run on pull requests because a full run is too slow. Run them per task locally, and the full suite before a release or by triggering Mutation testing in the Actions tab. More in CI.
Command reference
Test environment variables
All are read from the shell only; neither the integration config nor Playwright loads.env.