Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: collect test dependencies (#20645)
Pavel Feldmanb6df48758df6
diff --git a/packages/playwright-test/package.json b/packages/playwright-test/package.jsonindex 54fed7d214..f3e599f3ef 100644--- a/packages/playwright-test/package.json+++ b/packages/playwright-test/package.json@@ -18,6 +18,7 @@"./cli": "./cli.js","./package.json": "./package.json","./lib/cli": "./lib/cli.js",+ "./lib/internalsForTest": "./lib/internalsForTest.js","./lib/experimentalLoader": "./lib/experimentalLoader.js","./lib/mount": "./lib/mount.js","./lib/plugins": "./lib/plugins/index.js",diff --git a/packages/playwright-test/src/common/compilationCache.ts b/packages/playwright-test/src/common/compilationCache.tsindex 44e23a9975..3c4879c433 100644--- a/packages/playwright-test/src/common/compilationCache.ts+++ b/packages/playwright-test/src/common/compilationCache.ts@@ -32,6 +32,7 @@ const cacheDir = process.env.PWTEST_CACHE_DIR || path.join(os.tmpdir(), 'playwriconst sourceMaps: Map<string, string> = new Map();const memoryCache = new Map<string, MemoryCache>();+const fileDependencies = new Map<string, string[]>();Error.stackTraceLimit = 200;@@ -91,6 +92,7 @@ export function serializeCompilationCache(): any {return {sourceMaps: [...sourceMaps.entries()],memoryCache: [...memoryCache.entries()],+ fileDependencies: [...fileDependencies.entries()],};}@@ -104,6 +106,8 @@ export function addToCompilationCache(payload: any) {sourceMaps.set(entry[0], entry[1]);for (const entry of payload.memoryCache)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 {@@ -117,3 +121,44 @@ function calculateCachePath(content: string, filePath: string, isModule: booleanconst fileName = path.basename(filePath, path.extname(filePath)).replace(/\W/g, '') + '_' + hash;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;+}diff --git a/packages/playwright-test/src/common/testLoader.ts b/packages/playwright-test/src/common/testLoader.tsindex 8c9f06e864..08b4cfee88 100644--- a/packages/playwright-test/src/common/testLoader.ts+++ b/packages/playwright-test/src/common/testLoader.ts@@ -16,10 +16,11 @@import path from 'path';import type { TestError } from '../../reporter';-import { setCurrentlyLoadingFileSuite } from './globals';+import { isWorkerProcess, setCurrentlyLoadingFileSuite } from './globals';import { Suite } from './test';import { requireOrImport } from './transform';import { serializeError } from '../util';+import { startCollectingFileDeps, stopCollectingFileDeps } from './compilationCache';export const defaultTimeout = 30000;@@ -35,6 +36,8 @@ export async function loadTestFile(file: string, rootDir: string, testErrors?: Tsuite.location = { file, line: 0, column: 0 };setCurrentlyLoadingFileSuite(suite);+ if (!isWorkerProcess())+ startCollectingFileDeps();try {await requireOrImport(file);cachedFileSuites.set(file, suite);@@ -43,6 +46,7 @@ export async function loadTestFile(file: string, rootDir: string, testErrors?: Tthrow e;testErrors.push(serializeError(e));} finally {+ stopCollectingFileDeps(file);setCurrentlyLoadingFileSuite(undefined);}diff --git a/packages/playwright-test/src/common/transform.ts b/packages/playwright-test/src/common/transform.tsindex 7cdf9944b8..1a63e777c9 100644--- a/packages/playwright-test/src/common/transform.ts+++ b/packages/playwright-test/src/common/transform.ts@@ -24,7 +24,7 @@ import { tsConfigLoader } from '../third_party/tsconfig-loader';import Module from 'module';import type { BabelTransformFunction } from './babelBundle';import { fileIsModule } from '../util';-import { getFromCompilationCache } from './compilationCache';+import { getFromCompilationCache, currentFileDepsCollector, belongsToNodeModules } from './compilationCache';type ParsedTsConfigData = {absoluteBaseUrl: string;@@ -180,7 +180,14 @@ export async function requireOrImport(file: string) {const esmImport = () => eval(`import(${JSON.stringify(url.pathToFileURL(file))})`);if (isModule)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 {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 {return (...args) => {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) {return specifier === '.' || specifier === '..' || specifier.startsWith('./') || specifier.startsWith('../');}diff --git a/packages/playwright-test/src/experimentalLoader.ts b/packages/playwright-test/src/experimentalLoader.tsindex 6e56f555de..f10daec192 100644--- a/packages/playwright-test/src/experimentalLoader.ts+++ b/packages/playwright-test/src/experimentalLoader.ts@@ -16,7 +16,8 @@import fs from 'fs';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: nextResolve from the chain takes 2 arguments.@@ -27,7 +28,10 @@ async function resolve(specifier: string, context: { parentURL?: string }, defauif (resolved !== undefined)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.diff --git a/packages/playwright-test/src/internalsForTest.ts b/packages/playwright-test/src/internalsForTest.tsnew file mode 100644index 0000000000..84b9c0b352--- /dev/null+++ b/packages/playwright-test/src/internalsForTest.ts@@ -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))]+ )));+}diff --git a/tests/playwright-test/watch.spec.ts b/tests/playwright-test/watch.spec.tsnew file mode 100644index 0000000000..f0184d503c--- /dev/null+++ b/tests/playwright-test/watch.spec.ts@@ -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'],+ });+});