simple-unit.test.tsā¢3.45 kB
import { describe, it, expect } from '@jest/globals';
// Simple unit tests to verify testing infrastructure
describe('Testing Infrastructure', () => {
it('should run basic Jest test', () => {
expect(1 + 1).toBe(2);
});
it('should handle async operations', async () => {
const result = await Promise.resolve(42);
expect(result).toBe(42);
});
it('should work with mocks', () => {
const mockFn = jest.fn().mockReturnValue('mocked');
const result = mockFn();
expect(result).toBe('mocked');
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
// Test utilities and types
describe('Type System', () => {
it('should handle string operations', () => {
const testString = 'Hello World';
expect(testString.includes('World')).toBe(true);
expect(testString.toLowerCase()).toBe('hello world');
});
it('should handle array operations', () => {
const testArray = [1, 2, 3, 4, 5];
expect(testArray.length).toBe(5);
expect(testArray.filter(x => x > 3)).toEqual([4, 5]);
});
it('should handle object operations', () => {
const testObj = { name: 'test', value: 123 };
expect(Object.keys(testObj)).toEqual(['name', 'value']);
expect(testObj.name).toBe('test');
});
});
// Error handling tests
describe('Error Handling', () => {
it('should catch and handle errors', () => {
expect(() => {
throw new Error('Test error');
}).toThrow('Test error');
});
it('should handle async errors', async () => {
await expect(async () => {
throw new Error('Async error');
}).rejects.toThrow('Async error');
});
});
// JSON processing tests
describe('JSON Processing', () => {
it('should parse valid JSON', () => {
const jsonString = '{"name": "test", "value": 123}';
const parsed = JSON.parse(jsonString);
expect(parsed.name).toBe('test');
expect(parsed.value).toBe(123);
});
it('should handle invalid JSON gracefully', () => {
expect(() => {
JSON.parse('invalid json');
}).toThrow();
});
it('should stringify objects', () => {
const obj = { test: true, count: 42 };
const json = JSON.stringify(obj);
expect(json).toBe('{"test":true,"count":42}');
});
});
// Schema validation simulation
describe('Schema Validation', () => {
const validateSchema = (data: any, schema: any): boolean => {
if (schema.type === 'object') {
if (typeof data !== 'object' || data === null) return false;
if (schema.required) {
for (const field of schema.required) {
if (!(field in data)) return false;
}
}
return true;
}
return typeof data === schema.type;
};
it('should validate object schema', () => {
const data = { name: 'John', age: 30 };
const schema = {
type: 'object',
required: ['name', 'age']
};
expect(validateSchema(data, schema)).toBe(true);
});
it('should reject invalid object', () => {
const data = { name: 'John' }; // missing 'age'
const schema = {
type: 'object',
required: ['name', 'age']
};
expect(validateSchema(data, schema)).toBe(false);
});
it('should validate primitive types', () => {
expect(validateSchema('test', { type: 'string' })).toBe(true);
expect(validateSchema(123, { type: 'number' })).toBe(true);
expect(validateSchema(true, { type: 'boolean' })).toBe(true);
expect(validateSchema('test', { type: 'number' })).toBe(false);
});
});