Testing Strategy (Unit & E2E)
Relevant source files
The following files were used as context for generating this wiki page:
The testing strategy for the regxa project is designed to ensure deterministic module behavior and verify live integration with external package registries. The system utilizes Vitest as the primary test runner, splitting the test suite into focused unit tests and network-sensitive end-to-end (E2E) smoke checks.
Unit tests focus on the internal logic of PURL parsing, registry adapters, and cache management using mocks and spies. E2E tests provide a "smoke check" layer, performing real HTTP requests against live registries like npm, PyPI, and Crates.io to ensure the current ecosystem adapters remain compatible with upstream API changes.
Sources: test/AGENTS.md:3-8, AGENTS.md:104-106
Test Architecture and Organization
The project organizes tests into two distinct directories to separate concerns between local logic validation and external integration.
Test Structure
| Category | Location | Purpose | Methodology |
|---|---|---|---|
| Unit Tests | test/unit/ | Validates modules like PURL parsing, cache TTL, and data normalization. | Deterministic, uses vi.fn() spies and module mocks. |
| E2E Tests | test/e2e/ | Verifies that adapters can successfully communicate with live registry APIs. | Live network calls, uses real HTTP. |
Sources: test/AGENTS.md:12-16, test/AGENTS.md:31-32
Component Coverage Mapping
Specific test files are assigned to cover individual system components:
- PURL Parsing:
test/unit/purl.test.tsvalidates the parsing and building of Package URLs. - Registry Adapters:
test/unit/registry.test.tsandtest/unit/registries.test.tscover factory registration and ecosystem-specific behaviors. - Cache & Lockfile:
test/unit/lockfile.test.tstests TTL policies, freshness, and integrity checks. - Normalization:
test/unit/license.test.tsandtest/unit/repository.test.tsensure canonical output for metadata.
Sources: test/AGENTS.md:20-25
Unit Testing Strategy
Unit tests are designed to be deterministic and fast. They rely on the Client class being mocked to return predefined JSON payloads, allowing developers to test how adapters handle various API responses (including error states like 404s) without hitting actual networks.
Mocking and Data Flow
In unit tests, vi.spyOn(client, "getJSON") is frequently used to intercept network calls and return mock data structures that simulate registry responses.
The diagram shows how unit tests isolate the implementation logic by providing controlled inputs through a mocked Client.
Sources: test/unit/registries.test.ts:15-58, test/unit/alpm.test.ts:79-88
Key Logic Validations
- Normalization: Ensuring disparate registry formats (e.g., PyPI's
project_urlsvs npm'srepository) are converted into a unifiedPackagestructure. - Error Handling: Verifying that HTTP 404 errors from the client are correctly mapped to internal
NotFoundErrorclasses. - Version Selection: Testing the
selectVersionhelper to ensure it correctly skips yanked or deprecated versions.
Sources: test/unit/registries.test.ts:68-76, test/unit/helpers.test.ts:50-54, AGENTS.md:79-82
E2E Smoke Testing
E2E tests serve as a high-level verification that the library remains functional against real-world APIs. These tests are network-sensitive and may fail due to rate limiting or transient network issues.
Sequence of an E2E Check
The E2E suite iterates through all supported ecosystems to confirm registration and basic data retrieval.
This sequence illustrates the direct interaction with live services during an E2E run.
Sources: test/e2e/smoke.test.ts:25-34, test/e2e/smoke.test.ts:1-7
Supported Ecosystem Verification
The smoke.test.ts file ensures that all six core ecosystems are correctly registered in the factory:
- npm
- cargo
- pypi
- gem
- composer
- alpm
Sources: test/e2e/smoke.test.ts:13-22
Testing Conventions and Tooling
The project enforces specific conventions to maintain test quality and consistency.
- Vitest Globals: Globals like
describe,it, andexpectare enabled to avoid repetitive imports. - Naming: All test files must use the
*.test.tssuffix. - Clean Slate:
beforeEachandafterEachhooks are used to clear mocks and restore spies between individual tests. - Single Test Execution: Developers can run specific tests using the
-tfilter (e.g.,pnpm vitest run -t "should parse valid PURL").
Sources: AGENTS.md:21-23, test/unit/registries.test.ts:8-13, test/AGENTS.md:30-31
Execution Commands
| Command | Description |
|---|---|
pnpm test | Runs Vitest in watch mode for development. |
pnpm test:run | Single CI-style run of all tests. |
pnpm vitest run test/e2e/smoke.test.ts | Manually triggers live network smoke tests. |
Sources: AGENTS.md:18-20, test/e2e/smoke.test.ts:3
Anti-Patterns to Avoid
The test/AGENTS.md file explicitly identifies practices that should be avoided to maintain the integrity of the test suite:
- Deterministic Logic in E2E: Do not use E2E tests for logic checks that can be handled by deterministic unit suites.
- Coverage Gaps: New modules must not be added without corresponding unit tests.
- Production Code in Test: Production helpers should never be placed within the
test/directory.
Sources: test/AGENTS.md:35-37
The testing strategy combines the speed and reliability of mocked unit tests with the necessary validation of live integration tests, ensuring that the regxa library remains robust against both internal logic errors and external API changes.