136 lines
3.5 KiB
TypeScript
136 lines
3.5 KiB
TypeScript
/**
|
|
* Bun Test Setup File for Stock Bot Trading Platform
|
|
*
|
|
* Global test configuration and utilities available across all tests.
|
|
* This file is executed before each test via bunfig.toml preload.
|
|
*/
|
|
|
|
// Increase test timeout if needed (already configured in bunfig.toml)
|
|
// Bun.timeout = 30000;
|
|
|
|
// Store original console methods to allow restoration
|
|
const originalConsole = global.console;
|
|
|
|
// Mock console methods to reduce noise during tests
|
|
// These can be restored with testHelpers.restoreConsole()
|
|
console.log = () => {};
|
|
console.debug = () => {};
|
|
console.info = () => {};
|
|
console.warn = () => {};
|
|
console.error = () => {};
|
|
|
|
// 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 = 'AAPL', overrides = {}) => ({
|
|
symbol,
|
|
open: 150.0,
|
|
high: 153.0,
|
|
low: 149.0,
|
|
close: 152.5,
|
|
volume: 10000,
|
|
timestamp: global.testHelpers.mockTimestamp(),
|
|
...overrides
|
|
}),
|
|
|
|
/**
|
|
* Generate test trade data
|
|
*/
|
|
generateTestTrade: (symbol = 'AAPL', overrides = {}) => ({
|
|
symbol,
|
|
price: 152.5,
|
|
size: 100,
|
|
timestamp: global.testHelpers.mockTimestamp(),
|
|
exchange: 'NASDAQ',
|
|
conditions: ['@', 'T'],
|
|
...overrides
|
|
}),
|
|
|
|
/**
|
|
* Generate test quote data
|
|
*/
|
|
generateTestQuote: (symbol = 'AAPL', overrides = {}) => ({
|
|
symbol,
|
|
bidPrice: 152.45,
|
|
bidSize: 200,
|
|
askPrice: 152.55,
|
|
askSize: 150,
|
|
timestamp: global.testHelpers.mockTimestamp(),
|
|
...overrides
|
|
}),
|
|
|
|
/**
|
|
* Create a mock logger
|
|
*/
|
|
mockLogger: () => ({
|
|
debug: () => {},
|
|
info: () => {},
|
|
warn: () => {},
|
|
error: () => {},
|
|
critical: () => {},
|
|
}),
|
|
|
|
/**
|
|
* Restore console methods
|
|
*/
|
|
restoreConsole: () => {
|
|
global.console = originalConsole;
|
|
}
|
|
};
|
|
|
|
// Set up spyOn utilities
|
|
// Similar to jest.spyOn but using Bun's built-in spy functionality
|
|
// This makes it easier to migrate from Jest
|
|
global.spyOn = function(object: any, method: string) {
|
|
const original = object[method];
|
|
const mock = function(...args: any[]) {
|
|
mock.mock.calls.push(args);
|
|
return mock.mockImplementation ? mock.mockImplementation(...args) : original.apply(object, args);
|
|
};
|
|
|
|
mock.mock = { calls: [] };
|
|
mock.mockClear = () => { mock.mock.calls = []; return mock; };
|
|
mock.mockReset = () => {
|
|
mock.mock.calls = [];
|
|
mock.mockImplementation = null;
|
|
return mock;
|
|
};
|
|
mock.mockImplementation = null;
|
|
mock.mockReturnValue = (value: any) => {
|
|
mock.mockImplementation = () => value;
|
|
return mock;
|
|
};
|
|
mock.mockResolvedValue = (value: any) => {
|
|
return mock.mockReturnValue(Promise.resolve(value));
|
|
};
|
|
mock.mockRejectedValue = (value: any) => {
|
|
return mock.mockReturnValue(Promise.reject(value));
|
|
};
|
|
|
|
object[method] = mock;
|
|
return mock;
|
|
};
|