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

Commit c4e493b

Browse files
authored
test: support testing a single test file without only (#761)
This PR adds a test.ts wrapper over the test runner to simplify scripts in package.json and allow running a single test file.
1 parent 68ae2f8 commit c4e493b

File tree

5 files changed

+67
-15
lines changed

5 files changed

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

scripts/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
"noEmit": true,
2020
"useUnknownInCatchVariables": false
2121
},
22-
"include": ["./**/*.ts", "./**/*.js"]
22+
"include": ["./**/*.ts", "./**/*.js", "./**/*.mjs"]
2323
}

0 commit comments

Comments
 (0)