Skip to content

Commit

Permalink
test(trace-viewer): embedded trace viewer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Jul 23, 2024
1 parent e8a5a55 commit cfc7901
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,10 @@ export class Extension implements RunHooks {
this._treeItemSelected(testItem);
}

traceViewerInfoForTest() {
return this._currentTraceViewer?.infoForTest();
}

private _showTrace(testItem: vscodeTypes.TestItem) {
const traceUrl = (testItem as any)[traceUrlSymbol];
if (traceUrl)
Expand Down
24 changes: 24 additions & 0 deletions src/traceViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class SpawnTraceViewer extends DisposableBase {
private _traceViewerProcess: ChildProcess | undefined;
private _settingsModel: SettingsModel;
private _config: TestConfig;
private _serverUrlPrefix?: string;

constructor(vscode: vscodeTypes.VSCode, settingsModel: SettingsModel, envProvider: () => NodeJS.ProcessEnv, config: TestConfig) {
super();
Expand Down Expand Up @@ -107,6 +108,13 @@ export class SpawnTraceViewer extends DisposableBase {
this._vscode.window.showErrorMessage(error.message);
this.close().catch(() => {});
});
if (this._vscode.isUnderTest) {
traceViewerProcess.stdout?.on('data', data => {
const match = data.toString().match(/Listening on (.*)/);
if (match)
this._serverUrlPrefix = match[1];
});
}
}

checkVersion() {
Expand All @@ -125,6 +133,14 @@ export class SpawnTraceViewer extends DisposableBase {
this._traceViewerProcess?.stdin?.end();
this._traceViewerProcess = undefined;
}

infoForTest() {
return {
type: 'spawn',
serverUrlPrefix: this._serverUrlPrefix,
testConfigFile: this._config.configFile
};
}
}

export class EmbeddedTraceViewer extends DisposableBase {
Expand Down Expand Up @@ -227,6 +243,14 @@ export class EmbeddedTraceViewer extends DisposableBase {
}
return true;
}

infoForTest() {
return {
type: 'embedded',
serverUrlPrefix: this._serverUrlPrefix,
testConfigFile: this._config.configFile
};
}
}

class EmbeddedTraceViewerPanel extends DisposableBase {
Expand Down
4 changes: 3 additions & 1 deletion src/vscodeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ export type {
TerminalLink,
} from 'vscode';

export type VSCode = typeof import('vscode');
export type VSCode = typeof import('vscode') & {
isUnderTest?: boolean;
};
1 change: 1 addition & 0 deletions tests/mock/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ export class VSCode {
'playwright.env': {},
'playwright.reuseBrowser': false,
'playwright.showTrace': false,
'playwright.embedTraceViewer': false,
'workbench.colorTheme': 'Dark Modern',
};
this.workspace.getConfiguration = scope => {
Expand Down
47 changes: 47 additions & 0 deletions tests/trace-viewer-legacy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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, expect, traceViewerInfo } from './utils';

test.beforeEach(({ showBrowser, overridePlaywrightVersion }) => {
test.skip(!overridePlaywrightVersion || showBrowser);
// prevents spawn trace viewer process from opening in browser
process.env.PWTEST_UNDER_TEST = '1';
});

test.use({ showTrace: true, embedTraceViewer: true, envRemoteName: 'ssh-remote' });

test('should fallback to spawn trace viewer in older @playwright/test projects', async ({ activate }) => {
const { vscode, testController } = await activate({
'playwright.config.js': `module.exports = { testDir: 'tests' }`,
'tests/test.spec.ts': `
import { test } from '@playwright/test';
test('should pass', async () => {});
`,
});

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

await expect.poll(() => vscode.warnings).toContain('Playwright v1.46+ is required for embedded trace viewer to work, v1.43 found');

await expect.poll(() => traceViewerInfo(vscode)).toMatchObject({
type: 'spawn',
serverUrlPrefix: expect.anything(),
testConfigFile: expect.stringContaining('playwright.config.js')
});
});
Loading

0 comments on commit cfc7901

Please sign in to comment.