豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 1d6f003

Browse files
committed
test: support test a single file
1 parent 0abd95c commit 1d6f003

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

.github/workflows/run-tests.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,8 @@ jobs:
5757
shell: bash
5858
run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
5959

60-
- name: Run tests (node20)
61-
if: ${{ matrix.node == '20' }}
62-
shell: bash
63-
run: npm run test:node20
64-
6560
- name: Run tests
6661
shell: bash
67-
if: ${{ matrix.node != '20' }}
6862
run: npm run test:no-build
6963

7064
# Gating job for branch protection.

GEMINI.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Instructions
22

3-
- use `npm run build` to run tsc and test build.
4-
- use `npm run test` to run tests, run all tests to verify correctness.
5-
- use only scripts from `package.json` to run commands.
3+
- Use only scripts from `package.json` to run commands.
4+
- Use `npm run build` to run tsc and test build.
5+
- Use `npm run test` to build and run tests, run all tests to verify correctness.
6+
- use `npm run test path-to-test.ts` to build and run a single test file, for example, `npm run test tests/McpContext.test.ts`.

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
"docs:generate": "node --experimental-strip-types scripts/generate-docs.ts",
1717
"start": "npm run build && node build/src/index.js",
1818
"start-debug": "DEBUG=mcp:* DEBUG_COLORS=false npm run build && node build/src/index.js",
19-
"test:node20": "node --import ./build/tests/setup.js --test-reporter spec --test-force-exit --test build/tests",
20-
"test:no-build": "node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --experimental-print-required-tla --test-reporter spec --test-force-exit --test \"build/tests/**/*.test.js\"",
2119
"test": "npm run build && npm run test:no-build",
22-
"test:only": "npm run build && npm run test:only:no-build",
23-
"test:only:no-build": "node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-reporter spec --test-force-exit --test --test-only \"build/tests/**/*.test.js\"",
24-
"test:update-snapshots": "npm run build && node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-force-exit --test --test-update-snapshots \"build/tests/**/*.test.js\"",
20+
"test:no-build": "node scripts/test.mjs",
21+
"test:only": "npm run build && node scripts/test.mjs --test-only",
22+
"test:update-snapshots": "npm run build && node scripts/test.mjs --test-update-snapshots",
2523
"prepare": "node --experimental-strip-types scripts/prepare.ts",
2624
"verify-server-json-version": "node --experimental-strip-types scripts/verify-server-json-version.ts"
2725
},

scripts/test.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
import {spawn} from 'node:child_process';
7+
import path from 'node:path';
8+
import process from 'node:process';
9+
10+
const args = process.argv.slice(2);
11+
const userArgs = args.filter(arg => !arg.startsWith('-'));
12+
const flags = args.filter(arg => arg.startsWith('-'));
13+
14+
const files = [];
15+
16+
if (userArgs.length > 0) {
17+
for (const arg of userArgs) {
18+
// Map .ts files to build/ .js files
19+
let testPath = arg;
20+
if (testPath.endsWith('.ts')) {
21+
testPath = testPath.replace(/\.ts$/, '.js');
22+
if (!testPath.startsWith('build/')) {
23+
testPath = path.join('build', testPath);
24+
}
25+
}
26+
files.push(testPath);
27+
}
28+
} else {
29+
const isNode20 = process.version.startsWith('v20.');
30+
if (isNode20) {
31+
files.push('build/tests');
32+
} else {
33+
files.push('build/tests/**/*.test.js');
34+
}
35+
}
36+
37+
const nodeArgs = [
38+
'--import',
39+
'./build/tests/setup.js',
40+
'--no-warnings=ExperimentalWarning',
41+
'--test-reporter',
42+
'spec',
43+
'--test-force-exit',
44+
'--test',
45+
...flags,
46+
...files,
47+
];
48+
49+
const child = spawn('node', nodeArgs, {
50+
stdio: 'inherit',
51+
});
52+
53+
child.on('close', code => {
54+
process.exit(code ?? 1);
55+
});

0 commit comments

Comments
 (0)