- Added explanation for logger mocking in tests/setup.ts - Removed test files from coverage collection in jest.config.js - Updated tests/README.md to clarify coverage approach and remove outdated threshold reference Co-authored-by: mindesbunister <32161838+mindesbunister@users.noreply.github.com>
112 lines
3.0 KiB
TypeScript
112 lines
3.0 KiB
TypeScript
/**
|
|
* Jest Global Test Setup
|
|
*
|
|
* Configures the test environment for Position Manager integration tests.
|
|
* Mocks external dependencies to isolate unit testing.
|
|
*/
|
|
|
|
// Extend Jest matchers if needed
|
|
expect.extend({
|
|
toBeWithinRange(received: number, floor: number, ceiling: number) {
|
|
const pass = received >= floor && received <= ceiling
|
|
if (pass) {
|
|
return {
|
|
message: () => `expected ${received} not to be within range ${floor} - ${ceiling}`,
|
|
pass: true,
|
|
}
|
|
} else {
|
|
return {
|
|
message: () => `expected ${received} to be within range ${floor} - ${ceiling}`,
|
|
pass: false,
|
|
}
|
|
}
|
|
},
|
|
})
|
|
|
|
// Declare custom matchers for TypeScript
|
|
declare global {
|
|
namespace jest {
|
|
interface Matchers<R> {
|
|
toBeWithinRange(floor: number, ceiling: number): R
|
|
}
|
|
}
|
|
}
|
|
|
|
// Mock logger to reduce noise during tests
|
|
// Logger is mocked to:
|
|
// 1. Prevent console output during test runs
|
|
// 2. Isolate tests from external I/O operations
|
|
// 3. Enable verification of logging calls if needed
|
|
jest.mock('../lib/utils/logger', () => ({
|
|
logger: {
|
|
log: jest.fn(),
|
|
error: jest.fn(),
|
|
warn: jest.fn(),
|
|
info: jest.fn(),
|
|
},
|
|
}))
|
|
|
|
// Mock Drift service to avoid network calls
|
|
jest.mock('../lib/drift/client', () => ({
|
|
getDriftService: jest.fn(() => ({
|
|
getPosition: jest.fn(),
|
|
getOraclePrice: jest.fn(),
|
|
isInitialized: true,
|
|
})),
|
|
initializeDriftService: jest.fn(),
|
|
}))
|
|
|
|
// Mock Pyth price monitor
|
|
jest.mock('../lib/pyth/price-monitor', () => ({
|
|
getPythPriceMonitor: jest.fn(() => ({
|
|
start: jest.fn(),
|
|
stop: jest.fn(),
|
|
getCachedPrice: jest.fn(),
|
|
getLatestPrice: jest.fn(),
|
|
})),
|
|
}))
|
|
|
|
// Mock database operations
|
|
jest.mock('../lib/database/trades', () => ({
|
|
getOpenTrades: jest.fn(() => Promise.resolve([])),
|
|
updateTradeExit: jest.fn(() => Promise.resolve()),
|
|
updateTradeState: jest.fn(() => Promise.resolve()),
|
|
createTrade: jest.fn(() => Promise.resolve()),
|
|
}))
|
|
|
|
// Mock Telegram notifications
|
|
jest.mock('../lib/notifications/telegram', () => ({
|
|
sendPositionClosedNotification: jest.fn(() => Promise.resolve()),
|
|
sendPositionOpenedNotification: jest.fn(() => Promise.resolve()),
|
|
}))
|
|
|
|
// Mock Drift orders
|
|
jest.mock('../lib/drift/orders', () => ({
|
|
closePosition: jest.fn(() => Promise.resolve({ success: true, realizedPnL: 0 })),
|
|
cancelAllOrders: jest.fn(() => Promise.resolve({ success: true, cancelledCount: 0 })),
|
|
placeExitOrders: jest.fn(() => Promise.resolve({ success: true })),
|
|
}))
|
|
|
|
// Mock market data cache
|
|
jest.mock('../lib/trading/market-data-cache', () => ({
|
|
getMarketDataCache: jest.fn(() => ({
|
|
get: jest.fn(() => null),
|
|
set: jest.fn(),
|
|
})),
|
|
}))
|
|
|
|
// Mock stop hunt tracker
|
|
jest.mock('../lib/trading/stop-hunt-tracker', () => ({
|
|
getStopHuntTracker: jest.fn(() => ({
|
|
recordStopHunt: jest.fn(() => Promise.resolve()),
|
|
updateRevengeOutcome: jest.fn(() => Promise.resolve()),
|
|
})),
|
|
}))
|
|
|
|
// Reset mocks before each test
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
|
|
export {}
|