From 2b5464f3c3ea06ae6690ee57481514951180ea5e Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Sun, 22 Dec 2024 09:19:24 +0100 Subject: [PATCH] chore: running tests should create workspace-settings (#579) --- src/testModel.ts | 6 ++- tests/settings.spec.ts | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/testModel.ts b/src/testModel.ts index f4649d83d..f3df7b57c 100644 --- a/src/testModel.ts +++ b/src/testModel.ts @@ -519,6 +519,8 @@ export class TestModel extends DisposableBase { if (token?.isCancellationRequested) return; + this._collection._saveSettings(); + // Run global setup with the first test. let globalSetupResult: reporterTypes.FullResult['status'] = 'passed'; if (this.canRunGlobalHooks('setup')) @@ -567,6 +569,8 @@ export class TestModel extends DisposableBase { if (token?.isCancellationRequested) return; + this._collection._saveSettings(); + // Underlying debugTest implementation will run the global setup. await this.runGlobalHooks('teardown', reporter, token); if (token?.isCancellationRequested) @@ -837,7 +841,7 @@ export class TestModelCollection extends DisposableBase { this._didUpdate.fire(); } - private _saveSettings() { + _saveSettings() { const workspaceSettings: WorkspaceSettings = { configs: [] }; for (const model of this._models) { workspaceSettings.configs!.push({ diff --git a/tests/settings.spec.ts b/tests/settings.spec.ts index 690390264..370a1c3a2 100644 --- a/tests/settings.spec.ts +++ b/tests/settings.spec.ts @@ -177,3 +177,99 @@ test('should reload when playwright.env changes', async ({ activate }) => { expect(output).toContain(`foo=foo-value`); expect(output).toContain(`bar={"prop":"bar-value"}`); }); + +test('should sync project enabled state to workspace settings', async ({ activate }) => { + const { vscode, testController } = await activate({ + 'playwright.config.ts': ` + import { defineConfig } from '@playwright/test'; + export default defineConfig({ + projects: [ + { name: 'foo', testMatch: 'foo.ts' }, + { name: 'bar', testMatch: 'bar.ts' }, + ] + }); + `, + }); + + const webView = vscode.webViews.get('pw.extension.settingsView')!; + + await expect(webView.locator('body')).toMatchAriaSnapshot(` + - text: PROJECTS + - checkbox "foo" [checked] + - checkbox "bar" [checked=false] + `); + await testController.run(testController.findTestItems(/example.spec.ts/)); + expect(vscode.context.workspaceState.get('pw.workspace-settings')).toEqual({ + configs: [ + { + enabled: true, + projects: [ + { + enabled: true, + name: 'foo', + }, + { + enabled: false, + name: 'bar', + }, + ], + relativeConfigFile: 'playwright.config.ts', + selected: true, + }, + ], + }); + + await webView.getByRole('checkbox', { name: 'bar' }).check(); + await expect(webView.locator('body')).toMatchAriaSnapshot(` + - checkbox "foo" [checked] + - checkbox "bar" [checked] + `); + expect(vscode.context.workspaceState.get('pw.workspace-settings')).toEqual(expect.objectContaining({ + configs: [ + expect.objectContaining({ + enabled: true, + projects: [ + expect.objectContaining({ name: 'foo', enabled: true }), + expect.objectContaining({ name: 'bar', enabled: true }), + ] + }) + ] + })); +}); + +test('should read project enabled state from workspace settings', async ({ vscode, activate }) => { + vscode.context.workspaceState.update('pw.workspace-settings', { + configs: [ + { + relativeConfigFile: 'playwright.config.ts', + selected: true, + enabled: true, + projects: [ + { name: 'foo', enabled: true }, + { name: 'bar', enabled: false } + ] + } + ] + }); + + await activate({ + 'playwright.config.ts': ` + import { defineConfig } from '@playwright/test'; + export default defineConfig({ + projects: [ + { name: 'foo', testMatch: 'foo.ts' }, + { name: 'bar', testMatch: 'bar.ts' }, + { name: 'baz', testMatch: 'baz.ts' }, + ] + }); + `, + }); + + const webView = vscode.webViews.get('pw.extension.settingsView')!; + await expect(webView.locator('body')).toMatchAriaSnapshot(` + - text: PROJECTS + - checkbox "foo" [checked] + - checkbox "bar" [checked=false] + - checkbox "baz" [checked=false] + `); +}); \ No newline at end of file