Skip to content

Commit e593c64

Browse files
committed
cherry-pick(#37757): chore(mcp): fallback to cwd when resolving test config
1 parent a8a6e10 commit e593c64

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

packages/playwright/src/mcp/test/testBackend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class TestServerBackend implements mcp.ServerBackend {
5858
return;
5959
}
6060

61-
throw new Error('No config option or MCP root path provided');
61+
this._context.initialize(rootPath, resolveConfigLocation(undefined));
6262
}
6363

6464
async listTools(): Promise<mcp.Tool[]> {

tests/mcp/fixtures.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type CDPServer = {
4848
export type StartClient = (options?: {
4949
clientName?: string,
5050
args?: string[],
51+
omitArgs?: string[],
52+
cwd?: string,
5153
config?: Config,
5254
roots?: { name: string, uri: string }[],
5355
rootsResponseDelay?: number,
@@ -84,14 +86,14 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
8486
const clients: Client[] = [];
8587

8688
await use(async options => {
87-
const args: string[] = mcpArgs ?? [];
89+
let args: string[] = mcpArgs ?? [];
8890

8991
if (mcpHeadless)
9092
args.push('--headless');
9193

9294
if (mcpServerType === 'test-mcp') {
9395
if (!options?.args?.some(arg => arg.startsWith('--config')))
94-
args.push('--config', test.info().outputPath());
96+
args.push(`--config=${test.info().outputPath()}`);
9597
} else {
9698
if (process.env.CI && process.platform === 'linux')
9799
args.push('--no-sandbox');
@@ -106,6 +108,8 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
106108

107109
if (options?.args)
108110
args.push(...options.args);
111+
if (options?.omitArgs)
112+
args = args.filter(arg => !options.omitArgs?.includes(arg));
109113

110114
const client = new Client({ name: options?.clientName ?? 'test', version: '1.0.0' }, options?.roots ? { capabilities: { roots: {} } } : undefined);
111115
if (options?.roots) {
@@ -118,7 +122,7 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
118122
});
119123
}
120124
const env = { ...process.env, ...options?.env };
121-
const { transport, stderr } = await createTransport(mcpServerType, args, env);
125+
const { transport, stderr } = await createTransport(mcpServerType, { args, env, cwd: options?.cwd || test.info().outputPath() });
122126
let stderrBuffer = '';
123127
stderr?.on('data', data => {
124128
if (process.env.PWDEBUGIMPL)
@@ -182,18 +186,18 @@ export const test = serverTest.extend<TestFixtures & TestOptions, WorkerFixtures
182186
mcpServerType: ['mcp', { option: true }],
183187
});
184188

185-
async function createTransport(mcpServerType: TestOptions['mcpServerType'], args: string[], env: NodeJS.ProcessEnv): Promise<{
189+
async function createTransport(mcpServerType: TestOptions['mcpServerType'], options: { args: string[], env: NodeJS.ProcessEnv, cwd: string }): Promise<{
186190
transport: Transport,
187191
stderr: Stream | null,
188192
}> {
189193
const profilesDir = test.info().outputPath('ms-playwright');
190194
const transport = new StdioClientTransport({
191195
command: 'node',
192-
args: [...(mcpServerType === 'test-mcp' ? testMcpServerPath : mcpServerPath), ...args],
193-
cwd: test.info().outputPath(),
196+
args: [...(mcpServerType === 'test-mcp' ? testMcpServerPath : mcpServerPath), ...options.args],
197+
cwd: options.cwd,
194198
stderr: 'pipe',
195199
env: {
196-
...env,
200+
...options.env,
197201
DEBUG_COLORS: '0',
198202
DEBUG_HIDE_DATE: '1',
199203
PWMCP_PROFILES_DIR_FOR_TEST: profilesDir,

tests/mcp/test-list.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ test('test_list', async ({ startClient }) => {
4545
[id=<ID>] a.test.ts:6:11 › example2
4646
Total: 4 tests in 1 file`);
4747
});
48+
49+
test('test_list config in cwd', async ({ startClient }) => {
50+
await writeFiles({
51+
'a.test.ts': `
52+
import { test, expect } from '@playwright/test';
53+
test('passes', () => {});
54+
`,
55+
});
56+
57+
const { client } = await startClient({
58+
omitArgs: ['--config'],
59+
});
60+
expect(await client.callTool({
61+
name: 'test_list',
62+
})).toHaveTextResponse(`Listing tests:
63+
[id=<ID>] a.test.ts:3:11 › passes
64+
Total: 1 test in 1 file`);
65+
});

0 commit comments

Comments
 (0)