Testing
Rut.ts has comprehensive test coverage to ensure reliability.
Test Suite
The library is tested using JestΒ with 166 test cases covering all functions and edge cases.
npm testCoverage Statistics
- 8 Test suites (one per main function)
- 166 Test cases (comprehensive coverage)
- 100% Pass rate β
Whatβs Tested
β Valid RUT Cases
- Different formats (with/without dots, hyphens)
- K verifier (uppercase and lowercase)
- Leading zeros
- 8 and 9 character RUTs
- All verifier digits (0-9, K)
β Invalid RUT Cases
- Wrong verifier digits
- Too short/long RUTs
- Invalid characters
- K not at the end
- Empty strings
- Non-string inputs
β Edge Cases
- Minimum valid RUT (10.000.002-4)
- Maximum valid RUT (99.999.999-9)
- Verifier digit 0
- Multiple K letters
- Special characters
- Whitespace handling
- Parentheses
β Safe Mode
- All functions tested with
throwOnError: false - Null return validation
- Combined options testing
β Incremental Formatting
- Progressive formatting at each length
- Hyphen appearance at 8+ chars
- Leading zeros in incremental mode
- Very long inputs
- Empty inputs
β Strict Mode
- Suspicious pattern detection
- Normal RUTs pass strict mode
- Edge cases with strict mode
Test Organization
Tests are organized by function:
tests/
βββ validate.test.ts (25 tests)
βββ format.test.ts (30 tests)
βββ clean.test.ts (22 tests)
βββ getVerifier.test.ts (22 tests)
βββ calculateVerifier.test.ts (19 tests)
βββ decompose.test.ts (18 tests)
βββ getBody.test.ts (18 tests)
βββ generate.test.ts (12 tests)Each test file includes:
describeblocks for organization- Happy path tests
- Error case tests
- Edge case tests
- Safe mode tests
Running Tests
# Run all tests
npm test
# Run specific test file
npm test validate
# Run in watch mode
npm test -- --watch
# Run with coverage
npm test -- --coverageExample Test Cases
validate() Tests
describe('validate', () => {
test('validates correct RUTs', () => {
expect(validate('12.345.678-5')).toBe(true)
expect(validate('18.972.631-7')).toBe(true)
})
test('rejects invalid RUTs', () => {
expect(validate('12.345.678-0')).toBe(false)
expect(validate('invalid')).toBe(false)
})
test('strict mode rejects suspicious patterns', () => {
expect(validate('11.111.111-1', { strict: true })).toBe(false)
})
})format() Tests
describe('format', () => {
test('formats with dots', () => {
expect(format('123456785')).toBe('12.345.678-5')
})
test('formats without dots', () => {
expect(format('123456785', { dots: false })).toBe('12345678-5')
})
test('incremental formatting', () => {
expect(format('1234', { incremental: true })).toBe('1.234')
expect(format('12345678', { incremental: true })).toBe('1.234.567-8')
})
})Safe Mode Tests
describe('safe mode', () => {
test('returns null instead of throwing', () => {
expect(clean('invalid', { throwOnError: false })).toBeNull()
expect(format('123', { throwOnError: false })).toBeNull()
expect(decompose('abc', { throwOnError: false })).toBeNull()
})
})Contributing Tests
When contributing, please:
- Add tests for new features
- Ensure all tests pass
- Maintain test organization with
describeblocks - Test both happy paths and error cases
- Include edge cases
- Test safe mode behavior
See Contributing for more details.
Last updated on