@@ -137,202 +137,29 @@ This document provides comprehensive guidance for AI agents working on the Copy
137137
138138## Testing Strategy
139139
140+ > ** 📖 For comprehensive testing strategy including coverage goals, test layers (unit/component/integration/E2E), tools, mocking strategy, and implementation roadmap, see [ docs/testing.md] ( docs/testing.md ) **
141+
140142### Running Tests
141143``` bash
142144npm test # Run tests once
143145npm run test:watch # Watch mode
146+ npm run test:coverage # Run with coverage report
144147```
145148
146- ### Test Coverage
147- - Unit tests for utility functions: ` utils/*.test.js `
148- - Uses Vitest with browser API mocking
149- - Mock Chrome APIs using test helpers
150-
151- ### Writing Tests
152- - Place tests alongside source files (e.g., ` link.test.js ` next to ` link.ts ` )
153- - Mock browser APIs: ` browser.tabs.query ` , ` browser.i18n.getMessage `
154- - Focus on utility logic and edge cases
155-
156- ### Areas Needing More Tests
157- - React components (currently no component tests)
158- - Integration tests for full workflows
159- - Context menu handlers
160-
161- ### Test-Driven Development (TDD)
162-
163- ** This project follows Test-Driven Development practices. Always write tests BEFORE implementation.**
164-
165- #### TDD Cycle (Red-Green-Refactor)
166-
167- 1 . ** 🔴 Red - Write a Failing Test**
168- - Before writing any new code, write a test that fails
169- - The test should describe the desired behavior
170- - Run the test to confirm it fails for the right reason
171- - Command: ` npm test ` or ` npm run test:watch `
172-
173- 2 . ** 🟢 Green - Make the Test Pass**
174- - Write the minimum code necessary to make the test pass
175- - Don't worry about perfection yet
176- - Focus on making the test green
177- - Run tests to confirm they pass
178-
179- 3 . ** ♻️ Refactor - Improve the Code**
180- - Clean up the code while keeping tests green
181- - Improve structure, readability, performance
182- - Remove duplication
183- - Run tests after each refactoring step
184-
185- #### TDD Workflow for This Project
186-
187- ** For Utility Functions** (e.g., ` utils/link.ts ` , ` utils/tabs.ts ` ):
188- ``` bash
189- # 1. Write test first
190- vim utils/myfeature.test.js # Write failing test
191-
192- # 2. Run test (should fail)
193- npm test
194-
195- # 3. Implement feature
196- vim utils/myfeature.ts # Write minimal implementation
197-
198- # 4. Run test (should pass)
199- npm test
200-
201- # 5. Refactor if needed
202- vim utils/myfeature.ts # Improve code quality
203-
204- # 6. Verify tests still pass
205- npm test
206- ```
207-
208- ** For React Components** (e.g., popup buttons):
209- ``` bash
210- # 1. Write component test first (if testable)
211- vim entrypoints/popup/components/MyButton.test.tsx
212-
213- # 2. Write minimal component
214- vim entrypoints/popup/components/MyButton.tsx
215-
216- # 3. Iterate until tests pass
217- npm run test:watch # Keep running in watch mode
218- ```
219-
220- ** For Bug Fixes** :
221- ``` bash
222- # 1. Write a test that reproduces the bug (should fail)
223- vim utils/link.test.js # Add test case that fails due to bug
224-
225- # 2. Run test to confirm it fails
226- npm test
227-
228- # 3. Fix the bug
229- vim utils/link.ts # Make the fix
230-
231- # 4. Run test to confirm fix (should pass)
232- npm test
233- ```
234-
235- #### TDD Best Practices for This Project
236-
237- 1 . ** Start with the Test**
238- - Never write production code without a failing test first
239- - The test defines the contract/API of your code
240-
241- 2 . ** Keep Tests Simple and Focused**
242- - One test per behavior
243- - Clear test names describing what is being tested
244- - Use descriptive assertions
245-
246- 3 . ** Test File Location**
247- - Place tests next to source files: ` link.ts ` → ` link.test.js `
248- - Use same directory structure
249-
250- 4 . ** Mock Browser APIs**
251- - Always mock ` browser.tabs ` , ` browser.i18n ` , etc.
252- - Use Vitest's mocking capabilities
253- - See existing tests for examples
254-
255- 5 . ** Watch Mode During Development**
256- - Run ` npm run test:watch ` to get immediate feedback
257- - Tests re-run automatically on file changes
258-
259- 6 . ** Test Coverage Goals**
260- - Aim for high coverage of utility functions
261- - Test edge cases and error conditions
262- - Test i18n message retrieval
263- - Test link formatting edge cases (special characters, etc.)
264-
265- #### Example TDD Session
266-
267- ** Task** : Add a function to remove emoji from link titles
268-
269- ``` javascript
270- // Step 1: Write failing test in utils/link.test.js
271- import { describe , it , expect } from ' vitest' ;
272- import { removeEmoji } from ' ./link' ;
273-
274- describe (' removeEmoji' , () => {
275- it (' should remove emoji from title' , () => {
276- expect (removeEmoji (' Hello 👋 World' )).toBe (' Hello World' );
277- });
278-
279- it (' should handle titles without emoji' , () => {
280- expect (removeEmoji (' Hello World' )).toBe (' Hello World' );
281- });
282- });
283-
284- // Run: npm test
285- // Result: ❌ FAIL - removeEmoji is not defined
286-
287- // Step 2: Write minimal implementation in utils/link.ts
288- export function removeEmoji (title : string ): string {
289- return title .replace (/ [\u {1F600}-\u {1F6FF}] / gu , ' ' );
290- }
291-
292- // Run: npm test
293- // Result: ❌ FAIL - regex doesn't cover all emoji
294-
295- // Step 3: Fix implementation
296- export function removeEmoji (title : string ): string {
297- return title .replace (/ [\u {1F000}-\u {1F9FF}] / gu , ' ' );
298- }
299-
300- // Run: npm test
301- // Result: ✅ PASS
302-
303- // Step 4: Refactor if needed (tests still pass)
304- export function removeEmoji (title : string ): string {
305- // More comprehensive emoji regex
306- return title .replace (
307- / [\u {1F000}-\u {1F9FF}\u {2600}-\u {26FF}\u {2700}-\u {27BF}] / gu ,
308- ' '
309- );
310- }
311-
312- // Run: npm test
313- // Result: ✅ PASS
314- ```
315-
316- #### When TDD Might Be Skipped
317-
318- In rare cases, TDD might be challenging:
319- - Initial UI layout/styling exploration
320- - Chrome extension permissions setup
321- - Manifest configuration changes
149+ ### Key Principles
322150
323- ** However** , even in these cases:
324- - Add tests after implementation
325- - Write tests for any logic extracted from UI
326- - Test integration points
151+ - ** Test-Driven Development (TDD)** : Always write tests BEFORE implementation
152+ - ** 80%+ Coverage Goal** : Target 80% overall, 100% for critical paths
153+ - ** Test Location** : Place tests next to source files (e.g., ` link.test.js ` next to ` link.ts ` )
154+ - ** Browser API Mocking** : Use WxtVitest's ` fakeBrowser ` for all browser APIs
155+ - ** Fast Feedback** : Keep unit/component tests under 10 seconds total
327156
328- #### Benefits of TDD for This Project
157+ ### Current Test Coverage
329158
330- 1 . ** Confidence in Changes** : Tests catch regressions immediately
331- 2 . ** Better Design** : Writing tests first leads to better APIs
332- 3 . ** Documentation** : Tests serve as executable documentation
333- 4 . ** Refactoring Safety** : Can improve code without fear
334- 5 . ** Debugging** : Failing test pinpoints exact issue
335- 6 . ** CI/CD** : Automated testing prevents broken deployments
159+ - ✅ ** Utils layer** : ~ 80% coverage (needs expansion to 100%)
160+ - ❌ ** Component layer** : Not implemented (needs React Testing Library)
161+ - ❌ ** Integration tests** : Not implemented
162+ - ❌ ** E2E tests** : Not implemented (4 key workflows planned)
336163
337164## Build and Development
338165
@@ -563,6 +390,7 @@ npm run dev # Test in browser
563390- ` CLAUDE.md ` (this file) - AI agent development guide
564391- ` docs/architecture.md ` - ** Module dependencies, data flows, and architectural design**
565392- ` docs/development-guideline.md ` - ** Comprehensive development guidelines (TDD, coding standards, workflows)**
393+ - ` docs/testing.md ` - ** Testing strategy (coverage goals, test layers, tools, implementation roadmap)**
566394
567395## Working with WXT
568396
@@ -640,6 +468,7 @@ Based on codebase analysis, here are areas for enhancement:
640468### Project Documentation
641469- ** [ Architecture Documentation] ( docs/architecture.md ) ** - Module dependencies, layered architecture, data flows, design decisions
642470- ** [ Development Guidelines] ( docs/development-guideline.md ) ** - TDD workflow, coding standards, testing, Git workflow, common tasks
471+ - ** [ Testing Strategy] ( docs/testing.md ) ** - Testing goals, coverage targets, test layers (unit/component/integration/E2E), tools, implementation roadmap
643472
644473### External Documentation
645474- [ WXT Documentation] ( https://wxt.dev/ )
@@ -689,6 +518,7 @@ npm run build && npm run zip
689518> ** 📖 Important Resources for AI Agents:**
690519> - ** [ docs/architecture.md] ( docs/architecture.md ) ** - Understand module dependencies before making changes
691520> - ** [ docs/development-guideline.md] ( docs/development-guideline.md ) ** - Follow detailed development workflow and best practices
521+ > - ** [ docs/testing.md] ( docs/testing.md ) ** - Follow comprehensive testing strategy (coverage goals, test layers, tools)
692522
693523### When Making Changes
694524
0 commit comments