Setup and teardown are balanced
Why it matters
When a beforeAll or beforeEach hook creates database rows, writes files, or inserts records without a matching afterAll/afterEach to remove them, test data accumulates across runs. Subsequent test runs operate on a progressively more polluted database: unique-constraint violations start appearing, count assertions become wrong, and test order begins to matter. This makes the test suite flaky and eventually unmaintainable. ISO-25010:2011 recoverability requires that test runs return the system to a clean state.
Severity rationale
Medium because unbalanced setup causes intermittent failures and cross-test contamination that are difficult to diagnose, though immediate test runs may still pass.
Remediation
Every beforeAll/beforeEach that creates state needs a corresponding afterAll/afterEach that removes it. Track created entity IDs in the outer scope and delete them in teardown:
describe('UserAPI', () => {
let userId: string
beforeAll(async () => {
const user = await prisma.user.create({ data: { email: 'test@example.com' } })
userId = user.id
})
afterAll(async () => {
await prisma.user.deleteMany({ where: { id: userId } })
})
it('retrieves the user', async () => {
const found = await prisma.user.findUnique({ where: { id: userId } })
expect(found?.email).toBe('test@example.com')
})
})
For simpler isolation, run each test inside a database transaction and roll it back in afterEach — no manual tracking required.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Test Reality Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- iso-25010:2011 · reliability.recoverability — Recoverability (reliability sub-characteristic)
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-slop-test-theater·automated