From 373042ed897103526700e79171862d10dd93a86b Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 6 Jan 2022 09:29:05 -0800 Subject: [PATCH] chore: don't allow importing @playwright/test twice (#11218) --- packages/playwright-test/src/index.ts | 12 ++++++++++++ tests/config/baseTest.ts | 4 ++-- tests/config/serverFixtures.ts | 7 +++---- tests/playwright-test/playwright-test-fixtures.ts | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) 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) - ._extendTest(serverTest) + .extend(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 = { +export const serverFixtures: Fixtures = { 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); 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) - .extend(serverFixtures as any) + .extend(serverFixtures) .extend({ writeFiles: async ({}, use, testInfo) => { await use(files => writeFiles(testInfo, files));