spanwright
Version:
CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation
194 lines • 12.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const errors_1 = require("../errors");
(0, vitest_1.describe)('Errors Module', () => {
let mockConsoleError;
let mockProcessExit;
(0, vitest_1.beforeEach)(() => {
mockConsoleError = vitest_1.vi.spyOn(console, 'error').mockImplementation(() => { });
mockProcessExit = vitest_1.vi.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('process.exit called');
});
});
(0, vitest_1.afterEach)(() => {
vitest_1.vi.restoreAllMocks();
});
(0, vitest_1.describe)('SpanwrightError', () => {
(0, vitest_1.it)('should create error with message and optional code', () => {
const error = new errors_1.SpanwrightError('Test error message', 'TEST_CODE');
(0, vitest_1.expect)(error.message).toBe('Test error message');
(0, vitest_1.expect)(error.code).toBe('TEST_CODE');
(0, vitest_1.expect)(error.name).toBe('SpanwrightError');
(0, vitest_1.expect)(error).toBeInstanceOf(Error);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
});
(0, vitest_1.it)('should create error with message only', () => {
const error = new errors_1.SpanwrightError('Test error message');
(0, vitest_1.expect)(error.message).toBe('Test error message');
(0, vitest_1.expect)(error.code).toBeUndefined();
(0, vitest_1.expect)(error.name).toBe('SpanwrightError');
});
(0, vitest_1.it)('should be catchable as Error', () => {
try {
throw new errors_1.SpanwrightError('Test error');
}
catch (error) {
(0, vitest_1.expect)(error).toBeInstanceOf(Error);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
}
});
});
(0, vitest_1.describe)('ValidationError', () => {
(0, vitest_1.it)('should create validation error with message and field', () => {
const error = new errors_1.ValidationError('Invalid input', 'username');
(0, vitest_1.expect)(error.message).toBe('Invalid input');
(0, vitest_1.expect)(error.field).toBe('username');
(0, vitest_1.expect)(error.code).toBe('VALIDATION_ERROR');
(0, vitest_1.expect)(error.name).toBe('ValidationError');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.ValidationError);
});
(0, vitest_1.it)('should create validation error with message only', () => {
const error = new errors_1.ValidationError('Invalid input');
(0, vitest_1.expect)(error.message).toBe('Invalid input');
(0, vitest_1.expect)(error.field).toBeUndefined();
(0, vitest_1.expect)(error.code).toBe('VALIDATION_ERROR');
(0, vitest_1.expect)(error.name).toBe('ValidationError');
});
(0, vitest_1.it)('should inherit from SpanwrightError', () => {
const error = new errors_1.ValidationError('Test error');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.ValidationError);
});
});
(0, vitest_1.describe)('FileSystemError', () => {
(0, vitest_1.it)('should create filesystem error with message and path', () => {
const error = new errors_1.FileSystemError('File not found', '/path/to/file');
(0, vitest_1.expect)(error.message).toBe('File not found');
(0, vitest_1.expect)(error.path).toBe('/path/to/file');
(0, vitest_1.expect)(error.code).toBe('FILESYSTEM_ERROR');
(0, vitest_1.expect)(error.name).toBe('FileSystemError');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.FileSystemError);
});
(0, vitest_1.it)('should create filesystem error with message only', () => {
const error = new errors_1.FileSystemError('File operation failed');
(0, vitest_1.expect)(error.message).toBe('File operation failed');
(0, vitest_1.expect)(error.path).toBeUndefined();
(0, vitest_1.expect)(error.code).toBe('FILESYSTEM_ERROR');
(0, vitest_1.expect)(error.name).toBe('FileSystemError');
});
(0, vitest_1.it)('should inherit from SpanwrightError', () => {
const error = new errors_1.FileSystemError('Test error');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.FileSystemError);
});
});
(0, vitest_1.describe)('ConfigurationError', () => {
(0, vitest_1.it)('should create configuration error with message and key', () => {
const error = new errors_1.ConfigurationError('Invalid configuration', 'database.host');
(0, vitest_1.expect)(error.message).toBe('Invalid configuration');
(0, vitest_1.expect)(error.key).toBe('database.host');
(0, vitest_1.expect)(error.code).toBe('CONFIGURATION_ERROR');
(0, vitest_1.expect)(error.name).toBe('ConfigurationError');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.ConfigurationError);
});
(0, vitest_1.it)('should create configuration error with message only', () => {
const error = new errors_1.ConfigurationError('Configuration failed');
(0, vitest_1.expect)(error.message).toBe('Configuration failed');
(0, vitest_1.expect)(error.key).toBeUndefined();
(0, vitest_1.expect)(error.code).toBe('CONFIGURATION_ERROR');
(0, vitest_1.expect)(error.name).toBe('ConfigurationError');
});
(0, vitest_1.it)('should inherit from SpanwrightError', () => {
const error = new errors_1.ConfigurationError('Test error');
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.SpanwrightError);
(0, vitest_1.expect)(error).toBeInstanceOf(errors_1.ConfigurationError);
});
});
(0, vitest_1.describe)('handleError', () => {
(0, vitest_1.it)('should handle SpanwrightError', () => {
const error = new errors_1.SpanwrightError('Test spanwright error');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('Test spanwright error');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle ValidationError', () => {
const error = new errors_1.ValidationError('Test validation error', 'field');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('Test validation error');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle FileSystemError', () => {
const error = new errors_1.FileSystemError('Test filesystem error', '/path');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('Test filesystem error');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle ConfigurationError', () => {
const error = new errors_1.ConfigurationError('Test configuration error', 'key');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('Test configuration error');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle generic Error', () => {
const error = new Error('Generic error message');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('❌ An error occurred:', 'Generic error message');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle unknown error types', () => {
const error = 'string error';
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('❌ An unknown error occurred');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle null/undefined errors', () => {
(0, vitest_1.expect)(() => (0, errors_1.handleError)(null)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('❌ An unknown error occurred');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
mockConsoleError.mockClear();
mockProcessExit.mockClear();
(0, vitest_1.expect)(() => (0, errors_1.handleError)(undefined)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('❌ An unknown error occurred');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should handle object errors', () => {
const error = { message: 'Object error', code: 'OBJ_ERROR' };
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('❌ An unknown error occurred');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
(0, vitest_1.it)('should prioritize SpanwrightError handling over generic Error', () => {
const error = new errors_1.ValidationError('Validation failed');
(0, vitest_1.expect)(() => (0, errors_1.handleError)(error)).toThrow('process.exit called');
(0, vitest_1.expect)(mockConsoleError).toHaveBeenCalledWith('Validation failed');
(0, vitest_1.expect)(mockConsoleError).not.toHaveBeenCalledWith(vitest_1.expect.stringContaining('❌ An error occurred:'));
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
});
});
(0, vitest_1.describe)('safeExit', () => {
(0, vitest_1.it)('should exit with default code 0', () => {
(0, vitest_1.expect)(() => (0, errors_1.safeExit)()).toThrow('process.exit called');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(0);
});
(0, vitest_1.it)('should exit with specified code', () => {
(0, vitest_1.expect)(() => (0, errors_1.safeExit)(1)).toThrow('process.exit called');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(1);
mockProcessExit.mockClear();
(0, vitest_1.expect)(() => (0, errors_1.safeExit)(42)).toThrow('process.exit called');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(42);
});
(0, vitest_1.it)('should handle negative exit codes', () => {
(0, vitest_1.expect)(() => (0, errors_1.safeExit)(-1)).toThrow('process.exit called');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(-1);
});
(0, vitest_1.it)('should handle zero exit code explicitly', () => {
(0, vitest_1.expect)(() => (0, errors_1.safeExit)(0)).toThrow('process.exit called');
(0, vitest_1.expect)(mockProcessExit).toHaveBeenCalledWith(0);
});
});
});
//# sourceMappingURL=errors.test.js.map