Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- CONTRIBUTING.md
- AGENTS.md
- examples/basic/e2e/todo-item.resolver.spec.ts
- packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts
- packages/query-graphql/tests/types/query/none-query-args.type.spec.ts
- examples/hooks/e2e/todo-item.resolver.spec.ts
- examples/sequelize/e2e/todo-item.resolver.spec.ts
The nestjs-query project implements a multi-layered testing strategy to ensure the reliability of its CRUD libraries. The strategy categorizes tests into unit, integration, and end-to-end (E2E) levels, requiring all contributors to include relevant tests for new features or bug fixes.
Sources: CONTRIBUTING.md:46-52, AGENTS.md:12-13
Test Categories
The project separates testing concerns based on the scope of the code and the presence of external dependencies.
| Test Type | Scope | Storage Dependency | Location |
|---|---|---|---|
| Unit Tests | Individual classes and functions. | None (mocked). | packages/*/test/ |
| Integration Tests | Interaction between modules (e.g., GraphQL and TypeORM). | In-memory or mocking. | packages/*/__tests__/ |
| E2E Tests | Full stack validation through API calls. | Persistent (Docker). | examples/*/e2e/ |
Sources: CONTRIBUTING.md:73-108, AGENTS.md:14-16
Unit Testing
Unit tests focus on individual code paths, including error cases and conditionals. These tests must not rely on persistent storage. Developers place unit test files in a test directory that mirrors the src directory structure, using the .spec.ts suffix.
Sources: CONTRIBUTING.md:73-86
Integration Testing
Integration tests verify that different project packages work together correctly. For example, integration tests verify that DataLoader prevents N+1 query issues when resolving GraphQL relationships across persistence layers.
Sources: packages/query-graphql/tests/integration/federation-n1/federation-n1-integration.spec.ts:108-142
End-to-End (E2E) Testing
E2E tests ensure high-level functionality works across the entire stack. Most E2E tests reside within the examples/ directory to validate library features in realistic application contexts.
Sources: CONTRIBUTING.md:88-100, AGENTS.md:33
Testing Infrastructure
The project uses Jest as the primary test runner and Docker Compose to manage backing services required for E2E validation.
Sources: CONTRIBUTING.md:120-137
The diagram shows the flow from a developer command to the execution of different test suites, highlighting the dependency on Docker for E2E tests.
Sources: CONTRIBUTING.md:104-137, AGENTS.md:21-25
E2E Implementation Logic
E2E tests utilize supertest to issue GraphQL queries and mutations against a running NestJS application.
Lifecycle Management
- Setup: The
beforeAllblock compiles theTestingModule, initializes the Nest application, and refreshes the database using provider-specific fixtures (e.g.,DataSourcefor TypeORM orSequelize). - Global Pipes: The application uses
ValidationPipeto enforce DTO constraints during testing. - Execution: Tests send POST requests to the
/graphqlendpoint. - Teardown: The
afterAllblock closes the application and cleans up database connections.
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:20-42, examples/sequelize/e2e/todo-item.resolver.spec.ts:37-54
Data Flow for Resolver Testing
The following sequence diagram illustrates a typical E2E test for a GraphQL mutation.
This diagram represents the request lifecycle from a supertest call to the database response, including input validation.
Sources: examples/basic/e2e/todo-item.resolver.spec.ts:300-335, examples/hooks/e2e/todo-item.resolver.spec.ts:245-280
Validation Rules in Tests
Tests verify several critical areas of the framework:
- Filtering and Sorting: Validating that generated filters and sort orders correctly influence the result set.
- Paging: Testing
Cursor,Offset, andNonepaging strategies. - Hooks: Ensuring
beforeCreateOne,beforeUpdateMany, and other lifecycle hooks execute correctly. - Authorization: verifying that resources are forbidden when appropriate headers are missing.
Sources: packages/query-graphql/tests/types/query/none-query-args.type.spec.ts:80-100, examples/hooks/e2e/todo-item.resolver.spec.ts:553-605, examples/sequelize/e2e/todo-item.resolver.spec.ts:279-290
Running Tests
Execution is managed via Nx and Yarn commands:
- Linting:
yarn nx lint <project> - Unit Tests:
yarn nx test <project>ornpm run jest:unit - E2E Tests:
npm run jest:e2e(requiresdocker compose up -din the examples directory) - All Tests:
npm run jest
Sources: CONTRIBUTING.md:112-140, AGENTS.md:21-26
In summary, the nestjs-query testing strategy focuses on comprehensive coverage through tiered test suites, utilizing Docker for environmental consistency and specialized integration tests to prevent performance regressions like N+1 queries.