Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- src/tests/README.md
- src/tests/versioned_codebase_context.test.ts
- e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
- scripts/generate-playwright-summary.js
- src/prompts/system_prompt.ts
- src/prompts/security_review_prompt.ts
- e2e-tests/snapshots/security_review.spec.ts_security-review---multi-select-and-fix-issues-1.aria.yml
The Dyad project employs a two-pronged testing strategy to ensure code quality and application stability. This includes unit and integration testing using the Vitest framework for isolated function and module verification, and comprehensive end-to-end (E2E) testing using Playwright to validate application behavior from a user's perspective.
This strategy is supported by a CI process that automatically runs tests, generates reports, and provides feedback on pull requests. The testing approach heavily relies on snapshot testing, particularly for E2E tests, to capture and verify the complex conversational outputs of the AI.
Unit & Integration Testing
Unit and integration tests are managed with Vitest, a modern test runner built on top of Vite. These tests are located in the src/__tests__/ directory and focus on testing individual functions and modules in isolation.
Sources: src/tests/README.md
Setup and Commands
The project's package.json is configured with scripts to run the Vitest test suite.
| Command | Description |
|---|---|
npm run test | Runs all tests once. |
npm run test:watch | Runs tests in watch mode, re-running them when files change. |
npm run test:ui | Runs tests with the Vitest UI reporter for a graphical interface. |
Sources: src/tests/README.md:9-15
Mocking Dependencies
A key aspect of the unit testing strategy is the extensive use of mocking to isolate the code under test from its dependencies. The vi.mock() function from Vitest is used to replace external modules like node:fs, isomorphic-git, and electron.
This diagram illustrates the mocking process for an IPC handler test.
This diagram shows how a test file mocks the electron module to control and inspect calls to ipcMain during a test run.
Sources: src/tests/README.md:19-41, src/tests/versioned_codebase_context.test.ts:7-19
Example: Testing Versioned Codebase Context
The file src/__tests__/versioned_codebase_context.test.ts provides a clear example of the unit testing approach. It tests the parseFilesFromMessage and processChatMessagesWithVersionedFiles functions.
Key features of this test file include:
- Mocking: It mocks the
@/ipc/utils/git_utilsmodule to simulate fetching file content from specific git commits without actual git operations. - Describe Blocks: Tests are organized into logical groups using
describe()for different functions and scenarios (e.g., "basic functionality", "processing assistant messages", "error handling"). - Assertions: It uses
expect()to assert the output of functions against expected values, including complex data structures and error handling behavior.
Sources: src/tests/versioned_codebase_context.test.ts
End-to-End (E2E) Testing
E2E testing is performed using Playwright. These tests are located in the e2e-tests/ directory and are designed to simulate user interactions and verify the application's behavior, especially the AI's conversational responses.
Snapshot Testing
The core of the E2E strategy is snapshot testing. Instead of asserting specific UI elements, tests generate a transcript of the interaction between the user and the AI. This transcript is saved as a snapshot file (e.g., .txt or .aria.yml) and compared against a previously approved version on subsequent test runs.
The snapshot files capture:
- System Prompts: The initial instructions and context given to the AI.
- User Messages: The inputs provided by the user during the test.
- Assistant Responses: The AI's full response, including text and special tags like
<dyad-write>. - UI/Accessibility Tree:
.aria.ymlfiles capture a simplified representation of the UI, likely for accessibility or structural verification.
This flow is illustrated below:
This flowchart shows the E2E snapshot testing process, from user simulation to comparison with a stored snapshot.
Sources: e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt, e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt, e2e-tests/snapshots/security_review.spec.ts_security-review---multi-select-and-fix-issues-1.aria.yml
Test Case Structure
A typical E2E snapshot test file contains a sequence of messages, each with a role (system, user, or assistant) and message content. This format allows for precise replication of a conversational flow.
For example, a test for "ask mode" includes:
- A
systemmessage defining the AI's role and rules (no code generation). - A
usermessage providing the initial codebase context. - An
assistantmessage with a canned "ready to help" response. - A
usermessage with the actual test prompt. - An
assistantmessage containing the AI's generated explanation, which is then snapshotted.
Sources: e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
CI Reporting
The project uses a Node.js script to generate a summary of Playwright test results for pull request comments. This provides immediate feedback to developers within the GitHub UI.
Sources: scripts/generate-playwright-summary.js
Report Generation Flow
The scripts/generate-playwright-summary.js script is executed in the CI pipeline after the Playwright tests complete.
This sequence diagram details how the reporting script processes test results and interacts with the GitHub API to post a summary comment on a pull request.
Sources: scripts/generate-playwright-summary.js
Key Metrics
The script aggregates test results and presents them in a clear table format, tracking the following metrics for each operating system:
| Metric | Description |
|---|---|
| Passed | Tests that completed successfully. |
| Failed | Tests that failed, timed out, or were interrupted. |
| Skipped | Tests that were intentionally not run. |
| Flaky | Tests that failed on a previous attempt but passed after a retry. |
The script also lists the specific titles of failed and flaky tests to help developers quickly identify issues.
Sources: scripts/generate-playwright-summary.js:154-184, 201-231