|
| 1 | +# Test Documentation |
| 2 | + |
| 3 | +This document describes the test suite for easy-i18n-cli and how to run tests and generate coverage reports. |
| 4 | + |
| 5 | +## Test Framework |
| 6 | + |
| 7 | +- **Framework**: Mocha |
| 8 | +- **Assertion Library**: Node.js built-in `assert` |
| 9 | +- **Coverage Tool**: nyc (Istanbul) |
| 10 | + |
| 11 | +## Running Tests |
| 12 | + |
| 13 | +### Run all tests |
| 14 | +```bash |
| 15 | +npm test |
| 16 | +``` |
| 17 | + |
| 18 | +### Run tests with coverage report |
| 19 | +```bash |
| 20 | +npm test |
| 21 | +``` |
| 22 | + |
| 23 | +This will automatically generate: |
| 24 | +- Text coverage report in the terminal |
| 25 | +- LCOV report in `coverage/` directory |
| 26 | + |
| 27 | +### Run specific test files |
| 28 | +```bash |
| 29 | +npx mocha test/utils.test.js |
| 30 | +npx mocha test/locale.test.js |
| 31 | +npx mocha test/easy-i18n-cli.test.js |
| 32 | +npx mocha test/bin.test.js |
| 33 | +``` |
| 34 | + |
| 35 | +## Coverage Requirements |
| 36 | + |
| 37 | +The project has achieved **100% code coverage** across all metrics: |
| 38 | + |
| 39 | +- **Statement Coverage**: 100% |
| 40 | +- **Branch Coverage**: 100% |
| 41 | +- **Function Coverage**: 100% |
| 42 | +- **Line Coverage**: 100% |
| 43 | + |
| 44 | +### Coverage by File |
| 45 | + |
| 46 | +| File | Statements | Branches | Functions | Lines | |
| 47 | +|------|------------|----------|-----------|-------| |
| 48 | +| bin/easy-i18n-cli.js | 100% | 100% | 100% | 100% | |
| 49 | +| src/easy-i18n.js | 100% | 100% | 100% | 100% | |
| 50 | +| src/locale.js | 100% | 100% | 100% | 100% | |
| 51 | +| src/utils.js | 100% | 100% | 100% | 100% | |
| 52 | + |
| 53 | +## Test Files |
| 54 | + |
| 55 | +### 1. test/utils.test.js |
| 56 | +Tests for utility functions. |
| 57 | + |
| 58 | +**Test Coverage:** |
| 59 | +- `noCacheRequire()`: Module loading without cache |
| 60 | +- `extractLocaleFromExport()`: Locale data extraction from various export formats |
| 61 | + - Direct exports |
| 62 | + - Default exports |
| 63 | + - Nested object structures |
| 64 | + |
| 65 | +### 2. test/locale.test.js |
| 66 | +Tests for the runtime locale resolution module. |
| 67 | + |
| 68 | +**Test Coverage:** |
| 69 | +- Basic text translation (Chinese/English) |
| 70 | +- Interpolation with string values |
| 71 | +- Multi-scene text handling (e.g., `text#sceneA`) |
| 72 | +- Empty key handling in interpolation |
| 73 | +- Numeric key handling in interpolation |
| 74 | +- Undefined value handling in interpolation |
| 75 | +- Custom output filters |
| 76 | +- Custom transfer filters |
| 77 | +- Default options behavior |
| 78 | +- Missing translation fallback |
| 79 | + |
| 80 | +### 3. test/easy-i18n-cli.test.js |
| 81 | +Comprehensive tests for the main EasyI18n class. |
| 82 | + |
| 83 | +**Test Coverage:** |
| 84 | +- Constructor and initialization |
| 85 | +- Debug and info logging |
| 86 | +- Token extraction from source files |
| 87 | + - Single quotes, double quotes, template literals |
| 88 | + - Custom token names |
| 89 | + - Ignore keys |
| 90 | + - Custom regex patterns |
| 91 | +- File and directory resolution |
| 92 | +- Key sorting |
| 93 | +- Chinese character detection |
| 94 | +- Translation data filtering |
| 95 | +- Data initialization from existing files |
| 96 | +- Post-resolution merging (append mode vs non-append mode) |
| 97 | +- Translation filter execution |
| 98 | +- Output file generation |
| 99 | +- Complete workflow execution (`run()`) |
| 100 | +- Translation checking (`check()`) |
| 101 | +- Combined run and check (`runWithCheck()`) |
| 102 | +- Line offset calculation |
| 103 | +- Error handling for untranslated content |
| 104 | + |
| 105 | +### 4. test/bin.test.js |
| 106 | +Tests for the CLI binary entry point. |
| 107 | + |
| 108 | +**Test Coverage:** |
| 109 | +- `--version` / `-v` flag |
| 110 | +- `--config` / `-c` flag with custom config file |
| 111 | +- `--check` flag with translation validation |
| 112 | +- Default behavior without flags |
| 113 | +- Error handling and output |
| 114 | + |
| 115 | +## Test Structure |
| 116 | + |
| 117 | +Each test file follows this structure: |
| 118 | + |
| 119 | +```javascript |
| 120 | +describe('test/filename.test.js', () => { |
| 121 | + describe('Feature/Function Name', () => { |
| 122 | + beforeEach(() => { |
| 123 | + // Setup before each test |
| 124 | + }); |
| 125 | + |
| 126 | + afterEach(() => { |
| 127 | + // Cleanup after each test |
| 128 | + }); |
| 129 | + |
| 130 | + it('should test specific behavior', () => { |
| 131 | + // Test assertions |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +## Edge Cases and Error Handling |
| 138 | + |
| 139 | +The test suite includes comprehensive edge case testing: |
| 140 | + |
| 141 | +1. **Empty/Invalid Files** |
| 142 | + - Missing dist files |
| 143 | + - Empty source files |
| 144 | + - Invalid module exports |
| 145 | + |
| 146 | +2. **Different File Formats** |
| 147 | + - JavaScript (.js) files |
| 148 | + - TypeScript (.ts) files (with ts-node registration) |
| 149 | + - JSX (.jsx) files |
| 150 | + |
| 151 | +3. **Command Line Arguments** |
| 152 | + - All flag combinations |
| 153 | + - Missing required config |
| 154 | + - Invalid config paths |
| 155 | + |
| 156 | +4. **Error Scenarios** |
| 157 | + - Network/filesystem errors (handled gracefully) |
| 158 | + - Untranslated content detection |
| 159 | + - Invalid regex patterns |
| 160 | + |
| 161 | +## Continuous Integration |
| 162 | + |
| 163 | +The tests are designed to run in CI environments: |
| 164 | + |
| 165 | +```yaml |
| 166 | +# Example CI configuration |
| 167 | +- name: Run tests |
| 168 | + run: npm test |
| 169 | + |
| 170 | +- name: Check coverage |
| 171 | + run: npm test && npx nyc check-coverage --lines 100 --branches 100 --functions 100 --statements 100 |
| 172 | +``` |
| 173 | +
|
| 174 | +## Coverage Thresholds |
| 175 | +
|
| 176 | +To enforce coverage thresholds, you can add to `package.json`: |
| 177 | + |
| 178 | +```json |
| 179 | +{ |
| 180 | + "nyc": { |
| 181 | + "check-coverage": true, |
| 182 | + "lines": 100, |
| 183 | + "statements": 100, |
| 184 | + "functions": 100, |
| 185 | + "branches": 100 |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Test Best Practices |
| 191 | + |
| 192 | +1. **Isolation**: Each test is independent and doesn't rely on other tests |
| 193 | +2. **Cleanup**: Test artifacts are cleaned up after each test |
| 194 | +3. **Descriptive Names**: Test names clearly describe what is being tested |
| 195 | +4. **Coverage**: All code paths, branches, and edge cases are covered |
| 196 | +5. **Maintainability**: Tests are well-organized and easy to understand |
| 197 | + |
| 198 | +## Viewing Coverage Reports |
| 199 | + |
| 200 | +### Terminal Report |
| 201 | +The terminal shows a summary table after running tests. |
| 202 | + |
| 203 | +### HTML Report |
| 204 | +Generate an HTML coverage report: |
| 205 | + |
| 206 | +```bash |
| 207 | +npx nyc report --reporter=html |
| 208 | +``` |
| 209 | + |
| 210 | +Then open `coverage/index.html` in a browser. |
| 211 | + |
| 212 | +### LCOV Report |
| 213 | +The LCOV report (`coverage/lcov.info`) can be used with various tools: |
| 214 | +- Code coverage badges |
| 215 | +- IDE integrations |
| 216 | +- CI/CD pipelines |
| 217 | + |
| 218 | +## Troubleshooting |
| 219 | + |
| 220 | +### Tests Failing |
| 221 | +1. Ensure dependencies are installed: `npm install` |
| 222 | +2. Check Node.js version compatibility (>= 8) |
| 223 | +3. Clear test artifacts: `rm -rf test/fixtures/output/*` |
| 224 | + |
| 225 | +### Coverage Not 100% |
| 226 | +1. Check which files/lines are uncovered in the terminal report |
| 227 | +2. Review the HTML coverage report for details |
| 228 | +3. Add tests for uncovered code paths |
| 229 | + |
| 230 | +## Contributing |
| 231 | + |
| 232 | +When adding new features: |
| 233 | +1. Write tests first (TDD approach recommended) |
| 234 | +2. Ensure all tests pass: `npm test` |
| 235 | +3. Verify 100% coverage is maintained |
| 236 | +4. Update this documentation if adding new test files |
0 commit comments