Update project configuration and testing infrastructure
- Refactored Jest configuration for improved ESM and TypeScript support - Updated `jest.setup.ts` with comprehensive test environment configuration - Enhanced mocking for WebSocket, console, and external dependencies - Adjusted package.json dependencies and scripts - Updated tsconfig.json with decorator and test exclusion settings - Improved test coverage configuration and reporting - Simplified test file structure and mocking strategies
This commit is contained in:
@@ -1,15 +1,84 @@
|
||||
import { config } from 'dotenv';
|
||||
import { resolve } from 'path';
|
||||
import { jest } from '@jest/globals';
|
||||
import dotenv from 'dotenv';
|
||||
import { TextEncoder, TextDecoder } from 'util';
|
||||
|
||||
// Load test environment variables
|
||||
const envFile = process.env.NODE_ENV === 'test' ? '.env.test' : '.env';
|
||||
config({ path: resolve(__dirname, envFile) });
|
||||
dotenv.config({ path: '.env.test' });
|
||||
|
||||
// Set default test environment variables if not provided
|
||||
process.env.TEST_HASS_HOST = process.env.TEST_HASS_HOST || 'http://localhost:8123';
|
||||
process.env.TEST_HASS_TOKEN = process.env.TEST_HASS_TOKEN || 'test_token';
|
||||
process.env.TEST_HASS_SOCKET_URL = process.env.TEST_HASS_SOCKET_URL || 'ws://localhost:8123/api/websocket';
|
||||
process.env.TEST_PORT = process.env.TEST_PORT || '3001';
|
||||
// Set test environment
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.HASS_URL = 'http://localhost:8123';
|
||||
process.env.HASS_TOKEN = 'test_token';
|
||||
process.env.CLAUDE_API_KEY = 'test_api_key';
|
||||
process.env.CLAUDE_MODEL = 'test_model';
|
||||
|
||||
// Ensure test environment
|
||||
process.env.NODE_ENV = 'test';
|
||||
// Add TextEncoder and TextDecoder to global scope
|
||||
Object.defineProperty(global, 'TextEncoder', {
|
||||
value: TextEncoder,
|
||||
writable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(global, 'TextDecoder', {
|
||||
value: TextDecoder,
|
||||
writable: true
|
||||
});
|
||||
|
||||
// Configure console for tests
|
||||
const originalConsole = { ...console };
|
||||
global.console = {
|
||||
...console,
|
||||
log: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
info: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
};
|
||||
|
||||
// Increase test timeout
|
||||
jest.setTimeout(30000);
|
||||
|
||||
// Mock WebSocket
|
||||
jest.mock('ws', () => {
|
||||
return {
|
||||
WebSocket: jest.fn().mockImplementation(() => ({
|
||||
on: jest.fn(),
|
||||
send: jest.fn(),
|
||||
close: jest.fn(),
|
||||
removeAllListeners: jest.fn()
|
||||
}))
|
||||
};
|
||||
});
|
||||
|
||||
// Mock chalk
|
||||
jest.mock('chalk', () => ({
|
||||
default: {
|
||||
red: (text: string) => text,
|
||||
green: (text: string) => text,
|
||||
yellow: (text: string) => text,
|
||||
blue: (text: string) => text,
|
||||
magenta: (text: string) => text,
|
||||
cyan: (text: string) => text,
|
||||
white: (text: string) => text,
|
||||
gray: (text: string) => text,
|
||||
grey: (text: string) => text,
|
||||
black: (text: string) => text,
|
||||
bold: (text: string) => text,
|
||||
dim: (text: string) => text,
|
||||
italic: (text: string) => text,
|
||||
underline: (text: string) => text,
|
||||
inverse: (text: string) => text,
|
||||
hidden: (text: string) => text,
|
||||
strikethrough: (text: string) => text,
|
||||
visible: (text: string) => text,
|
||||
}
|
||||
}));
|
||||
|
||||
// Reset mocks between tests
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
// Cleanup after tests
|
||||
afterEach(() => {
|
||||
jest.clearAllTimers();
|
||||
});
|
||||
Reference in New Issue
Block a user