|
| 1 | +# Test ID Operations Example |
| 2 | + |
| 3 | +This example demonstrates how to use the new deterministic test ID functionality. |
| 4 | + |
| 5 | +```typescript |
| 6 | +import { |
| 7 | + setTestId, |
| 8 | + getTestId, |
| 9 | + setTestIdsForReport, |
| 10 | + findTestById, |
| 11 | + generateTestIdFromProperties |
| 12 | +} from 'ctrf' |
| 13 | +import type { Test, Report } from 'ctrf' |
| 14 | + |
| 15 | +// Example test object |
| 16 | +const test: Test = { |
| 17 | + name: 'should authenticate user', |
| 18 | + status: 'passed', |
| 19 | + duration: 150, |
| 20 | + suite: ['auth', 'login'], |
| 21 | + filePath: 'src/auth/login.test.ts' |
| 22 | +} |
| 23 | + |
| 24 | +// Set a test ID (generates deterministic UUID based on properties) |
| 25 | +setTestId(test) |
| 26 | +console.log('Test ID:', test.id) // Always the same UUID for these properties! |
| 27 | + |
| 28 | +// Get a test ID (generates one if not present) |
| 29 | +const testId = getTestId(test) |
| 30 | +console.log('Test ID:', testId) // Same as above |
| 31 | + |
| 32 | +// Generate a test ID from properties - always deterministic! |
| 33 | +const customId = generateTestIdFromProperties( |
| 34 | + 'my test', |
| 35 | + ['suite1', 'suite2'], |
| 36 | + 'my-test.ts' |
| 37 | +) |
| 38 | +console.log('Generated ID:', customId) // Always the same for these inputs |
| 39 | + |
| 40 | +// Demonstrate deterministic behavior |
| 41 | +const sameId = generateTestIdFromProperties( |
| 42 | + 'my test', |
| 43 | + ['suite1', 'suite2'], |
| 44 | + 'my-test.ts' |
| 45 | +) |
| 46 | +console.log('Same ID?', customId === sameId) // true! |
| 47 | + |
| 48 | +// Set IDs for all tests in a report |
| 49 | +const report: Report = { |
| 50 | + reportFormat: 'CTRF', |
| 51 | + specVersion: '1.0.0', |
| 52 | + results: { |
| 53 | + tool: { name: 'vitest' }, |
| 54 | + summary: { |
| 55 | + tests: 2, |
| 56 | + passed: 2, |
| 57 | + failed: 0, |
| 58 | + skipped: 0, |
| 59 | + pending: 0, |
| 60 | + other: 0, |
| 61 | + start: Date.now(), |
| 62 | + stop: Date.now() + 1000 |
| 63 | + }, |
| 64 | + tests: [ |
| 65 | + { |
| 66 | + name: 'test 1', |
| 67 | + status: 'passed', |
| 68 | + duration: 100, |
| 69 | + suite: ['unit'], |
| 70 | + filePath: 'test1.ts' |
| 71 | + }, |
| 72 | + { |
| 73 | + name: 'test 2', |
| 74 | + status: 'passed', |
| 75 | + duration: 200, |
| 76 | + suite: ['integration'], |
| 77 | + filePath: 'test2.ts' |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// Set IDs for all tests |
| 84 | +setTestIdsForReport(report) |
| 85 | + |
| 86 | +// Find a test by its ID |
| 87 | +const foundTest = findTestById(report, report.results.tests[0].id!) |
| 88 | +console.log('Found test:', foundTest?.name) |
| 89 | +``` |
| 90 | + |
| 91 | +## Key Features |
| 92 | + |
| 93 | +1. **Deterministic UUIDs**: Same test properties always generate the same UUID |
| 94 | +2. **Proper UUID format**: Valid UUIDs that follow the standard format |
| 95 | +3. **Non-destructive**: Won't overwrite existing IDs |
| 96 | +4. **Property-based**: Uses test name, suite, and filePath for generation |
| 97 | +5. **Report-level operations**: Can process entire reports at once |
| 98 | +6. **Search functionality**: Find tests by their deterministic IDs |
| 99 | +7. **Consistent across runs**: Same test will always have the same ID |
0 commit comments