agentleFS
ai-enhanced-engineerai-enhanced-engineer/aiee-skills/skills/jest-nestjs-testing/SKILL.md

jest-nestjs-testing

Skill4 starsChanged 5 months agoMITView on GitHub

Unit and e2e testing for NestJS with Jest, Test.createTestingModule, supertest, and TypeORM. Covers repository mocking, test database isolation, auth in e2e tests, and coverage targets per layer. Use when writing or reviewing NestJS tests.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Unit tests: use Test.createTestingModule + getRepositoryToken(Entity) + createMock<Repository<Entity>>() from @golevelup/ts-jest …

    line 34

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Jest Testing for NestJS Services

NestJS testing has a few load-bearing primitives: Test.createTestingModule for assembling a DI graph, getRepositoryToken(Entity) for swapping the repo, INestApplication + supertest for e2e, and a real Postgres test DB (SQLite-in-memory breaks on uuid and jsonb).

For test naming (test__<what>__<expected>) and coverage thresholds, see unit-test-standards. The async-test lifecycle parallels pytest-fastapi-async (dependency_overrides ↔ overrideProvider, fixture teardown ↔ app.close() in afterAll).

When to use

  • Writing the first unit test for a service
  • Adding e2e coverage for an authenticated route
  • Eliminating Jest open-handle warnings or hangs
  • Migrating from "mock everything" to integration-with-test-DB

Decision: unit vs e2e

LayerTest typeWhy
Service business logicUnit (mock repo)Highest priority — most logic lives here
Controller routing / DTO validationUnit + e2eUnit for shape; e2e for the validation pipe
Guards / interceptorsUnitPass + fail paths both required
Repository custom queriesIntegration with real PostgresMocks lie about query semantics
Auth flows end-to-ende2eWhole pipeline matters

Skeletons

  • Unit tests use Test.createTestingModule + getRepositoryToken(Entity) + createMock<Repository<Entity>>() from @golevelup/ts-jest (auto-mocks every method including QueryBuilder chains).
  • E2E tests boot the full AppModule once in beforeAll (not beforeEach), apply the production ValidationPipe, and await app.close() in afterAll — without it Jest hangs.

See examples.md for the full unit and e2e test skeletons, controller test with guard bypass, guard unit test, and DB truncation helper.

Anti-patterns

  • Mocking down to the repository in every test → 80% coverage, 0% confidence
  • Shared test DB state across tests → order-dependent flakes
  • Snapshot tests for HTTP responses → churn on every harmless change
  • Asserting on log output → brittle
  • Tests with no negative-path assertions → "happy path only" coverage
  • synchronize: true against a shared dev DB in tests → schema drift on every run
  • beforeEach(app.init) → re-creates the app per test; ~10× slower e2e suite, also masks shared-state bugs
  • Missing afterAll(app.close) → Jest "open handle" warning, eventual hang

See reference.md for repository mock patterns, the three test-DB isolation strategies, and auth in e2e. See examples.md for project-specific specs.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.