Skip to content

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

chore: don't allow importing @playwright/test twice (#11218)

Pavel Feldman373042ed8971
diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts
index 367f1d9c35..53a602091f 100644
--- a/packages/playwright-test/src/index.ts
+++ b/packages/playwright-test/src/index.ts
@@ -25,6 +25,18 @@ import { prependToTestError } from './util';
export { expect } from './expect';
export const _baseTest: TestType<{}, {}> = rootTestType.test;
+if ((process as any)['__pw_initiator__']) {
+ const originalStackTraceLimit = Error.stackTraceLimit;
+ Error.stackTraceLimit = 200;
+ try {
+ throw new Error('Requiring @playwright/test second time, \nFirst:\n' + (process as any)['__pw_initiator__'] + '\n\nSecond: ');
+ } finally {
+ Error.stackTraceLimit = originalStackTraceLimit;
+ }
+} else {
+ (process as any)['__pw_initiator__'] = new Error().stack;
+}
+
type TestFixtures = PlaywrightTestArgs & PlaywrightTestOptions & {
_combinedContextOptions: BrowserContextOptions,
_setupContextOptionsAndArtifacts: void;
diff --git a/tests/config/baseTest.ts b/tests/config/baseTest.ts
index bddb5a6e80..9d4bf8a857 100644
--- a/tests/config/baseTest.ts
+++ b/tests/config/baseTest.ts
@@ -16,7 +16,7 @@
import { test, TestType, Fixtures } from '@playwright/test';
import { commonFixtures, CommonFixtures } from './commonFixtures';
-import { serverTest } from './serverFixtures';
+import { serverFixtures, ServerFixtures, ServerWorkerOptions } from './serverFixtures';
import { coverageTest } from './coverageFixtures';
import { platformTest } from './platformFixtures';
import { testModeTest } from './testModeFixtures';
@@ -34,7 +34,7 @@ export const baseTest = base
._extendTest(platformTest)
._extendTest(testModeTest)
.extend<CommonFixtures>(commonFixtures)
- ._extendTest(serverTest)
+ .extend<ServerFixtures, ServerWorkerOptions>(serverFixtures)
.extend<{}, { _snapshotSuffix: string }>({
_snapshotSuffix: ['', { scope: 'worker' }],
});
diff --git a/tests/config/serverFixtures.ts b/tests/config/serverFixtures.ts
index cc87258418..095a7e5e07 100644
--- a/tests/config/serverFixtures.ts
+++ b/tests/config/serverFixtures.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { test, Fixtures } from '@playwright/test';
+import type { Fixtures } from '@playwright/test';
import path from 'path';
import socks from 'socksv5';
import { TestServer } from '../../utils/testserver';
@@ -22,6 +22,7 @@ import { TestProxy } from './proxy';
export type ServerWorkerOptions = {
loopback?: string;
+ __servers: ServerFixtures & { socksServer: socks.SocksServer };
};
export type ServerFixtures = {
@@ -32,8 +33,7 @@ export type ServerFixtures = {
asset: (p: string) => string;
};
-export type ServersInternal = ServerFixtures & { socksServer: socks.SocksServer };
-export const serverFixtures: Fixtures<ServerFixtures, { __servers: ServersInternal } & ServerWorkerOptions> = {
+export const serverFixtures: Fixtures<ServerFixtures, ServerWorkerOptions> = {
loopback: [ undefined, { scope: 'worker', option: true } ],
__servers: [ async ({ loopback }, run, workerInfo) => {
const assetsPath = path.join(__dirname, '..', 'assets');
@@ -111,4 +111,3 @@ export const serverFixtures: Fixtures<ServerFixtures, { __servers: ServersIntern
},
};
-export const serverTest = test.extend<ServerFixtures, ServerWorkerOptions & { __servers: ServersInternal }>(serverFixtures);
diff --git a/tests/playwright-test/playwright-test-fixtures.ts b/tests/playwright-test/playwright-test-fixtures.ts
index 69e3eea895..850218b735 100644
--- a/tests/playwright-test/playwright-test-fixtures.ts
+++ b/tests/playwright-test/playwright-test-fixtures.ts
@@ -197,7 +197,7 @@ type Fixtures = {
export const test = base
.extend<CommonFixtures>(commonFixtures)
- .extend<ServerFixtures, ServerWorkerOptions>(serverFixtures as any)
+ .extend<ServerFixtures, ServerWorkerOptions>(serverFixtures)
.extend<Fixtures>({
writeFiles: async ({}, use, testInfo) => {
await use(files => writeFiles(testInfo, files));