Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(test runner): process-wide cache of required test files (#11230)

Dmitry Gozman19460d50e49f
diff --git a/packages/playwright-test/src/loader.ts b/packages/playwright-test/src/loader.ts
index 21abc096ac..b4caa51850 100644
--- a/packages/playwright-test/src/loader.ts
+++ b/packages/playwright-test/src/loader.ts
@@ -29,6 +29,11 @@ import { BuiltInReporter, builtInReporters } from './runner';
import { isRegExp } from 'playwright-core/lib/utils/utils';
import { tsConfigLoader, TsConfigLoaderResult } from './third_party/tsconfig-loader';
+// To allow multiple loaders in the same process without clearing require cache,
+// we make these maps global.
+const cachedFileSuites = new Map<string, Suite>();
+const cachedTSConfigs = new Map<string, TsConfigLoaderResult>();
+
export class Loader {
private _defaultConfig: Config;
private _configOverrides: Config;
@@ -36,9 +41,7 @@ export class Loader {
private _config: Config = {};
private _configFile: string | undefined;
private _projects: ProjectImpl[] = [];
- private _fileSuites = new Map<string, Suite>();
private _lastModuleInfo: { rootFolder: string, isModule: boolean } | null = null;
- private _tsConfigCache = new Map<string, TsConfigLoaderResult>();
constructor(defaultConfig: Config, configOverrides: Config) {
this._defaultConfig = defaultConfig;
@@ -113,15 +116,15 @@ export class Loader {
}
async loadTestFile(file: string) {
- if (this._fileSuites.has(file))
- return this._fileSuites.get(file)!;
+ if (cachedFileSuites.has(file))
+ return cachedFileSuites.get(file)!;
try {
const suite = new Suite(path.relative(this._fullConfig.rootDir, file) || path.basename(file));
suite._requireFile = file;
suite.location = { file, line: 0, column: 0 };
setCurrentlyLoadingFileSuite(suite);
await this._requireOrImport(file);
- this._fileSuites.set(file, suite);
+ cachedFileSuites.set(file, suite);
return suite;
} finally {
setCurrentlyLoadingFileSuite(undefined);
@@ -154,10 +157,6 @@ export class Loader {
return this._projects;
}
- fileSuites() {
- return this._fileSuites;
- }
-
serialize(): SerializedLoaderData {
return {
defaultConfig: this._defaultConfig,
@@ -197,13 +196,13 @@ export class Loader {
private async _requireOrImport(file: string) {
// Respect tsconfig paths.
const cwd = path.dirname(file);
- let tsconfig = this._tsConfigCache.get(cwd);
+ let tsconfig = cachedTSConfigs.get(cwd);
if (!tsconfig) {
tsconfig = tsConfigLoader({
getEnv: (name: string) => process.env[name],
cwd
});
- this._tsConfigCache.set(cwd, tsconfig);
+ cachedTSConfigs.set(cwd, tsconfig);
}
const revertBabelRequire = installTransform(tsconfig);
diff --git a/packages/playwright-test/src/runner.ts b/packages/playwright-test/src/runner.ts
index 27ce6ff227..ce41ff9a91 100644
--- a/packages/playwright-test/src/runner.ts
+++ b/packages/playwright-test/src/runner.ts
@@ -227,12 +227,9 @@ export class Runner {
if (config.globalSetup && !list)
globalSetupResult = await (await this._loader.loadGlobalHook(config.globalSetup, 'globalSetup'))(this._loader.fullConfig());
try {
- for (const file of allTestFiles)
- await this._loader.loadTestFile(file);
-
const preprocessRoot = new Suite('');
- for (const fileSuite of this._loader.fileSuites().values())
- preprocessRoot._addSuite(fileSuite);
+ for (const file of allTestFiles)
+ preprocessRoot._addSuite(await this._loader.loadTestFile(file));
if (config.forbidOnly) {
const onlyTestsAndSuites = preprocessRoot._getOnlyItems();
if (onlyTestsAndSuites.length > 0) {