Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:The avatar project employs a comprehensive testing strategy using Vitest as the primary test runner. The strategy is bifurcated into two main categories: unit tests and browser tests, each with its own environment and configuration. This ensures both the logical integrity of individual functions and the correct rendering and behavior in a browser environment.
The testing setup is defined in the root vitest.config.ts and is executed via scripts in package.json. It includes strict code coverage enforcement to maintain code quality and a pre-commit hook to run a full check, including linting, type-checking, and testing.
Framework and Tooling
The project relies on a modern set of tools for its testing needs, managed via pnpm and defined in the monorepo's configuration files.
| Tool | Purpose | Source -
| vitest | Test runner, assertion library, and mocking. | pnpm-workspace.yaml:15 -
| @vitest/browser-playwright | Playwright provider for browser-based tests. | pnpm-workspace.yaml:13 -
| playwright | Browser automation library. | pnpm-workspace.yaml:15 -
| @vitest/coverage-v8 | Code coverage provider using V8's engine. | pnpm-workspace.yaml:14 -
Sources: [pnpm-workspace.yaml:12-15](https://github.com/nextorders/avatar/blob/HEAD/pnpm-workspace.yaml#L12-L15)
Test Organization
Tests are organized into two distinct projects within Vitest, allowing for separate configurations and execution environments. This separation is defined in vitest.config.ts.
This diagram illustrates the two main testing projects.
Sources: [vitest.config.ts:5-33](https://github.com/nextorders/avatar/blob/HEAD/vitest.config.ts#L5-L33)
Test Projects Configuration
| Project | Name | Environment | Included Files -
| unit | unit | node | **/test/unit/**/*.{test,spec}.ts, **/test/**/*.unit.{test,spec}.ts -
| browser | browser | playwright | **/test/browser/**/*.{test,spec}.ts, **/test/**/*.browser.{testspec}.ts -
Sources: [vitest.config.ts:6-32](https://github.com/nextorders/avatar/blob/HEAD/vitest.config.ts#L6-L32)
Test Execution Scripts
Several npm scripts are available to run the tests.
| Script | Command | Description - |
|---|---|---|
test | vitest run | Runs all tests (both unit and browser) once. - |
test:watch | vitest | Runs tests in watch mode, re-running on file changes. - |
test:coverage | vitest run --coverage | Runs all tests and generates a code coverage report. - |
check:full | pnpm run lint:fix && pnpm run typecheck && pnpm vitest run | A comprehensive check that includes linting, type-checking, and running all tests. This is likely used in CI/CD pipelines or pre-commit hooks. |
Sources: [package.json:15-28](https://github.com/nextorders/avatar/blob/HEAD/package.json#L15-L28)
Unit Testing
Unit tests are located in packages/avatar/test/unit/ and run in a Node.js environment. They focus on testing individual functions and modules in isolation.
Sources: [vitest.config.ts:10-17](https://github.com/nextorders/avatar/blob/HEAD/vitest.config.ts#L10-L17), [tsconfig.json:35](https://github.com/nextorders/avatar/blob/HEAD/tsconfig.json#L35)
Core Functionality: createAvatar
The primary function createAvatar is tested to ensure it behaves as expected under various conditions.
The following diagram shows the sequence of a typical test for the createAvatar function.
Key test cases for createAvatar include:
- Existence: Verifies that
createAvataris a function. - Default Behavior: Ensures it returns a string (SVG) when called with only the required
seedparameter. - Error Handling: Confirms that an
Erroris thrown if the requiredseedis not provided. - Output Correctness: Checks that the generated SVG string contains specific path data for a given set of options, ensuring deterministic output.
- Output Integrity: Validates that the output is a well-formed SVG by checking for the
<svg>tag. - Output Size: Ensures the generated SVG is within a reasonable size range (e.g., > 0 KB and < 20 KB).
// Example test case from packages/avatar/test/unit/index.test.ts
it('should return correct result with emotion 5', () => {
const result = createAvatar({ seed: 'random4', emotion: 5 });
expect(result).toContain('m157.9 261 2-.5c7.7-2 15.7-5.4');
});
Sources: [packages/avatar/test/unit/index.test.ts:4-41](https://github.com/nextorders/avatar/blob/HEAD/packages/avatar/test/unit/index.test.ts#L4-L41)
Utility Functions: random
Utility functions are also unit-tested. For example, getRandInteger is tested for its correctness in various scenarios.
Key test cases for getRandInteger:
- Generates a random number within a specified positive range.
- Generates a random number within a specified negative range.
- Correctly handles the edge case where
minandmaxare the same. - Correctly handles the edge case where
maxis less thanmin.
// Example test case from packages/avatar/test/unit/random.test.ts
it('should return min if max is less than min', () => {
const result = getRandInteger(1, 0);
expect(result).toBe(1);
});
Sources: [packages/avatar/test/unit/random.test.ts:4-25](https://github.com/nextorders/avatar/blob/HEAD/packages/avatar/test/unit/random.test.ts#L4-L25)
Browser Testing
The project is configured for browser testing using Playwright as the provider for Vitest.
- Provider:
@vitest/browser-playwright - Browser: Tests are run on
chromium. - Mode: Tests are executed in
headlessmode. - File Location: Browser-specific tests are expected to be in
**/test/browser/directories or have a.browser.{test,spec}.tsextension.
Sources: [vitest.config.ts:19-32](https://github.com/nextorders/avatar/blob/HEAD/vitest.config.ts#L19-L32)
Code Coverage
Code coverage is enforced to ensure a high standard of testing across the codebase. The configuration mandates a minimum of 80% coverage for statements, branches, functions, and lines.
Coverage Configuration
| Parameter | Value - |
|---|
| provider | @vitest/coverage-v8 elliptical, release, nuxt, app, drizzle}.config., **/{vue-i18n}.options., and default Vitest exclusions. | | thresholds |statements: 80, branches: 80, functions: 80, lines: 80` -
Sources: [vitest.config.ts:34-47](https://github.com/nextorders/avatar/blob/HEAD/vitest.config.ts#L34-L47)
This configuration ensures that all new and existing code is well-tested, preventing regressions and maintaining a high level of quality.