Skip to content

Commit d8dc314

Browse files
committed
test: add BaseTool tests
1 parent 8f34d26 commit d8dc314

File tree

5 files changed

+435
-2
lines changed

5 files changed

+435
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ README
55
.DS_Store
66
dist
77
node_modules
8+
*.log
9+
coverage

jest.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** @type {import('jest').Config} */
2+
export default {
3+
preset: 'ts-jest/presets/default-esm',
4+
testEnvironment: 'node',
5+
extensionsToTreatAsEsm: ['.ts'],
6+
moduleNameMapper: {
7+
'^(\\.{1,2}/.*)\\.js$': '$1',
8+
},
9+
transform: {
10+
'^.+\\.tsx?$': [
11+
'ts-jest',
12+
{
13+
useESM: true,
14+
tsconfig: {
15+
module: 'Node16',
16+
moduleResolution: 'Node16',
17+
},
18+
},
19+
],
20+
},
21+
testMatch: ['**/tests/**/*.test.ts'],
22+
moduleFileExtensions: ['ts', 'js', 'json'],
23+
coverageDirectory: 'coverage',
24+
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts', '!src/cli/**', '!src/index.ts'],
25+
};

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
"lint": "eslint",
2626
"lint:fix": "eslint --fix",
2727
"format": "prettier --write \"src/**/*.ts\"",
28-
"prepare": "npm run build"
29-
"dev:pub": "rm -rf dist && npm run build && yalc publish --push"
28+
"prepare": "npm run build",
29+
"dev:pub": "rm -rf dist && npm run build && yalc publish --push",
30+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest",
31+
"test:watch": "NODE_OPTIONS='--experimental-vm-modules' jest --watch",
32+
"test:coverage": "NODE_OPTIONS='--experimental-vm-modules' jest --coverage"
3033
},
3134
"engines": {
3235
"node": ">=18.19.0"

tests/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# MCP Framework Tests
2+
3+
This directory contains unit tests for the MCP Framework.
4+
5+
## Structure
6+
7+
Tests are organized to mirror the source code structure:
8+
9+
```
10+
tests/
11+
├── tools/
12+
│ └── BaseTool.test.ts
13+
├── resources/
14+
├── prompts/
15+
└── ...
16+
```
17+
18+
## Running Tests
19+
20+
```bash
21+
# Run all tests
22+
npm test
23+
24+
# Run tests in watch mode
25+
npm run test:watch
26+
27+
# Run tests with coverage
28+
npm run test:coverage
29+
30+
# Run specific test file
31+
npm test tests/tools/BaseTool.test.ts
32+
```
33+
34+
## Writing Tests
35+
36+
1. **Naming Convention**: Test files should be named `[ComponentName].test.ts`
37+
2. **Location**: Place test files in a directory structure that mirrors `src/`
38+
3. **Imports**: Use `.js` extensions for local imports (ESM compatibility)
39+
4. **Test Structure**: Use `describe` blocks to group related tests
40+
41+
### Example Test Structure
42+
43+
```typescript
44+
import { describe, it, expect, beforeEach } from '@jest/globals';
45+
import { MyClass } from '../../src/path/to/MyClass.js';
46+
47+
describe('MyClass', () => {
48+
let instance: MyClass;
49+
50+
beforeEach(() => {
51+
instance = new MyClass();
52+
});
53+
54+
describe('methodName', () => {
55+
it('should do something specific', () => {
56+
// Test implementation
57+
});
58+
});
59+
});
60+
```
61+
62+
## Testing Abstract Classes
63+
64+
When testing abstract classes like `BaseTool`, create a concrete implementation:
65+
66+
```typescript
67+
class TestTool extends MCPTool<TestInput> {
68+
// Implement abstract properties and methods
69+
}
70+
```
71+
72+
## Jest Configuration
73+
74+
The Jest configuration (`jest.config.js`) is set up for:
75+
- TypeScript with ES modules
76+
- Node.js environment
77+
- Coverage reporting
78+
- Proper path resolution for `.js` imports
79+
80+
## Coverage
81+
82+
Coverage reports are generated in the `coverage/` directory and include:
83+
- All TypeScript files in `src/`
84+
- Exclusions: CLI tools, type definitions, and index files

0 commit comments

Comments
 (0)