playwright/tests/playwright-test/test-server-connection.spec.ts
Simon Knott 8412d973c0
fix(ui): added test in watched file should be run (#31842)
Closes https://github.com/microsoft/playwright/issues/22211

Currently, when the server notifies the UI about changed files, the UI
determines what files to re-run based on an old test list. By listing
tests before that, we make sure that the test list is up-to-date, and
that added tests are included in the next run.

I've also removed the `listChanged` event as discussed in the team sync.
The event isn't used anywhere and fires in exactly the same cases where
`testFilesChanged` fired, so i've folded them into one another. This allowed simplifying `Watcher`.
2024-07-30 14:17:41 +02:00

97 lines
3.4 KiB
TypeScript

/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test as baseTest, expect } from './ui-mode-fixtures';
import { TestServerConnection } from '../../packages/playwright/lib/isomorphic/testServerConnection';
class TestServerConnectionUnderTest extends TestServerConnection {
events: [string, any][] = [];
constructor(wsUrl: string) {
super(wsUrl);
this.onTestFilesChanged(params => this.events.push(['testFilesChanged', params]));
this.onStdio(params => this.events.push(['stdio', params]));
this.onLoadTraceRequested(params => this.events.push(['loadTraceRequested', params]));
}
}
const test = baseTest.extend<{ testServerConnection: TestServerConnectionUnderTest }>({
testServerConnection: async ({ startCLICommand }, use, testInfo) => {
testInfo.skip(!globalThis.WebSocket, 'WebSocket not available prior to Node 22.4.0');
const testServerProcess = await startCLICommand({}, 'test-server');
await testServerProcess.waitForOutput('Listening on');
const line = testServerProcess.output.split('\n').find(l => l.includes('Listening on'));
const wsEndpoint = line!.split(' ')[2];
await use(new TestServerConnectionUnderTest(wsEndpoint));
await testServerProcess.kill();
}
});
test('file watching', async ({ testServerConnection, writeFiles }, testInfo) => {
await writeFiles({
'utils.ts': `
export const expected = 42;
`,
'a.test.ts': `
import { test } from '@playwright/test';
import { expected } from "./utils";
test('foo', () => {
expect(123).toBe(expected);
});
`,
});
const tests = await testServerConnection.listTests({});
expect(tests.report.map(e => e.method)).toEqual(['onConfigure', 'onProject', 'onBegin', 'onEnd']);
await testServerConnection.watch({ fileNames: [testInfo.outputPath('a.test.ts')] });
await writeFiles({
'utils.ts': `
export const expected = 123;
`,
});
await expect.poll(() => testServerConnection.events).toHaveLength(1);
expect(testServerConnection.events).toEqual([
['testFilesChanged', { testFiles: [testInfo.outputPath('a.test.ts')] }]
]);
});
test('stdio interception', async ({ testServerConnection, writeFiles }) => {
await testServerConnection.initialize({ interceptStdio: true });
await writeFiles({
'a.test.ts': `
import { test, expect } from '@playwright/test';
test('foo', () => {
console.log("this goes to stdout");
console.error("this goes to stderr");
expect(true).toBe(true);
});
`,
});
const tests = await testServerConnection.runTests({ trace: 'on' });
expect(tests).toEqual({ status: 'passed' });
await expect.poll(() => testServerConnection.events).toEqual(expect.arrayContaining([
['stdio', { type: 'stderr', text: 'this goes to stderr\n' }],
['stdio', { type: 'stdout', text: 'this goes to stdout\n' }]
]));
});