mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: collect test dependencies (#20645)
This commit is contained in:
parent
33a05446d2
commit
b6df48758d
@ -18,6 +18,7 @@
|
|||||||
"./cli": "./cli.js",
|
"./cli": "./cli.js",
|
||||||
"./package.json": "./package.json",
|
"./package.json": "./package.json",
|
||||||
"./lib/cli": "./lib/cli.js",
|
"./lib/cli": "./lib/cli.js",
|
||||||
|
"./lib/internalsForTest": "./lib/internalsForTest.js",
|
||||||
"./lib/experimentalLoader": "./lib/experimentalLoader.js",
|
"./lib/experimentalLoader": "./lib/experimentalLoader.js",
|
||||||
"./lib/mount": "./lib/mount.js",
|
"./lib/mount": "./lib/mount.js",
|
||||||
"./lib/plugins": "./lib/plugins/index.js",
|
"./lib/plugins": "./lib/plugins/index.js",
|
||||||
|
@ -32,6 +32,7 @@ const cacheDir = process.env.PWTEST_CACHE_DIR || path.join(os.tmpdir(), 'playwri
|
|||||||
|
|
||||||
const sourceMaps: Map<string, string> = new Map();
|
const sourceMaps: Map<string, string> = new Map();
|
||||||
const memoryCache = new Map<string, MemoryCache>();
|
const memoryCache = new Map<string, MemoryCache>();
|
||||||
|
const fileDependencies = new Map<string, string[]>();
|
||||||
|
|
||||||
Error.stackTraceLimit = 200;
|
Error.stackTraceLimit = 200;
|
||||||
|
|
||||||
@ -91,6 +92,7 @@ export function serializeCompilationCache(): any {
|
|||||||
return {
|
return {
|
||||||
sourceMaps: [...sourceMaps.entries()],
|
sourceMaps: [...sourceMaps.entries()],
|
||||||
memoryCache: [...memoryCache.entries()],
|
memoryCache: [...memoryCache.entries()],
|
||||||
|
fileDependencies: [...fileDependencies.entries()],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +106,8 @@ export function addToCompilationCache(payload: any) {
|
|||||||
sourceMaps.set(entry[0], entry[1]);
|
sourceMaps.set(entry[0], entry[1]);
|
||||||
for (const entry of payload.memoryCache)
|
for (const entry of payload.memoryCache)
|
||||||
memoryCache.set(entry[0], entry[1]);
|
memoryCache.set(entry[0], entry[1]);
|
||||||
|
for (const entry of payload.fileDependencies)
|
||||||
|
fileDependencies.set(entry[0], entry[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateCachePath(content: string, filePath: string, isModule: boolean): string {
|
function calculateCachePath(content: string, filePath: string, isModule: boolean): string {
|
||||||
@ -117,3 +121,44 @@ function calculateCachePath(content: string, filePath: string, isModule: boolean
|
|||||||
const fileName = path.basename(filePath, path.extname(filePath)).replace(/\W/g, '') + '_' + hash;
|
const fileName = path.basename(filePath, path.extname(filePath)).replace(/\W/g, '') + '_' + hash;
|
||||||
return path.join(cacheDir, hash[0] + hash[1], fileName);
|
return path.join(cacheDir, hash[0] + hash[1], fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since ESM and CJS collect dependencies differently,
|
||||||
|
// we go via the global state to collect them.
|
||||||
|
let depsCollector: Set<string> | undefined;
|
||||||
|
|
||||||
|
export function startCollectingFileDeps() {
|
||||||
|
depsCollector = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stopCollectingFileDeps(filename: string) {
|
||||||
|
if (!depsCollector)
|
||||||
|
return;
|
||||||
|
depsCollector.delete(filename);
|
||||||
|
const deps = [...depsCollector!].filter(f => !belongsToNodeModules(f));
|
||||||
|
fileDependencies.set(filename, deps);
|
||||||
|
depsCollector = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function currentFileDepsCollector(): Set<string> | undefined {
|
||||||
|
return depsCollector;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fileDependenciesForTest() {
|
||||||
|
return fileDependencies;
|
||||||
|
}
|
||||||
|
|
||||||
|
// These two are only used in the dev mode, they are specifically excluding
|
||||||
|
// files from packages/playwright*. In production mode, node_modules covers
|
||||||
|
// that.
|
||||||
|
const kPlaywrightInternalPrefix = path.resolve(__dirname, '../../../playwright');
|
||||||
|
const kPlaywrightCoveragePrefix = path.resolve(__dirname, '../../../../tests/config/coverage.js');
|
||||||
|
|
||||||
|
export function belongsToNodeModules(file: string) {
|
||||||
|
if (file.includes(`${path.sep}node_modules${path.sep}`))
|
||||||
|
return true;
|
||||||
|
if (file.startsWith(kPlaywrightInternalPrefix))
|
||||||
|
return true;
|
||||||
|
if (file.startsWith(kPlaywrightCoveragePrefix))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@ -16,10 +16,11 @@
|
|||||||
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import type { TestError } from '../../reporter';
|
import type { TestError } from '../../reporter';
|
||||||
import { setCurrentlyLoadingFileSuite } from './globals';
|
import { isWorkerProcess, setCurrentlyLoadingFileSuite } from './globals';
|
||||||
import { Suite } from './test';
|
import { Suite } from './test';
|
||||||
import { requireOrImport } from './transform';
|
import { requireOrImport } from './transform';
|
||||||
import { serializeError } from '../util';
|
import { serializeError } from '../util';
|
||||||
|
import { startCollectingFileDeps, stopCollectingFileDeps } from './compilationCache';
|
||||||
|
|
||||||
export const defaultTimeout = 30000;
|
export const defaultTimeout = 30000;
|
||||||
|
|
||||||
@ -35,6 +36,8 @@ export async function loadTestFile(file: string, rootDir: string, testErrors?: T
|
|||||||
suite.location = { file, line: 0, column: 0 };
|
suite.location = { file, line: 0, column: 0 };
|
||||||
|
|
||||||
setCurrentlyLoadingFileSuite(suite);
|
setCurrentlyLoadingFileSuite(suite);
|
||||||
|
if (!isWorkerProcess())
|
||||||
|
startCollectingFileDeps();
|
||||||
try {
|
try {
|
||||||
await requireOrImport(file);
|
await requireOrImport(file);
|
||||||
cachedFileSuites.set(file, suite);
|
cachedFileSuites.set(file, suite);
|
||||||
@ -43,6 +46,7 @@ export async function loadTestFile(file: string, rootDir: string, testErrors?: T
|
|||||||
throw e;
|
throw e;
|
||||||
testErrors.push(serializeError(e));
|
testErrors.push(serializeError(e));
|
||||||
} finally {
|
} finally {
|
||||||
|
stopCollectingFileDeps(file);
|
||||||
setCurrentlyLoadingFileSuite(undefined);
|
setCurrentlyLoadingFileSuite(undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ import { tsConfigLoader } from '../third_party/tsconfig-loader';
|
|||||||
import Module from 'module';
|
import Module from 'module';
|
||||||
import type { BabelTransformFunction } from './babelBundle';
|
import type { BabelTransformFunction } from './babelBundle';
|
||||||
import { fileIsModule } from '../util';
|
import { fileIsModule } from '../util';
|
||||||
import { getFromCompilationCache } from './compilationCache';
|
import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules } from './compilationCache';
|
||||||
|
|
||||||
type ParsedTsConfigData = {
|
type ParsedTsConfigData = {
|
||||||
absoluteBaseUrl: string;
|
absoluteBaseUrl: string;
|
||||||
@ -180,7 +180,14 @@ export async function requireOrImport(file: string) {
|
|||||||
const esmImport = () => eval(`import(${JSON.stringify(url.pathToFileURL(file))})`);
|
const esmImport = () => eval(`import(${JSON.stringify(url.pathToFileURL(file))})`);
|
||||||
if (isModule)
|
if (isModule)
|
||||||
return await esmImport();
|
return await esmImport();
|
||||||
return require(file);
|
const result = require(file);
|
||||||
|
const depsCollector = currentFileDepsCollector();
|
||||||
|
if (depsCollector) {
|
||||||
|
const module = require.cache[file];
|
||||||
|
if (module)
|
||||||
|
collectCJSDependencies(module, depsCollector);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
revertBabelRequire();
|
revertBabelRequire();
|
||||||
}
|
}
|
||||||
@ -213,6 +220,17 @@ function installTransform(): () => void {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const collectCJSDependencies = (module: Module, dependencies: Set<string>) => {
|
||||||
|
if (dependencies.has(module.filename))
|
||||||
|
return;
|
||||||
|
module.children.forEach(child => {
|
||||||
|
if (!belongsToNodeModules(child.filename)) {
|
||||||
|
dependencies.add(child.filename);
|
||||||
|
collectCJSDependencies(child, dependencies);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export function wrapFunctionWithLocation<A extends any[], R>(func: (location: Location, ...args: A) => R): (...args: A) => R {
|
export function wrapFunctionWithLocation<A extends any[], R>(func: (location: Location, ...args: A) => R): (...args: A) => R {
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
const oldPrepareStackTrace = Error.prepareStackTrace;
|
const oldPrepareStackTrace = Error.prepareStackTrace;
|
||||||
@ -238,20 +256,6 @@ export function wrapFunctionWithLocation<A extends any[], R>(func: (location: Lo
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will catch the playwright-test package as well
|
|
||||||
const kPlaywrightInternalPrefix = path.resolve(__dirname, '../../../playwright');
|
|
||||||
const kPlaywrightCoveragePrefix = path.resolve(__dirname, '../../../../tests/config/coverage.js');
|
|
||||||
|
|
||||||
export function belongsToNodeModules(file: string) {
|
|
||||||
if (file.includes(`${path.sep}node_modules${path.sep}`))
|
|
||||||
return true;
|
|
||||||
if (file.startsWith(kPlaywrightInternalPrefix))
|
|
||||||
return true;
|
|
||||||
if (file.startsWith(kPlaywrightCoveragePrefix))
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isRelativeSpecifier(specifier: string) {
|
function isRelativeSpecifier(specifier: string) {
|
||||||
return specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../');
|
return specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../');
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@
|
|||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import url from 'url';
|
import url from 'url';
|
||||||
import { transformHook, resolveHook, belongsToNodeModules } from './common/transform';
|
import { belongsToNodeModules, currentFileDepsCollector } from './common/compilationCache';
|
||||||
|
import { transformHook, resolveHook } from './common/transform';
|
||||||
|
|
||||||
// Node < 18.6: defaultResolve takes 3 arguments.
|
// Node < 18.6: defaultResolve takes 3 arguments.
|
||||||
// Node >= 18.6: nextResolve from the chain takes 2 arguments.
|
// Node >= 18.6: nextResolve from the chain takes 2 arguments.
|
||||||
@ -27,7 +28,10 @@ async function resolve(specifier: string, context: { parentURL?: string }, defau
|
|||||||
if (resolved !== undefined)
|
if (resolved !== undefined)
|
||||||
specifier = url.pathToFileURL(resolved).toString();
|
specifier = url.pathToFileURL(resolved).toString();
|
||||||
}
|
}
|
||||||
return defaultResolve(specifier, context, defaultResolve);
|
const result = await defaultResolve(specifier, context, defaultResolve);
|
||||||
|
if (result?.url)
|
||||||
|
currentFileDepsCollector()?.add(url.fileURLToPath(result.url));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node < 18.6: defaultLoad takes 3 arguments.
|
// Node < 18.6: defaultLoad takes 3 arguments.
|
||||||
|
24
packages/playwright-test/src/internalsForTest.ts
Normal file
24
packages/playwright-test/src/internalsForTest.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import path from 'path';
|
||||||
|
import { fileDependenciesForTest } from './common/compilationCache';
|
||||||
|
|
||||||
|
export function fileDependencies() {
|
||||||
|
return Object.fromEntries([...fileDependenciesForTest().entries()].map(entry => (
|
||||||
|
[path.basename(entry[0]), entry[1].map(f => path.basename(f))]
|
||||||
|
)));
|
||||||
|
}
|
89
tests/playwright-test/watch.spec.ts
Normal file
89
tests/playwright-test/watch.spec.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { test, expect, stripAnsi } from './playwright-test-fixtures';
|
||||||
|
|
||||||
|
test('should print dependencies in CJS mode', async ({ runInlineTest }) => {
|
||||||
|
const result = await runInlineTest({
|
||||||
|
'playwright.config.ts': `
|
||||||
|
import { defineConfig } from '@playwright/test';
|
||||||
|
export default defineConfig({
|
||||||
|
globalTeardown: './globalTeardown.ts',
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
'helper.ts': `export function foo() {}`,
|
||||||
|
'a.test.ts': `
|
||||||
|
import './helper';
|
||||||
|
pwt.test('passes', () => {});
|
||||||
|
`,
|
||||||
|
'b.test.ts': `
|
||||||
|
import './helper';
|
||||||
|
pwt.test('passes', () => {});
|
||||||
|
`,
|
||||||
|
'globalTeardown.ts': `
|
||||||
|
import { fileDependencies } from '@playwright/test/lib/internalsForTest';
|
||||||
|
export default () => {
|
||||||
|
console.log('###' + JSON.stringify(fileDependencies()) + '###');
|
||||||
|
};
|
||||||
|
`
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
expect(result.passed).toBe(2);
|
||||||
|
const output = stripAnsi(result.output);
|
||||||
|
const deps = JSON.parse(output.match(/###(.*)###/)![1]);
|
||||||
|
expect(deps).toEqual({
|
||||||
|
'a.test.ts': ['helper.ts'],
|
||||||
|
'b.test.ts': ['helper.ts'],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should print dependencies in ESM mode', async ({ runInlineTest, nodeVersion }) => {
|
||||||
|
test.skip(nodeVersion.major < 16);
|
||||||
|
const result = await runInlineTest({
|
||||||
|
'package.json': `{ "type": "module" }`,
|
||||||
|
'playwright.config.ts': `
|
||||||
|
import { defineConfig } from '@playwright/test';
|
||||||
|
export default defineConfig({
|
||||||
|
globalTeardown: './globalTeardown.ts',
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
'helper.ts': `export function foo() {}`,
|
||||||
|
'a.test.ts': `
|
||||||
|
import './helper.js';
|
||||||
|
pwt.test('passes', () => {});
|
||||||
|
`,
|
||||||
|
'b.test.ts': `
|
||||||
|
import './helper.js';
|
||||||
|
pwt.test('passes', () => {});
|
||||||
|
`,
|
||||||
|
'globalTeardown.ts': `
|
||||||
|
import { fileDependencies } from '@playwright/test/lib/internalsForTest';
|
||||||
|
export default () => {
|
||||||
|
console.log('###' + JSON.stringify(fileDependencies()) + '###');
|
||||||
|
};
|
||||||
|
`
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
expect(result.passed).toBe(2);
|
||||||
|
const output = stripAnsi(result.output);
|
||||||
|
const deps = JSON.parse(output.match(/###(.*)###/)![1]);
|
||||||
|
expect(deps).toEqual({
|
||||||
|
'a.test.ts': ['helper.ts'],
|
||||||
|
'b.test.ts': ['helper.ts'],
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user