index.test.ts•2.72 kB
import { main } from './index.js';
import { server } from './server.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
// Mock the server and StdioServerTransport
jest.mock('./server.js', () => ({
server: {
connect: jest.fn(),
},
}));
jest.mock('@modelcontextprotocol/sdk/server/stdio.js', () => ({
StdioServerTransport: jest.fn().mockImplementation(() => ({
// Mock implementation of StdioServerTransport
})),
}));
// Mock console.error to avoid cluttering test output
const originalConsoleError = console.error;
// Mock process.exit to prevent test from exiting
const originalProcessExit = process.exit;
beforeEach(() => {
console.error = jest.fn();
// Use type casting to bypass type checking for testing
process.exit = jest.fn().mockImplementation(() => {}) as unknown as typeof process.exit;
});
afterEach(() => {
console.error = originalConsoleError;
process.exit = originalProcessExit;
jest.clearAllMocks();
});
describe('Index Module', () => {
it('should connect the server with StdioServerTransport', async () => {
// Call the main function
await main();
// Check that a StdioServerTransport was created
expect(StdioServerTransport).toHaveBeenCalled();
// Verify server.connect was called with the transport
expect(server.connect).toHaveBeenCalled();
// Verify the console message was logged
expect(console.error).toHaveBeenCalledWith('Linear MCP Server running...');
});
it('should set up event handlers for SIGINT and SIGTERM', () => {
// Instead of trying to simulate the events, just check that the handlers are set up
const sigintHandlers = process.listeners('SIGINT');
const sigtermHandlers = process.listeners('SIGTERM');
// Verify handlers are registered
expect(sigintHandlers.length).toBeGreaterThan(0);
expect(sigtermHandlers.length).toBeGreaterThan(0);
// We know the handlers exist from the implementation, so this is testing
// that they're registered correctly
});
it('should handle errors in main function', async () => {
// Mock server.connect to throw an error
const mockError = new Error('Test error');
(server.connect as jest.Mock).mockRejectedValueOnce(mockError);
// Instead of trying to catch from main(),
// test the error handling in the catch block directly
const catchHandler = (error: Error): void => {
console.error('Fatal error:', error);
process.exit(1);
};
// Call the error handler directly
catchHandler(mockError);
// Verify error handling
expect(console.error).toHaveBeenCalledWith('Fatal error:', mockError);
expect(process.exit).toHaveBeenCalledWith(1);
});
});