Skip to content

Commit 9b801f9

Browse files
committed
add simple test
1 parent 832b4cf commit 9b801f9

File tree

2 files changed

+38
-119
lines changed

2 files changed

+38
-119
lines changed

tests/README.md

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -17,122 +17,3 @@ To run a specific test file or directory:
1717
npm test -- path/to/test/file.test.ts
1818
npm test -- path/to/directory
1919
```
20-
21-
## Writing Tests
22-
23-
### Basic Test Structure
24-
25-
Each test file should follow this basic structure:
26-
27-
```typescript
28-
import { describe, it, expect, jest, beforeEach, afterEach } from '@jest/globals';
29-
import { moduleToTest } from '../path/to/module';
30-
31-
describe('Feature being tested', () => {
32-
beforeEach(() => {
33-
// Setup before each test
34-
jest.clearAllMocks();
35-
});
36-
37-
afterEach(() => {
38-
// Cleanup after each test
39-
});
40-
41-
it('should do something specific', () => {
42-
// Test code
43-
expect(result).toBe(expectedValue);
44-
});
45-
});
46-
```
47-
48-
### Mocking
49-
50-
#### Mocking Modules
51-
52-
To mock an imported module:
53-
54-
```typescript
55-
jest.mock('../path/to/module', () => {
56-
return {
57-
functionName: jest.fn().mockReturnValue('mocked value'),
58-
ClassName: jest.fn().mockImplementation(() => ({
59-
methodName: jest.fn().mockResolvedValue('result'),
60-
})),
61-
};
62-
});
63-
```
64-
65-
#### Mocking HTTP Requests
66-
67-
For testing API calls:
68-
69-
```typescript
70-
// Mock fetch or any HTTP client
71-
global.fetch = jest.fn().mockImplementation(() =>
72-
Promise.resolve({
73-
ok: true,
74-
status: 200,
75-
json: () => Promise.resolve({ data: 'mock data' }),
76-
})
77-
);
78-
```
79-
80-
### Testing Async Code
81-
82-
```typescript
83-
it('should handle async operations', async () => {
84-
const result = await asyncFunction();
85-
expect(result).toEqual(expectedValue);
86-
});
87-
88-
it('should handle rejected promises', async () => {
89-
await expect(failingAsyncFunction()).rejects.toThrow();
90-
});
91-
```
92-
93-
### Testing MongoDB Tools
94-
95-
When testing MongoDB tools, mock the MongoDB client:
96-
97-
```typescript
98-
jest.mock('mongodb', () => {
99-
// Create mock implementation of MongoDB client
100-
});
101-
```
102-
103-
### Testing Atlas API Tools
104-
105-
When testing Atlas API tools, mock the authentication and HTTP calls:
106-
107-
```typescript
108-
// Mock authentication
109-
jest.mock('../../src/common/atlas/auth', () => ({
110-
getAccessToken: jest.fn().mockResolvedValue('mock-token'),
111-
}));
112-
113-
// Mock HTTP responses
114-
global.fetch = jest.fn();
115-
(global.fetch as jest.Mock).mockResolvedValue({
116-
ok: true,
117-
json: async () => ({ /* mock response data */ }),
118-
});
119-
```
120-
121-
## Best Practices
122-
123-
1. **Test one thing per test**: Each test should verify one specific behavior
124-
2. **Use descriptive test names**: Test names should describe what is being tested
125-
3. **Mock external dependencies**: Don't rely on external services in unit tests
126-
4. **Clean up after tests**: Reset state after tests to avoid interference
127-
5. **Test edge cases**: Include tests for error conditions and edge cases
128-
6. **Keep tests independent**: Tests should not depend on each other
129-
130-
## ESM and TypeScript Configuration
131-
132-
The project uses ESM modules with TypeScript. The Jest configuration in `jest.config.js` is set up to handle this properly. Make sure you:
133-
134-
1. Use `import` instead of `require`
135-
2. Import from `@jest/globals` for Jest functions
136-
3. Follow the patterns in the template test
137-
138-
For more information on testing patterns and techniques, refer to the [Jest documentation](https://jestjs.io/docs/getting-started).

tests/unit/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { jest, describe, it, expect, beforeEach } from '@jest/globals';
2+
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport";
3+
4+
// Mock modules
5+
jest.mock('@modelcontextprotocol/sdk/server/stdio', () => ({
6+
StdioServerTransport: jest.fn().mockImplementation(() => ({} as Transport))
7+
}));
8+
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+
}));
19+
20+
describe('Server initialization', () => {
21+
beforeEach(() => {
22+
jest.clearAllMocks();
23+
jest.resetModules();
24+
});
25+
26+
it('should create a server instance', async () => {
27+
// Import these after mocking
28+
const { Server } = await import('../../src/server');
29+
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio');
30+
31+
// Import the module under test
32+
await import('../../src/index');
33+
34+
expect(Server).toHaveBeenCalledTimes(1);
35+
expect(StdioServerTransport).toHaveBeenCalledTimes(1);
36+
expect(mockConnect).toHaveBeenCalledTimes(1);
37+
});
38+
});

0 commit comments

Comments
 (0)