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: add existing connection log to tests #454

Merged
merged 1 commit into from
Apr 3, 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
8 changes: 6 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,19 @@ export class Extension implements RunHooks {
}
}

const items: vscodeTypes.TestItem[] = include ? [...include] : [];
try {
for (const model of this._models.enabledModels()) {
const result = model.narrowDownLocations(items);
if (!result.testIds && !result.locations)
continue;
let globalSetupResult: reporterTypes.FullResult['status'] = 'passed';
if (model.canRunGlobalHooks('setup')) {
const testListener = this._errorReportingListener(this._testRun);
const testListener = this._errorReportingListener(this._testRun, testItemForGlobalErrors);
globalSetupResult = await model.runGlobalHooks('setup', testListener);
}
if (globalSetupResult === 'passed')
await this._runTest(this._testRun, include ? [...include] : [], testItemForGlobalErrors, new Set(), model, mode === 'debug', enqueuedTests.length === 1);
await this._runTest(this._testRun, items, testItemForGlobalErrors, new Set(), model, mode === 'debug', enqueuedTests.length === 1);
}
} finally {
this._activeSteps.clear();
Expand Down
30 changes: 4 additions & 26 deletions src/playwrightTestServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { escapeRegex, pathSeparator } from './utils';
import { debugSessionName } from './debugSessionName';
import type { TestModel } from './testModel';
import { TestServerInterface } from './upstream/testServerInterface';
import { upstreamTreeItem } from './testTree';
import { collectTestIds } from './upstream/testTree';

export class PlaywrightTestServer {
private _vscode: vscodeTypes.VSCode;
Expand Down Expand Up @@ -142,7 +140,7 @@ export class PlaywrightTestServer {
if (!testServer)
return;

const { locations, testIds } = this._narrowDownLocations(items);
const { locations, testIds } = this._model.narrowDownLocations(items);
if (!locations && !testIds)
return;

Expand Down Expand Up @@ -220,7 +218,7 @@ export class PlaywrightTestServer {
if (token?.isCancellationRequested)
return;

const { locations, testIds } = this._narrowDownLocations(items);
const { locations, testIds } = this._model.narrowDownLocations(items);
if (!locations && !testIds)
return;

Expand All @@ -247,7 +245,7 @@ export class PlaywrightTestServer {
await testEndPromise;
} finally {
disposable?.dispose();
testServer?.closeGracefully({});
testServer?.close();
await this._options.runHooks.onDidRunTests(true);
}
}
Expand Down Expand Up @@ -319,27 +317,7 @@ export class PlaywrightTestServer {
const testServer = this._testServerPromise;
this._testServerPromise = undefined;
if (testServer)
testServer.then(server => server?.closeGracefully({}));
}

private _narrowDownLocations(items: vscodeTypes.TestItem[]): { locations: string[] | null, testIds?: string[] } {
if (!items.length)
return { locations: [] };
const locations = new Set<string>();
const testIds: string[] = [];
for (const item of items) {
const treeItem = upstreamTreeItem(item);
if (treeItem.kind === 'group' && (treeItem.subKind === 'folder' || treeItem.subKind === 'file')) {
for (const file of this._model.enabledFiles()) {
if (file === treeItem.location.file || file.startsWith(treeItem.location.file))
locations.add(treeItem.location.file);
}
} else {
testIds.push(...collectTestIds(treeItem));
}
}

return { locations: locations.size ? [...locations] : null, testIds: testIds.length ? testIds : undefined };
testServer.then(server => server?.close());
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/testModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { MultiMap } from './multimap';
import { PlaywrightTestServer } from './playwrightTestServer';
import type { PlaywrightTestRunOptions, RunHooks, TestConfig } from './playwrightTestTypes';
import { PlaywrightTestCLI } from './playwrightTestCLI';
import { upstreamTreeItem } from './testTree';
import { collectTestIds } from './upstream/testTree';

export type TestEntry = reporterTypes.TestCase | reporterTypes.Suite;

Expand Down Expand Up @@ -508,6 +510,26 @@ export class TestModel {
}
}
}

narrowDownLocations(items: vscodeTypes.TestItem[]): { locations: string[] | null, testIds?: string[] } {
if (!items.length)
return { locations: [] };
const locations = new Set<string>();
const testIds: string[] = [];
for (const item of items) {
const treeItem = upstreamTreeItem(item);
if (treeItem.kind === 'group' && (treeItem.subKind === 'folder' || treeItem.subKind === 'file')) {
for (const file of this.enabledFiles()) {
if (file === treeItem.location.file || file.startsWith(treeItem.location.file))
locations.add(treeItem.location.file);
}
} else {
testIds.push(...collectTestIds(treeItem));
}
}

return { locations: locations.size ? [...locations] : null, testIds: testIds.length ? testIds : undefined };
}
}

export class TestModelCollection extends DisposableBase {
Expand Down
7 changes: 7 additions & 0 deletions src/upstream/testServerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
async closeGracefully(params: Parameters<TestServerInterface['closeGracefully']>[0]): ReturnType<TestServerInterface['closeGracefully']> {
await this._sendMessage('closeGracefully', params);
}

close() {
try {
this._ws.close();
} catch {
}
}
}
29 changes: 29 additions & 0 deletions tests/debug-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { expect, test } from './utils';
import { TestRun } from './mock/vscode';
import path from 'path';

test('should debug all tests', async ({ activate }) => {
const { vscode } = await activate({
Expand Down Expand Up @@ -46,6 +47,17 @@ test('should debug all tests', async ({ activate }) => {
> playwright list-files -c playwright.config.js
> debug -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'runGlobalSetup', params: {} },
{
method: 'runTests',
params: expect.objectContaining({
locations: [],
testIds: undefined
})
},
]);
});

test('should debug one test', async ({ activate }) => {
Expand Down Expand Up @@ -74,6 +86,23 @@ test('should debug one test', async ({ activate }) => {
> playwright test -c playwright.config.js --list --reporter=null tests/test.spec.ts
> debug -c playwright.config.js tests/test.spec.ts:3
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{
method: 'listTests',
params: expect.objectContaining({
locations: [expect.stringContaining(`tests${path.sep}test\\.spec\\.ts`)]
})
},
{ method: 'runGlobalSetup', params: {} },
{
method: 'runTests',
params: expect.objectContaining({
locations: undefined,
testIds: [expect.any(String)]
})
},
]);
});

test('should debug error', async ({ activate }, testInfo) => {
Expand Down
18 changes: 18 additions & 0 deletions tests/decorations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { expect, test } from './utils';
import path from 'path';

test('should highlight steps while running', async ({ activate }) => {
const { vscode, testController } = await activate({
Expand Down Expand Up @@ -68,4 +69,21 @@ test('should highlight steps while running', async ({ activate }) => {
> playwright test -c playwright.config.js --list --reporter=null tests/test.spec.ts
> playwright test -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{
method: 'listTests',
params: expect.objectContaining({
locations: [expect.stringContaining(`tests${path.sep}test\\.spec\\.ts`)]
})
},
{ method: 'runGlobalSetup', params: {} },
{
method: 'runTests',
params: expect.objectContaining({
locations: [],
testIds: undefined
})
},
]);
});
15 changes: 15 additions & 0 deletions tests/global-errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ test('should report error in global setup (implicit)', async ({ activate, useTes
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'runGlobalSetup', params: {} },
{
method: 'runTests',
params: expect.objectContaining({
locations: [],
testIds: undefined
})
},
]);
});

test('should report error in global setup (explicit)', async ({ activate, useTestServer }) => {
Expand Down Expand Up @@ -101,4 +112,8 @@ test('should report error in global setup (explicit)', async ({ activate, useTes
> playwright list-files -c playwright.config.js
> playwright test -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'runGlobalSetup', params: {} },
]);
});
39 changes: 39 additions & 0 deletions tests/list-files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ test('should list files', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should list files top level if no testDir', async ({ activate }, testInfo) => {
Expand All @@ -49,6 +52,9 @@ test('should list files top level if no testDir', async ({ activate }, testInfo)
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should list only test files', async ({ activate }) => {
Expand Down Expand Up @@ -94,6 +100,9 @@ test('should list folders', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should pick new files', async ({ activate }) => {
Expand All @@ -110,6 +119,9 @@ test('should pick new files', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);

await Promise.all([
new Promise(f => testController.onDidChangeTestItem(f)),
Expand All @@ -126,6 +138,10 @@ test('should pick new files', async ({ activate }) => {
> playwright list-files -c playwright.config.js
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'listFiles', params: {} }
]);
});

test('should not pick non-test files', async ({ activate }) => {
Expand Down Expand Up @@ -189,6 +205,9 @@ test('should remove deleted files', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);

await Promise.all([
new Promise(f => testController.onDidChangeTestItem(f)),
Expand All @@ -205,6 +224,10 @@ test('should remove deleted files', async ({ activate }) => {
> playwright list-files -c playwright.config.js
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'listFiles', params: {} }
]);
});

test('should do nothing for not loaded changed file', async ({ activate }) => {
Expand Down Expand Up @@ -256,6 +279,9 @@ test('should support multiple projects', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should switch between multiple projects with filter', async ({ activate }) => {
Expand Down Expand Up @@ -288,6 +314,9 @@ test('should switch between multiple projects with filter', async ({ activate })
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);

await expect(vscode).toHaveProjectTree(`
config: playwright.config.js
Expand All @@ -311,6 +340,9 @@ test('should switch between multiple projects with filter', async ({ activate })
await expect(vscode).toHaveExecLog(`
> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should list files in relative folder', async ({ activate }) => {
Expand All @@ -328,6 +360,9 @@ test('should list files in relative folder', async ({ activate }) => {
await expect(vscode).toHaveExecLog(`
foo/bar> playwright list-files -c playwright.config.js
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} }
]);
});

test('should list files in multi-folder workspace with project switching', async ({ activate }, testInfo) => {
Expand Down Expand Up @@ -386,6 +421,10 @@ test('should ignore errors when listing files', async ({ activate }) => {
> playwright list-files -c playwright.config.js
> playwright list-files -c playwright.config.ts
`);
await expect(vscode).toHaveConnectionLog([
{ method: 'listFiles', params: {} },
{ method: 'listFiles', params: {} }
]);

await new Promise(f => setTimeout(f, 2000));
await expect.poll(() => vscode.languages.getDiagnostics()).toEqual([
Expand Down
Loading
Loading