Skip to content

Commit 0347448

Browse files
fix(test runner): track shared ESM dependencies for only-changed (#43837)
Co-authored-by: microsoft-playwright-automation[bot] <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com>
1 parent 86e6924 commit 0347448

8 files changed

Lines changed: 98 additions & 17 deletions

File tree

‎packages/playwright/src/common/testLoader.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import path from 'path';
1818

1919
import { isWorkerProcess, setCurrentlyLoadingFileSuite } from '../globals';
2020
import { Suite } from './test';
21-
import { requireOrImport, startCollectingFileDeps, stopCollectingFileDeps } from '../transform/transform';
21+
import { installTransformIfNeeded, requireOrImport, startCollectingFileDeps, stopCollectingFileDeps } from '../transform/transform';
2222
import { serializeError } from '../util';
2323

2424
import type { Location, TestError } from '../../types/testReporter';
@@ -39,9 +39,10 @@ export async function loadTestFile(file: string, config: FullConfigInternal, tes
3939
suite._tags = [...config.config.tags];
4040

4141
setCurrentlyLoadingFileSuite(suite);
42-
if (!isWorkerProcess())
43-
await startCollectingFileDeps();
4442
try {
43+
installTransformIfNeeded();
44+
if (!isWorkerProcess())
45+
await startCollectingFileDeps();
4546
await requireOrImport(file);
4647
cachedFileSuites.set(file, suite);
4748
} catch (e) {

‎packages/playwright/src/transform/DEPS.list‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
../util.ts
33
../globals.ts
44
../package.ts
5+
@isomorphic/multimap.ts
56
@utils/**
67

78
node_modules/json5

‎packages/playwright/src/transform/compilationCache.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import fs from 'fs';
1818
import os from 'os';
1919
import path from 'path';
20+
import url from 'url';
2021

2122
import sourceMapSupport from 'source-map-support';
23+
import { MultiMap } from '@isomorphic/multimap';
2224
import { calculateSha1 } from '@utils/crypto';
2325

2426
import { isWorkerProcess } from '../globals';
@@ -208,14 +210,35 @@ function clearOldCacheEntries(cacheFolderName: string, filePathHash: string) {
208210
// Since ESM and CJS collect dependencies differently,
209211
// we go via the global state to collect them.
210212
let depsCollector: Set<string> | undefined;
213+
// Retain import and require edges even outside collection, for later test files that reuse cached modules.
214+
const importGraph = new MultiMap<string, string>();
215+
216+
export function recordResolvedFile(resolvedUrl: string | undefined, parentUrl: string | undefined) {
217+
if (isWorkerProcess() || !resolvedUrl?.startsWith('file://'))
218+
return;
219+
const child = url.fileURLToPath(resolvedUrl);
220+
depsCollector?.add(child);
221+
if (!parentUrl?.startsWith('file://'))
222+
return;
223+
const parent = url.fileURLToPath(parentUrl);
224+
if (!belongsToNodeModules(parent) && !importGraph.hasValue(parent, child))
225+
importGraph.set(parent, child);
226+
}
211227

212228
export function startCollectingFileDeps() {
213229
depsCollector = new Set();
214230
}
215231

216-
export function stopCollectingFileDeps(filename: string) {
232+
export function stopCollectingFileDeps(filename: string, additionalDeps: string[] = []) {
217233
if (!depsCollector)
218234
return;
235+
for (const dep of additionalDeps)
236+
depsCollector.add(dep);
237+
// Sets visit entries added during iteration, so this collects the whole transitive closure.
238+
for (const dep of depsCollector) {
239+
for (const child of importGraph.get(dep))
240+
depsCollector.add(child);
241+
}
219242
depsCollector.delete(filename);
220243
for (const dep of depsCollector) {
221244
if (belongsToNodeModules(dep))

‎packages/playwright/src/transform/esmLoader.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import fs from 'fs';
1818
import url from 'url';
1919

20-
import { addToCompilationCache, currentFileDepsCollector, serializeCompilationCache, startCollectingFileDeps, stopCollectingFileDeps } from './compilationCache';
20+
import { addToCompilationCache, recordResolvedFile, serializeCompilationCache, startCollectingFileDeps, stopCollectingFileDeps } from './compilationCache';
2121
import { PortTransport } from './portTransport';
2222
import { hasTransformedExtension, isStyleFile, resolveHook, setSingleTSConfig, setTransformConfig, shouldTransform, transformHook } from './transform';
2323
import { fileIsModule } from '../util';
@@ -39,8 +39,7 @@ async function resolve(originalSpecifier: string, context: { parentURL?: string
3939
const result = await defaultResolve(specifier, context, defaultResolve);
4040
// Note: we collect dependencies here that will be sent to the main thread
4141
// (and optionally runner process) after the loading finishes.
42-
if (result?.url && result.url.startsWith('file://'))
43-
currentFileDepsCollector()?.add(url.fileURLToPath(result.url));
42+
recordResolvedFile(result?.url, context.parentURL);
4443

4544
if (originalSpecifier.endsWith(esmPreflightExtension))
4645
result.url = result.url + esmPreflightExtension;
@@ -135,7 +134,7 @@ function createTransport(port: MessagePort) {
135134
}
136135

137136
if (method === 'stopCollectingFileDeps') {
138-
stopCollectingFileDeps(params.file);
137+
stopCollectingFileDeps(params.file, params.dependencies);
139138
return;
140139
}
141140
});

‎packages/playwright/src/transform/esmLoaderSync.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import fs from 'fs';
1818
import url from 'url';
1919

20-
import { currentFileDepsCollector } from './compilationCache';
20+
import { recordResolvedFile } from './compilationCache';
2121
import { hasTransformedExtension, isStyleFile, resolveHook, shouldTransform, transformHook } from './transform';
2222
import { fileIsModule } from '../util';
2323

@@ -36,8 +36,7 @@ export function resolve(specifier: string, context: { parentURL?: string, condit
3636
}
3737
}
3838
const result = nextResolve(specifier, context);
39-
if (result?.url && result.url.startsWith('file://'))
40-
currentFileDepsCollector()?.add(url.fileURLToPath(result.url));
39+
recordResolvedFile(result?.url, context.parentURL);
4140
return result;
4241
}
4342

‎packages/playwright/src/transform/transform.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export async function requireOrImport(file: string) {
323323

324324
let transformInstalled = false;
325325

326-
function installTransformIfNeeded() {
326+
export function installTransformIfNeeded() {
327327
if (transformInstalled)
328328
return;
329329
transformInstalled = true;
@@ -476,7 +476,7 @@ export async function startCollectingFileDeps() {
476476
export async function stopCollectingFileDeps(file: string) {
477477
cc.stopCollectingFileDeps(file);
478478
if (loaderChannel)
479-
await loaderChannel.send('stopCollectingFileDeps', { file });
479+
await loaderChannel.send('stopCollectingFileDeps', { file, dependencies: [...cc.dependenciesForTestFile(file)] });
480480
}
481481

482482
export async function incorporateCompilationCache() {

‎tests/playwright-test/only-changed.spec.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ test('should diff based on base commit', async ({ runInlineTest, git, writeFiles
105105
expect(result.output).toContain('b.spec.ts');
106106
});
107107

108-
test('should understand dependency structure', async ({ runInlineTest, git, writeFiles }) => {
108+
test('should understand dependency structure', {
109+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43824' },
110+
}, async ({ runInlineTest, git, writeFiles }) => {
109111
await writeFiles({
110112
'a.spec.ts': `
111113
import { test, expect } from '@playwright/test';
@@ -149,6 +151,43 @@ test('should understand dependency structure', async ({ runInlineTest, git, writ
149151
expect(result.output).toContain('a.spec.ts');
150152
expect(result.output).toContain('b.spec.ts');
151153
expect(result.output).not.toContain('c.spec.ts');
154+
155+
await writeFiles({
156+
'utils.ts': `
157+
export * from './answer';
158+
export * from './question';
159+
export const ready = import('./leaf.mjs');
160+
`,
161+
'leaf.mjs': `export const value = 'original';`,
162+
});
163+
git('add .');
164+
git('commit -m mixed');
165+
const mixedResult = await runInlineTest({
166+
'leaf.mjs': `export const value = 'changed';`,
167+
}, { 'only-changed': true, 'list': true }, { PLAYWRIGHT_FORCE_ASYNC_LOADER: '1' });
168+
expect(mixedResult.exitCode).toBe(0);
169+
expect(mixedResult.output).toContain('a.spec.ts');
170+
expect(mixedResult.output).toContain('b.spec.ts');
171+
expect(mixedResult.output).not.toContain('c.spec.ts');
172+
expect(mixedResult.output).toContain('Total: 2 tests in 2 files');
173+
174+
const packageJson = `{ "type": "module" }`;
175+
await writeFiles({ 'package.json': packageJson });
176+
git('add .');
177+
git('commit -m esm');
178+
179+
for (const env of [{}, { PLAYWRIGHT_FORCE_ASYNC_LOADER: '1' }]) {
180+
const esmResult = await runInlineTest({
181+
'package.json': packageJson,
182+
'question.ts': `export const question = "changed";`,
183+
}, { 'only-changed': true, 'list': true }, env);
184+
185+
expect(esmResult.exitCode).toBe(0);
186+
expect(esmResult.output).toContain('a.spec.ts');
187+
expect(esmResult.output).toContain('b.spec.ts');
188+
expect(esmResult.output).not.toContain('c.spec.ts');
189+
expect(esmResult.output).toContain('Total: 2 tests in 2 files');
190+
}
152191
});
153192

154193
test('watch mode is not supported', async ({ runWatchTest }) => {

‎tests/playwright-test/test-server.spec.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,35 @@ test('find related test files errors', async ({ startTestServer, writeFiles }) =
246246
expect((await testServerConnection.runGlobalTeardown({})).status).toBe('passed');
247247
});
248248

249-
test('find related test files', async ({ startTestServer, writeFiles }) => {
250-
await writeFiles(filesWithDependency);
249+
test('find related test files', {
250+
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43824' },
251+
}, async ({ startTestServer, writeFiles }) => {
251252
const testServerConnection = await startTestServer();
253+
await writeFiles({
254+
...filesWithDependency,
255+
'package.json': `{ "type": "module" }`,
256+
'src/other.test.ts': `
257+
import { test, expect } from '@playwright/test';
258+
import { label } from './button';
259+
test('pass', () => expect(label).toBe('Button'));
260+
`,
261+
'src/unrelated.test.ts': `
262+
import { test } from '@playwright/test';
263+
test('pass', () => {});
264+
`,
265+
'src/button.ts': `export { label } from './label';`,
266+
'src/label.ts': `export const label = 'Button';`,
267+
});
252268
await testServerConnection.initialize({ interceptStdio: true });
253269
expect((await testServerConnection.runGlobalSetup({})).status).toBe('passed');
254270

255271
const buttonTs = test.info().outputPath('src/button.ts');
256272
const buttonTestTs = test.info().outputPath('src/button.test.ts');
257273
const result = await testServerConnection.findRelatedTestFiles({ files: [buttonTs] });
258-
expect(result).toEqual({ testFiles: [buttonTestTs] });
274+
const otherTestTs = test.info().outputPath('src/other.test.ts');
275+
expect(result).toEqual({ testFiles: [buttonTestTs, otherTestTs] });
276+
expect(await testServerConnection.findRelatedTestFiles({ files: [test.info().outputPath('src/label.ts')] }))
277+
.toEqual({ testFiles: [buttonTestTs, otherTestTs] });
259278

260279
expect((await testServerConnection.runGlobalTeardown({})).status).toBe('passed');
261280
});

0 commit comments

Comments
 (0)