Skip to content

Commit

Permalink
test(trace-viewer): use latch to control test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Jul 29, 2024
1 parent 49a77a2 commit 308cba7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
21 changes: 12 additions & 9 deletions tests/spawn-trace-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,28 @@ test('should not open trace viewer if test did not run', async ({ activate }) =>
await expect.poll(() => traceViewerInfo(vscode)).toBeUndefined();
});

test('should refresh trace viewer while test is running', async ({ activate }) => {
test('should refresh trace viewer while test is running', async ({ activate, createLatch }) => {
const latch = createLatch();

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 new Promise(r => setTimeout(r, 1000)));
test('should pass', async () => ${latch.blockingCode});
`,
});

await testController.expandTestItems(/test.spec/);
selectTestItem(testController.findTestItems(/pass/)[0]);

await Promise.all([
testController.run(),
expect.poll(() => traceViewerInfo(vscode)).toMatchObject({
type: 'spawn',
traceFile: expect.stringMatching(/\.json$/),
}),
]);
const testRunPromise = testController.run();
await expect.poll(() => traceViewerInfo(vscode)).toMatchObject({
type: 'spawn',
traceFile: expect.stringMatching(/\.json$/),
});

latch.open();
await testRunPromise;

await expect.poll(() => traceViewerInfo(vscode)).toMatchObject({
type: 'spawn',
Expand Down
31 changes: 31 additions & 0 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { expect as baseExpect, test as baseTest, Browser, chromium, Page } from
import { Extension } from '../out/extension';
import { TestController, VSCode, WorkspaceFolder, TestRun, TestItem } from './mock/vscode';

import crypto from 'crypto';
import fs from 'fs';
import path from 'path';

type ActivateResult = {
Expand All @@ -26,11 +28,18 @@ type ActivateResult = {
workspaceFolder: WorkspaceFolder;
};

type Latch = {
blockingCode: string;
open: () => void;
close: () => void;
};

type TestFixtures = {
vscode: VSCode,
activate: (files: { [key: string]: string }, options?: { rootDir?: string, workspaceFolders?: [string, any][], env?: Record<string, any> }) => Promise<ActivateResult>;
showTrace: boolean;
envRemoteName?: string;
createLatch: () => Latch;
};

export type WorkerOptions = {
Expand Down Expand Up @@ -160,6 +169,18 @@ export const test = baseTest.extend<TestFixtures, WorkerOptions>({
for (const vscode of instances)
vscode.dispose();
},

// Copied from https://github.com/microsoft/playwright/blob/7e7319da7d84de6648900e27e6d844bec9071222/tests/playwright-test/ui-mode-fixtures.ts#L132
createLatch: async ({}, use, testInfo) => {
await use(() => {
const latchFile = path.join(testInfo.project.outputDir, createGuid() + '.latch');
return {
blockingCode: `await ((${waitForLatch})(${JSON.stringify(latchFile)}))`,
open: () => fs.writeFileSync(latchFile, 'ok'),
close: () => fs.unlinkSync(latchFile),
};
});
},
});

export async function connectToSharedBrowser(vscode: VSCode) {
Expand Down Expand Up @@ -234,3 +255,13 @@ export async function singleWebViewByPanelType(vscode: VSCode, viewType: string)
export function traceViewerInfo(vscode: VSCode): { type: 'spawn' | 'embedded', serverUrlPrefix?: string, testConfigFile: string } | undefined {
return vscode.extensions[0].traceViewerInfoForTest();
}

async function waitForLatch(latchFile: string) {
const fs = require('fs');
while (!fs.existsSync(latchFile))
await new Promise(f => setTimeout(f, 250));
}

function createGuid(): string {
return crypto.randomBytes(16).toString('hex');
}

0 comments on commit 308cba7

Please sign in to comment.