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.
In this repository101 other files it gives its agents.
- ai-video-understandingSkill
- apple-foundation-modelsSkill
- arch-dddSkill
- arch-decision-recordsSkill
- arch-diagramsSkill
- arch-eventsSkill
- arch-mvp-roadmapSkill
- arch-python-modernSkill
- aws-app-runnerSkill
- aws-cicd-patternsSkill
- aws-security-hardeningSkill
- azure-functions-python-v2Skill
- azure-identity-m2m-authSkill
- azure-service-bus-messagingSkill
- azure-workflow-execution-modelsSkill
- caddy-tls-proxySkill
- compliance-frameworksSkill
- design-system-workflowSkill
- dev-debugging-strategiesSkill
- dev-standardsSkill
- docker-python-poetrySkill
- docker-pythonSkill
- fastapi-patternsSkill
- ffmpeg-laravel-videoSkill
- formik-yup-react-nativeSkill
- frontend-accessibilitySkill
- frontend-angular-aiSkill
- frontend-angularSkill
- frontend-angular-toolingSkill
- frontend-design-systemsSkill
- frontend-material-chatSkill
- frontend-material-design-3Skill
- frontend-svelteSkill
- gcp-cicd-patternsSkill
- gcp-cloud-runSkill
- gcp-cloudsql-infrastructureSkill
- gcp-finopsSkill
- gcp-observabilitySkill
- gcp-security-hardeningSkill
- github-actions-cicdSkill
- healthkit-biofeedbackSkill
- infra-terraformSkill
- ios-app-qualitySkill
- ios-audio-hapticsSkill
- langchain-azure-openai-patternsSkill
- laravel-auth-hardeningSkill
- laravel-ci-mysqlSkill
- laravel-mix-webpackSkill
- laravel-modern-patternsSkill
- laravel-pint-patternsSkill
- laravel-s3-storageSkill
- laravel-sail-dockerSkill
- mobile-app-deploymentSkill
- mobile-e2e-testingSkill
- mobile-offline-syncSkill
- mobile-push-notificationsSkill
- mobile-video-uploadSkill
- mongodb-asyncmongoclient-patternsSkill
- mongodb-atlas-patternsSkill
- nestjs-patternsSkill
- The first 60 shown.
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
- Very specific: line 34
Unit tests: use Test.createTestingModule + getRepositoryToken(Entity) + createMock<Repository<Entity>>() from @golevelup/ts-jest …
≈ “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.
Files
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
| Layer | Test type | Why |
| Service business logic | Unit (mock repo) | Highest priority — most logic lives here |
| Controller routing / DTO validation | Unit + e2e | Unit for shape; e2e for the validation pipe |
| Guards / interceptors | Unit | Pass + fail paths both required |
| Repository custom queries | Integration with real Postgres | Mocks lie about query semantics |
| Auth flows end-to-end | e2e | Whole 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
AppModuleonce inbeforeAll(notbeforeEach), apply the productionValidationPipe, andawait app.close()inafterAll— 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: trueagainst a shared dev DB in tests → schema drift on every runbeforeEach(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.
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
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.

