Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix debug stop and align config folder in test vs debug #478

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/playwrightTestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class PlaywrightTestServer {
try {
if (type === 'setup') {
const { report, status } = await testServer.runGlobalSetup({});
testListener.onStdOut?.('\x1b[2mRunning global setup if any\u2026\x1b[0m\n');
for (const message of report)
teleReceiver.dispatch(message);
return status;
Expand Down Expand Up @@ -189,7 +190,6 @@ export class PlaywrightTestServer {
}

async debugTests(items: vscodeTypes.TestItem[], runOptions: PlaywrightTestRunOptions, reporter: reporterTypes.ReporterV2, token: vscodeTypes.CancellationToken): Promise<void> {
const configFolder = path.dirname(this._model.config.configFile);
const configFile = path.basename(this._model.config.configFile);
const args = ['test-server', '-c', configFile];

Expand All @@ -212,7 +212,7 @@ export class PlaywrightTestServer {
type: 'pwa-node',
name: debugSessionName,
request: 'launch',
cwd: configFolder,
cwd: this._model.config.workspaceFolder,
env: {
...process.env,
CI: this._options.isUnderTest ? undefined : process.env.CI,
Expand Down Expand Up @@ -330,15 +330,21 @@ export class PlaywrightTestServer {
resolvePath: (rootDir: string, relativePath: string) => this._vscode.Uri.file(path.join(rootDir, relativePath)).fsPath,
});
return new Promise<void>(resolve => {
const disposable = testServer.onReport(message => {
if (token.isCancellationRequested && message.method !== 'onEnd')
return;
teleReceiver.dispatch(message);
if (message.method === 'onEnd') {
disposable.dispose();
const disposables = [
testServer.onReport(message => {
if (token.isCancellationRequested && message.method !== 'onEnd')
return;
teleReceiver.dispatch(message);
if (message.method === 'onEnd') {
disposables.forEach(d => d.dispose());
resolve();
}
}),
testServer.onClose(() => {
disposables.forEach(d => d.dispose());
resolve();
}
});
}),
];
});
}

Expand Down
1 change: 1 addition & 0 deletions src/playwrightTestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type PlaywrightTestRunOptions = {
headed?: boolean;
workers?: string | number;
trace?: 'on' | 'off';
video?: 'on' | 'off';
reuseContext?: boolean;
connectWsEndpoint?: string;
};
Expand Down
8 changes: 7 additions & 1 deletion src/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,24 @@ export class TestModel {
const showBrowser = this._options.settingsModel.showBrowser.get() && !!externalOptions.connectWsEndpoint;

let trace: 'on' | 'off' | undefined;
let video: 'on' | 'off' | undefined;

if (this._options.settingsModel.showTrace.get())
trace = 'on';
// "Show browser" mode forces context reuse that survives over multiple test runs.
// Playwright Test sets up `tracesDir` inside the `test-results` folder, so it will be removed between runs.
// When context is reused, its ongoing tracing will fail with ENOENT because trace files
// were suddenly removed. So we disable tracing in this case.
if (this._options.settingsModel.showBrowser.get())
if (this._options.settingsModel.showBrowser.get()) {
trace = 'off';
video = 'off';
}

const options: PlaywrightTestRunOptions = {
headed: showBrowser && !this._options.isUnderTest,
workers: showBrowser ? 1 : undefined,
trace,
video,
reuseContext: showBrowser,
connectWsEndpoint: showBrowser ? externalOptions.connectWsEndpoint : undefined,
};
Expand All @@ -501,6 +506,7 @@ export class TestModel {
const options: PlaywrightTestRunOptions = {
headed: !this._options.isUnderTest,
workers: 1,
video: 'off',
trace: 'off',
reuseContext: false,
connectWsEndpoint: externalOptions.connectWsEndpoint,
Expand Down
1 change: 1 addition & 0 deletions src/upstream/testServerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface TestServerInterface {
timeout?: number,
reporters?: string[],
trace?: 'on' | 'off';
video?: 'on' | 'off';
projects?: string[];
reuseContext?: boolean;
connectWsEndpoint?: string;
Expand Down
36 changes: 36 additions & 0 deletions tests/debug-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,39 @@ test('should debug error', async ({ activate }, testInfo) => {

testRun.token.source.cancel();
});

test('should end test run when stopping the debugging', async ({ activate }, testInfo) => {
const { vscode, testController } = await activate({
'playwright.config.js': `module.exports = { testDir: 'tests' }`,
'tests/test.spec.ts': `
import { test } from '@playwright/test';
test('should fail', async () => {
// Simulate breakpoint via stalling.
console.log('READY TO BREAK');
await new Promise(() => {});
});
`,
});

await testController.expandTestItems(/test.spec/);
const testItems = testController.findTestItems(/fail/);

const profile = testController.debugProfile();
const testRunPromise = new Promise<TestRun>(f => testController.onDidCreateTestRun(f));
profile.run(testItems);
const testRun = await testRunPromise;
await expect.poll(() => vscode.debug.output).toContain('READY TO BREAK');

const endPromise = new Promise(f => testRun.onDidEnd(f));
vscode.debug.stopDebugging();
await endPromise;

expect(testRun.renderLog({ messages: true })).toBe(`
tests > test.spec.ts > should fail [2:0]
enqueued
enqueued
started
`);

testRun.token.source.cancel();
});
13 changes: 9 additions & 4 deletions tests/mock/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import glob from 'glob';
import path from 'path';
import { Disposable, EventEmitter, Event } from '../../src/upstream/events';
import minimatch from 'minimatch';
import { spawn } from 'child_process';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import which from 'which';
import { Browser, Page } from '@playwright/test';
import { CancellationToken } from '../../src/vscodeTypes';
Expand Down Expand Up @@ -640,6 +640,7 @@ class Debug {
output = '';
dapFactories: any[] = [];
private _dapSniffer: any;
private _debuggerProcess: ChildProcessWithoutNullStreams;

constructor() {
}
Expand All @@ -654,13 +655,13 @@ class Debug {
this._dapSniffer = factory.createDebugAdapterTracker(session);
this._didStartDebugSession.fire(session);
const node = await which('node');
const subprocess = spawn(node, [configuration.program, ...configuration.args], {
this._debuggerProcess = spawn(node, [configuration.program, ...configuration.args], {
cwd: configuration.cwd,
stdio: 'pipe',
env: configuration.env,
});

subprocess.stdout.on('data', data => {
this._debuggerProcess.stdout.on('data', data => {
this.output += data.toString();
this._dapSniffer.onDidSendMessage({
type: 'event',
Expand All @@ -671,10 +672,14 @@ class Debug {
}
});
});
subprocess.stderr.on('data', data => this.output += data.toString());
this._debuggerProcess.stderr.on('data', data => this.output += data.toString());
return true;
}

stopDebugging() {
this._debuggerProcess.kill();
}

simulateStoppedOnError(error: string, location: { file: string; line: number; }) {
this._dapSniffer.onDidSendMessage({
success: true,
Expand Down
5 changes: 3 additions & 2 deletions tests/run-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ test('should run tests for folders above root', async ({ activate }) => {
`);
});

test('should produce output twice', async ({ activate }) => {
test('should produce output twice', async ({ activate, overridePlaywrightVersion }) => {
const { testController } = await activate({
'playwright.config.js': `module.exports = {
testDir: 'tests',
Expand All @@ -1168,12 +1168,13 @@ test('should produce output twice', async ({ activate }) => {
expect(testItems.length).toBe(1);

const testRun1 = await testController.run(testItems);
const runningGlobalSetup = overridePlaywrightVersion ? '' : '\n Running global setup if any…';
expect(testRun1.renderLog({ output: true })).toBe(`
tests > test.spec.ts > one [2:0]
enqueued
started
passed
Output:
Output:${runningGlobalSetup}

Running 1 test using 1 worker

Expand Down