added initial integration tests with bun
This commit is contained in:
parent
3e451558ac
commit
fb22815450
52 changed files with 7588 additions and 364 deletions
159
jest.setup.ts
Normal file
159
jest.setup.ts
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
/**
|
||||
* Jest Setup File for Stock Bot Trading Platform
|
||||
*
|
||||
* Global test configuration and utilities available across all tests.
|
||||
* This file is executed before each test file runs.
|
||||
*/
|
||||
|
||||
import 'jest-extended';
|
||||
|
||||
// Increase test timeout for integration tests
|
||||
jest.setTimeout(30000);
|
||||
|
||||
// Mock console methods to reduce noise during tests
|
||||
// but allow them to be restored if needed
|
||||
const originalConsole = global.console;
|
||||
|
||||
global.console = {
|
||||
...originalConsole,
|
||||
log: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
};
|
||||
|
||||
// Global test utilities available in all test files
|
||||
declare global {
|
||||
var testHelpers: {
|
||||
sleep: (ms: number) => Promise<void>;
|
||||
mockTimestamp: () => Date;
|
||||
generateTestOHLCV: (symbol?: string, overrides?: any) => any;
|
||||
generateTestTrade: (symbol?: string, overrides?: any) => any;
|
||||
generateTestQuote: (symbol?: string, overrides?: any) => any;
|
||||
mockLogger: () => any;
|
||||
restoreConsole: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
global.testHelpers = {
|
||||
/**
|
||||
* Sleep utility for async tests
|
||||
*/
|
||||
sleep: (ms: number) => new Promise(resolve => setTimeout(resolve, ms)),
|
||||
|
||||
/**
|
||||
* Consistent mock timestamp for tests
|
||||
*/
|
||||
mockTimestamp: () => new Date('2024-01-01T12:00:00Z'),
|
||||
|
||||
/**
|
||||
* Generate test OHLCV data
|
||||
*/
|
||||
generateTestOHLCV: (symbol: string = 'AAPL', overrides: any = {}) => ({
|
||||
symbol,
|
||||
timestamp: new Date('2024-01-01T12:00:00Z'),
|
||||
open: 150.00,
|
||||
high: 152.00,
|
||||
low: 149.50,
|
||||
close: 151.50,
|
||||
volume: 1000000,
|
||||
source: 'test',
|
||||
...overrides
|
||||
}),
|
||||
|
||||
/**
|
||||
* Generate test trade data
|
||||
*/
|
||||
generateTestTrade: (symbol: string = 'AAPL', overrides: any = {}) => ({
|
||||
symbol,
|
||||
timestamp: new Date('2024-01-01T12:00:00Z'),
|
||||
price: 151.50,
|
||||
quantity: 100,
|
||||
side: 'buy',
|
||||
trade_id: 'test_trade_1',
|
||||
source: 'test',
|
||||
...overrides
|
||||
}),
|
||||
|
||||
/**
|
||||
* Generate test quote data
|
||||
*/
|
||||
generateTestQuote: (symbol: string = 'AAPL', overrides: any = {}) => ({
|
||||
symbol,
|
||||
timestamp: new Date('2024-01-01T12:00:00Z'),
|
||||
bid_price: 151.49,
|
||||
ask_price: 151.51,
|
||||
bid_size: 100,
|
||||
ask_size: 200,
|
||||
source: 'test',
|
||||
...overrides
|
||||
}),
|
||||
|
||||
/**
|
||||
* Create a mock logger
|
||||
*/
|
||||
mockLogger: () => ({
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
trace: jest.fn()
|
||||
}),
|
||||
|
||||
/**
|
||||
* Restore original console methods
|
||||
*/
|
||||
restoreConsole: () => {
|
||||
global.console = originalConsole;
|
||||
}
|
||||
};
|
||||
|
||||
// Environment setup for tests
|
||||
process.env.NODE_ENV = 'test';
|
||||
process.env.LOG_LEVEL = 'error';
|
||||
|
||||
// Set default test environment variables
|
||||
process.env.QUESTDB_HOST = process.env.QUESTDB_HOST || 'localhost';
|
||||
process.env.QUESTDB_HTTP_PORT = process.env.QUESTDB_HTTP_PORT || '9000';
|
||||
process.env.QUESTDB_PG_PORT = process.env.QUESTDB_PG_PORT || '8812';
|
||||
process.env.QUESTDB_INFLUX_PORT = process.env.QUESTDB_INFLUX_PORT || '9009';
|
||||
|
||||
process.env.POSTGRES_HOST = process.env.POSTGRES_HOST || 'localhost';
|
||||
process.env.POSTGRES_PORT = process.env.POSTGRES_PORT || '5432';
|
||||
process.env.POSTGRES_DB = process.env.POSTGRES_DB || 'trading_bot_test';
|
||||
process.env.POSTGRES_USER = process.env.POSTGRES_USER || 'trading_admin';
|
||||
process.env.POSTGRES_PASSWORD = process.env.POSTGRES_PASSWORD || 'trading_pass_test';
|
||||
|
||||
process.env.MONGODB_HOST = process.env.MONGODB_HOST || 'localhost';
|
||||
process.env.MONGODB_PORT = process.env.MONGODB_PORT || '27017';
|
||||
process.env.MONGODB_DATABASE = process.env.MONGODB_DATABASE || 'trading_bot_test';
|
||||
process.env.MONGODB_USERNAME = process.env.MONGODB_USERNAME || 'trading_admin';
|
||||
process.env.MONGODB_PASSWORD = process.env.MONGODB_PASSWORD || 'trading_mongo_test';
|
||||
|
||||
// Mock Date.now() for consistent test results
|
||||
const mockNow = new Date('2024-01-01T12:00:00Z').getTime();
|
||||
jest.spyOn(Date, 'now').mockReturnValue(mockNow);
|
||||
|
||||
// Global test cleanup
|
||||
beforeEach(() => {
|
||||
// Clear all mocks before each test
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Reset any module mocks after each test
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
// Handle unhandled promise rejections in tests
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
throw reason;
|
||||
});
|
||||
|
||||
// Handle uncaught exceptions in tests
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught Exception:', error);
|
||||
throw error;
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue