view data tests
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 5m51s
Contract Testing / contract-snapshot (pull_request) Has been skipped

This commit is contained in:
2026-01-22 17:28:09 +01:00
parent a165ac9b65
commit 0a37454171
22 changed files with 5534 additions and 16 deletions

View File

@@ -0,0 +1,221 @@
/**
* Domain Error Tests: IdentityDomainError
*
* Tests for domain error classes and their behavior
*/
import { describe, it, expect } from 'vitest';
import { IdentityDomainError, IdentityDomainValidationError, IdentityDomainInvariantError } from './IdentityDomainError';
describe('IdentityDomainError', () => {
describe('IdentityDomainError (base class)', () => {
it('should create an error with correct properties', () => {
const error = new IdentityDomainValidationError('Test error message');
expect(error.message).toBe('Test error message');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
expect(error.kind).toBe('validation');
});
it('should be an instance of Error', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error instanceof Error).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should have correct stack trace', () => {
const error = new IdentityDomainValidationError('Test error');
expect(error.stack).toBeDefined();
expect(error.stack).toContain('IdentityDomainError');
});
it('should handle empty error message', () => {
const error = new IdentityDomainValidationError('');
expect(error.message).toBe('');
});
it('should handle error message with special characters', () => {
const error = new IdentityDomainValidationError('Error: Invalid input @#$%^&*()');
expect(error.message).toBe('Error: Invalid input @#$%^&*()');
});
it('should handle error message with newlines', () => {
const error = new IdentityDomainValidationError('Error line 1\nError line 2');
expect(error.message).toBe('Error line 1\nError line 2');
});
});
describe('IdentityDomainValidationError', () => {
it('should create a validation error with correct kind', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error.kind).toBe('validation');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
});
it('should be an instance of IdentityDomainValidationError', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error instanceof IdentityDomainValidationError).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Invalid email format');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should handle validation error with empty message', () => {
const error = new IdentityDomainValidationError('');
expect(error.kind).toBe('validation');
expect(error.message).toBe('');
});
it('should handle validation error with complex message', () => {
const error = new IdentityDomainValidationError(
'Validation failed: Email must be at least 6 characters long and contain a valid domain'
);
expect(error.kind).toBe('validation');
expect(error.message).toBe(
'Validation failed: Email must be at least 6 characters long and contain a valid domain'
);
});
});
describe('IdentityDomainInvariantError', () => {
it('should create an invariant error with correct kind', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error.kind).toBe('invariant');
expect(error.type).toBe('domain');
expect(error.context).toBe('identity-domain');
});
it('should be an instance of IdentityDomainInvariantError', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error instanceof IdentityDomainInvariantError).toBe(true);
});
it('should be an instance of IdentityDomainError', () => {
const error = new IdentityDomainInvariantError('User must have a valid email');
expect(error instanceof IdentityDomainError).toBe(true);
});
it('should handle invariant error with empty message', () => {
const error = new IdentityDomainInvariantError('');
expect(error.kind).toBe('invariant');
expect(error.message).toBe('');
});
it('should handle invariant error with complex message', () => {
const error = new IdentityDomainInvariantError(
'Invariant violation: User rating must be between 0 and 100'
);
expect(error.kind).toBe('invariant');
expect(error.message).toBe(
'Invariant violation: User rating must be between 0 and 100'
);
});
});
describe('Error hierarchy', () => {
it('should maintain correct error hierarchy for validation errors', () => {
const error = new IdentityDomainValidationError('Test');
expect(error instanceof IdentityDomainValidationError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should maintain correct error hierarchy for invariant errors', () => {
const error = new IdentityDomainInvariantError('Test');
expect(error instanceof IdentityDomainInvariantError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should allow catching as IdentityDomainError', () => {
const error = new IdentityDomainValidationError('Test');
try {
throw error;
} catch (e) {
expect(e instanceof IdentityDomainError).toBe(true);
expect((e as IdentityDomainError).kind).toBe('validation');
}
});
it('should allow catching as Error', () => {
const error = new IdentityDomainInvariantError('Test');
try {
throw error;
} catch (e) {
expect(e instanceof Error).toBe(true);
expect((e as Error).message).toBe('Test');
}
});
});
describe('Error properties', () => {
it('should have consistent type property', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.type).toBe('domain');
expect(invariantError.type).toBe('domain');
});
it('should have consistent context property', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.context).toBe('identity-domain');
expect(invariantError.context).toBe('identity-domain');
});
it('should have different kind properties', () => {
const validationError = new IdentityDomainValidationError('Test');
const invariantError = new IdentityDomainInvariantError('Test');
expect(validationError.kind).toBe('validation');
expect(invariantError.kind).toBe('invariant');
});
});
describe('Error usage patterns', () => {
it('should be usable in try-catch blocks', () => {
expect(() => {
throw new IdentityDomainValidationError('Invalid input');
}).toThrow(IdentityDomainValidationError);
});
it('should be usable with error instanceof checks', () => {
const error = new IdentityDomainValidationError('Test');
expect(error instanceof IdentityDomainValidationError).toBe(true);
expect(error instanceof IdentityDomainError).toBe(true);
expect(error instanceof Error).toBe(true);
});
it('should be usable with error type narrowing', () => {
const error: IdentityDomainError = new IdentityDomainValidationError('Test');
if (error.kind === 'validation') {
expect(error instanceof IdentityDomainValidationError).toBe(true);
}
});
it('should support error message extraction', () => {
const errorMessage = 'User email is required';
const error = new IdentityDomainValidationError(errorMessage);
expect(error.message).toBe(errorMessage);
});
});
});