From a966aa200896958bcb842125dacc895cf8f7eac6 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 22 Feb 2024 16:26:15 -0800 Subject: [PATCH] chore: rebuild model on changing playwright.env --- src/extension.ts | 4 ++++ tests/settings.spec.ts | 37 +++++++++++++++++++++++++++++++++++++ tests/utils.ts | 15 +++++++-------- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index dbc0d5592..08ab72bed 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -214,6 +214,10 @@ export class Extension implements RunHooks { this._executionLinesChanged(); } }), + vscode.workspace.onDidChangeConfiguration(event => { + if (event.affectsConfiguration('playwright.env')) + this._rebuildModel(false); + }), this._treeItemObserver.onTreeItemSelected(item => this._treeItemSelected(item)), this._settingsView, this._testController, diff --git a/tests/settings.spec.ts b/tests/settings.spec.ts index 5d6f19b0e..ba6e9fb7d 100644 --- a/tests/settings.spec.ts +++ b/tests/settings.spec.ts @@ -70,6 +70,32 @@ test('should open test results', async ({ activate }) => { }); test('should support playwright.env', async ({ activate }) => { + const { testController } = await activate({ + 'playwright.config.js': `module.exports = {}`, + 'example.spec.ts': ` + import { test } from '@playwright/test'; + test('one', async () => { + console.log('foo=' + process.env.FOO); + console.log('bar=' + process.env.BAR); + }); + `, + }, { + env: { + 'FOO': 'foo-value', + 'BAR': { prop: 'bar-value' }, + } + }); + + const testItems = testController.findTestItems(/example.spec.ts/); + expect(testItems.length).toBe(1); + + const testRun = await testController.run(testItems); + const output = testRun.renderLog({ output: true }); + expect(output).toContain(`foo=foo-value`); + expect(output).toContain(`bar={"prop":"bar-value"}`); +}); + +test('should reload when playwright.env changes', async ({ activate }) => { const { vscode, testController } = await activate({ 'playwright.config.js': `module.exports = {}`, 'example.spec.ts': ` @@ -79,13 +105,24 @@ test('should support playwright.env', async ({ activate }) => { console.log('bar=' + process.env.BAR); }); `, + }, { + env: { + 'FOO': 'foo-value', + 'BAR': { prop: 'bar-value' }, + } }); + const configuration = vscode.workspace.getConfiguration('playwright'); configuration.update('env', { 'FOO': 'foo-value', 'BAR': { prop: 'bar-value' }, }); + // Changes to settings will trigger async update. + await expect.poll(() => testController.findTestItems(/Loading/)).toHaveLength(1); + // That will finish. + await expect.poll(() => testController.findTestItems(/Loading/)).toHaveLength(0); + const testItems = testController.findTestItems(/example.spec.ts/); expect(testItems.length).toBe(1); diff --git a/tests/utils.ts b/tests/utils.ts index 6f43d8d3b..849630f9f 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -27,7 +27,7 @@ type ActivateResult = { type TestFixtures = { vscode: VSCode, - activate: (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][] }) => Promise; + activate: (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record }) => Promise; }; export type WorkerOptions = { @@ -76,7 +76,7 @@ export const test = baseTest.extend({ activate: async ({ vscode, showBrowser, useTestServer }, use, testInfo) => { const instances: VSCode[] = []; - await use(async (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][] }) => { + await use(async (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record }) => { if (options?.workspaceFolders) { for (const wf of options?.workspaceFolders) await vscode.addWorkspaceFolder(wf[0], wf[1]); @@ -84,14 +84,13 @@ export const test = baseTest.extend({ await vscode.addWorkspaceFolder(options?.rootDir || testInfo.outputDir, files); } - if (showBrowser) { - const configuration = vscode.workspace.getConfiguration('playwright'); + const configuration = vscode.workspace.getConfiguration('playwright'); + if (options?.env) + configuration.update('env', options.env); + if (showBrowser) configuration.update('reuseBrowser', true); - } - if (useTestServer) { - const configuration = vscode.workspace.getConfiguration('playwright'); + if (useTestServer) configuration.update('useTestServer', true); - } const extension = new Extension(vscode); vscode.extensions.push(extension);