/**
* Tests for custom error classes
*
* This is a beginner-friendly test file that shows the basics:
* - How to import what we're testing
* - How to write test cases
* - How to check expected behavior
*/
import { describe, it, expect } from 'vitest';
import { ConfigurationError, ValidationError, ErrorType } from '../../../src/utils/errors.js';
/**
* Test suite for ConfigurationError
*
* A "describe" block groups related tests together
*/
describe('ConfigurationError', () => {
/**
* Individual test case
*
* "it" describes what the test checks
* The function contains the actual test logic
*/
it('should create an error with correct message', () => {
// Arrange: Set up the test data
const message = 'ATTIO_API_KEY not found';
// Act: Do the thing we're testing
const error = new ConfigurationError(message);
// Assert: Check that it worked as expected
expect(error.message).toBe(message);
});
it('should have correct name property', () => {
const error = new ConfigurationError('test');
expect(error.name).toBe('ConfigurationError');
});
it('should have correct error type', () => {
const error = new ConfigurationError('test');
expect(error.errorType).toBe(ErrorType.VALIDATION);
});
it('should be an instance of Error', () => {
const error = new ConfigurationError('test');
// This checks that our custom error is still a proper Error object
expect(error instanceof Error).toBe(true);
});
});
/**
* Test suite for ValidationError
*/
describe('ValidationError', () => {
it('should create an error with message', () => {
const message = 'Name is required';
const error = new ValidationError(message);
expect(error.message).toBe(message);
});
it('should store the field name when provided', () => {
const error = new ValidationError('Invalid value', 'email');
expect(error.field).toBe('email');
});
it('should allow field to be undefined', () => {
const error = new ValidationError('Something went wrong');
expect(error.field).toBeUndefined();
});
it('should have correct error type', () => {
const error = new ValidationError('test');
expect(error.errorType).toBe(ErrorType.VALIDATION);
});
});