Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- package.json
- nx.json
- CLAUDE.md
- src/utils/config/tests/migration-scripts/README.md
- src/utils/config/tests/migration-scripts/all-migrations.test.ts
- src/utils/google-drive/tests/sync.test.ts
- src/entrypoints/selection.content/selection-toolbar/tests/selection-toolbar.test.tsx
- src/utils/tests/url.test.ts
- src/utils/host/tests/translate-text.test.tsx
- src/utils/google-drive/tests/conflict-merge.test.ts
The Read Frog project employs a comprehensive testing strategy centered around the Vitest testing framework to ensure code quality, maintain stability, and prevent regressions. The strategy encompasses multiple layers of testing, including unit, component, integration, and a specialized automated system for configuration migrations. This approach allows for both isolated function validation and complex user-interaction simulations.
Tests are managed through pnpm scripts and optimized with Nx for caching, improving development workflow efficiency.
Framework and Tooling
The testing infrastructure is built on Vitest, a modern test runner, and is complemented by several other tools to cover different aspects of the application.
- Test Runner: Vitest
- Component Testing: React Testing Library
- DOM Environment: JSDOM
- Coverage: Istanbul
- Build & Caching: Nx
Sources: package.json, nx.json, CLAUDE.md:49
Test Scripts
The package.json file defines the core scripts for running tests.
| Script | Description |
|---|---|
pnpm test | Runs all tests once. |
pnpm test:watch | Runs tests in watch mode, re-running on file changes. |
pnpm test:cov | Runs all tests and generates a coverage report using Istanbul. |
Sources: package.json:11-14, CLAUDE.md:56-61
Caching with Nx
The project uses Nx to manage tasks, including tests. The nx.json configuration enables caching for test runs, which significantly speeds up the process by only re-running tests affected by code changes.
// nx.json
"test": {
"cache": true,
"inputs": [
"{projectRoot}/src/**/*",
"{projectRoot}/vitest.*.ts",
"{projectRoot}/tsconfig.json"
]
},
"test:cov": {
"cache": true,
"inputs": [
"{projectRoot}/src/**/*",
"{projectRoot}/vitest.*.ts",
"{projectRoot}/tsconfig.json"
],
"outputs": ["{projectRoot}/coverage"]
}
This configuration specifies that the test and test:cov targets are cacheable. The cache is invalidated if any files in src or the Vitest configuration change.
Sources: nx.json:28-45
Test Types
The project utilizes several types of tests to ensure comprehensive coverage.
This diagram shows the different categories of tests employed in the repository.
Unit Tests
Unit tests focus on validating individual, isolated functions. They are typically used for pure utility functions that have clear inputs and outputs. These tests ensure that the core logic of small code units works as expected.
Example: matchDomainPattern
The tests for matchDomainPattern cover various scenarios, including exact matches, subdomain matching, non-matching cases, and edge cases like invalid URLs.
// src/utils/__tests__/url.test.ts
describe('matchDomainPattern', () => {
describe('exact domain match', () => {
it('should match exact domain with https', () => {
const result = matchDomainPattern('https://x.com', 'x.com')
expect(result).toBe(true)
})
})
describe('subdomain matching', () => {
it('should match subdomain with single level', () => {
const result = matchDomainPattern('https://www.x.com', 'x.com')
expect(result).toBe(true)
})
})
describe('non-matching domains', () => {
it('should not match different domain', () => {
const result = matchDomainPattern('https://example.com', 'x.com')
expect(result).toBe(false)
})
})
})
Other examples include testing the conflict detection logic in conflict-merge.test.ts.
Sources: src/utils/tests/url.test.ts, src/utils/google-drive/tests/conflict-merge.test.ts
Component Tests
Component tests validate the behavior and rendering of React components. The project uses React Testing Library within a jsdom environment to render components, simulate user interactions, and assert on the resulting DOM state.
Example: SelectionToolbar
The tests for the SelectionToolbar component are a prime example. They verify complex UI logic, such as when the toolbar should appear or hide based on user text selection and mouse events.
Key testing techniques include:
- Mocking: Child components (
AiButton,TranslateButton) and Jotai atoms are mocked to isolate theSelectionToolbar. - Event Simulation: Browser events like
mouseupandselectionchangeare dispatched programmatically. - DOM Assertions: The test checks for the presence or absence of the toolbar in the DOM.
// src/entrypoints/selection.content/selection-toolbar/__tests__/selection-toolbar.test.tsx:90
it('should show toolbar when selecting text in a normal div element', async () => {
render(
<div>
<SelectionToolbar />
<div data-testid="test-element">{MOCK_SELECTED_TEXT}</div>
</div>,
)
await triggerMouseUpWithSelection(screen.getByTestId('test-element'))
await waitFor(expectToolbarVisible)
})
it('should not show toolbar when no text is selected', async () => {
setMockSelectionText('')
// ...
await triggerMouseUpWithSelection(screen.getByTestId('test-element'))
expectToolbarHidden()
})
These tests ensure that the toolbar's visibility logic correctly handles various user interactions and contexts, such as selections within standard elements versus input fields.
Sources: src/entrypoints/selection.content/selection-toolbar/tests/selection-toolbar.test.tsx
Integration Tests
Integration tests verify that multiple modules or components work together correctly. They often involve mocking external dependencies like APIs, storage, or authentication to focus on the interaction logic between internal modules.
Example: Google Drive syncConfig
The sync.test.ts file provides extensive integration tests for the Google Drive configuration synchronization feature. It tests the syncConfig function's behavior under various conditions.
The test setup uses vi.hoisted and vi.mock to replace external services with mock implementations.
This diagram illustrates the flow of an integration test for the sync feature, showing how the syncConfig function interacts with mocked dependencies. The tests cover scenarios like first-time sync, remote-newer, local-newer, and conflict resolution.
Sources: src/utils/google-drive/tests/sync.test.ts
Configuration Migration Tests
A dedicated, automated testing system exists to validate configuration schema migrations. This ensures that user configurations can be smoothly upgraded between extension versions without data loss or corruption.
The system is described in src/utils/config/__tests__/migration-scripts/README.md and implemented in all-migrations.test.ts.
Automated Test Flow
This diagram shows how the migration test runner automatically processes all versions.
Key Features
- Automatic Discovery: The main test file
all-migrations.test.tsautomatically discovers all versions fromv2to the currentLATEST_SCHEMA_VERSION. - Dynamic Loading: It dynamically imports test data from
example/v{version}.tsfor both the "from" and "to" versions. - Multi-Series Testing: Each version's test file can export a
testSeriesobject, allowing for multiple scenarios (e.g., default case, edge cases) to be tested for a single migration. This provides comprehensive coverage for different user configurations. - Validation: The system executes the migration script and asserts that the result is identical to the expected configuration defined in the next version's example file.
// src/utils/config/__tests__/migration-scripts/all-migrations.test.ts:31
for (let version = 2; version <= LATEST_SCHEMA_VERSION; version++) {
// ...
it(`should migrate config from v${fromVersion} to v${toVersion}`, async () => {
// ...
const oldConfigModule = await import(`../example/v${fromVersionStr}.ts`);
const newConfigModule = await import(`../example/v${toVersionStr}.ts`);
for (const [seriesId, oldSeriesData] of Object.entries(oldConfigModule.testSeries)) {
const newSeriesData = newConfigModule.testSeries[seriesId];
if (newSeriesData) {
const actualNewConfig = await runMigration(toVersion, oldSeriesData.config);
expect(actualNewConfig).toEqual(newSeriesData.config);
}
}
});
}
This automated system ensures that all historical and future configuration migrations are robust and backward-compatible.
Sources: src/utils/config/tests/migration-scripts/README.md, src/utils/config/tests/migration-scripts/all-migrations.test.ts