|
1 | 1 | import { jest, describe, it, expect, beforeEach } from "@jest/globals";
|
2 |
| -import type { Transport } from "@modelcontextprotocol/sdk/shared/transport"; |
| 2 | +import {runServer} from "../../src/index"; |
| 3 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio"; |
3 | 4 |
|
4 |
| -// Mock modules with proper typing |
5 |
| -jest.mock("@modelcontextprotocol/sdk/server/stdio", () => ({ |
6 |
| - StdioServerTransport: jest.fn().mockImplementation(() => ({}) as Transport), |
7 |
| -})); |
8 | 5 |
|
9 |
| -// Properly type the mock function to return Promise<void> |
10 |
| -const mockConnect = jest.fn<() => Promise<void>>().mockResolvedValue(); |
11 |
| -jest.mock("../../src/server", () => ({ |
12 |
| - Server: jest.fn().mockImplementation(() => ({ |
13 |
| - state: undefined, |
14 |
| - apiClient: undefined, |
15 |
| - initialized: false, |
16 |
| - connect: mockConnect, |
17 |
| - })), |
18 |
| -})); |
| 6 | +// mock the StdioServerTransport |
| 7 | +jest.mock('@modelcontextprotocol/sdk/server/stdio.js'); |
| 8 | +// mock Server class and its methods |
| 9 | +jest.mock('../../src/server.ts', () => { |
| 10 | + return { |
| 11 | + Server: jest.fn().mockImplementation(() => { |
| 12 | + return { |
| 13 | + connect: jest.fn().mockImplementation((transport) => { |
| 14 | + return new Promise((resolve) => { |
| 15 | + resolve(transport); |
| 16 | + }); |
| 17 | + }), |
| 18 | + }; |
| 19 | + }), |
| 20 | + }; |
| 21 | +}); |
19 | 22 |
|
20 |
| -describe("Server initialization", () => { |
21 |
| - beforeEach(() => { |
22 |
| - jest.clearAllMocks(); |
23 |
| - jest.resetModules(); |
24 |
| - }); |
25 | 23 |
|
| 24 | +describe("Server initialization", () => { |
26 | 25 | it("should create a server instance", async () => {
|
27 |
| - const { Server } = await import("../../src/server"); |
28 |
| - const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio"); |
29 |
| - |
30 |
| - // Import the module under test |
31 |
| - await import("../../src/index"); |
32 |
| - |
33 |
| - expect(Server).toHaveBeenCalledTimes(1); |
34 |
| - expect(StdioServerTransport).toHaveBeenCalledTimes(1); |
35 |
| - expect(mockConnect).toHaveBeenCalledTimes(1); |
| 26 | + const server = await runServer(); |
| 27 | + expect(StdioServerTransport).toHaveBeenCalled(); |
36 | 28 | });
|
37 | 29 | });
|
0 commit comments