test: always setUnderTest in index.js, rename to setDevMode (#3662)

Root index.js is only used for local development, so
assuming dev mode there is fine. This way we do not have
to worry about calling setUnderTest early enough.
This commit is contained in:
Dmitry Gozman 2020-08-27 21:08:33 -07:00 committed by GitHub
parent 7444de4b73
commit 5c0f93301d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 20 additions and 31 deletions

View file

@ -14,6 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
const { setDevMode } = require('./lib/utils/utils');
setDevMode(); // Note: we must call setDevMode before initializing.
const { Playwright } = require('./lib/server/playwright'); const { Playwright } = require('./lib/server/playwright');
const { Electron } = require('./lib/server/electron/electron'); const { Electron } = require('./lib/server/electron/electron');
const { setupInProcess } = require('./lib/inprocess'); const { setupInProcess } = require('./lib/inprocess');
@ -21,9 +24,4 @@ const path = require('path');
const playwright = new Playwright(__dirname, require(path.join(__dirname, 'browsers.json'))['browsers']); const playwright = new Playwright(__dirname, require(path.join(__dirname, 'browsers.json'))['browsers']);
playwright.electron = new Electron(); playwright.electron = new Electron();
if (process.env.PWCHANNEL === 'none') { module.exports = setupInProcess(playwright);
playwright._toImpl = x => x;
module.exports = playwright;
} else {
module.exports = setupInProcess(playwright);
}

View file

@ -26,7 +26,7 @@ import { Events } from './events';
import { TimeoutSettings } from '../utils/timeoutSettings'; import { TimeoutSettings } from '../utils/timeoutSettings';
import { Waiter } from './waiter'; import { Waiter } from './waiter';
import { URLMatch, Headers, WaitForEventOptions } from './types'; import { URLMatch, Headers, WaitForEventOptions } from './types';
import { isUnderTest, headersObjectToArray } from '../utils/utils'; import { isDevMode, headersObjectToArray } from '../utils/utils';
export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel, channels.BrowserContextInitializer> { export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel, channels.BrowserContextInitializer> {
_pages = new Set<Page>(); _pages = new Set<Page>();
@ -158,7 +158,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
} }
async setHTTPCredentials(httpCredentials: { username: string, password: string } | null): Promise<void> { async setHTTPCredentials(httpCredentials: { username: string, password: string } | null): Promise<void> {
if (!isUnderTest()) if (!isDevMode())
deprecate(`context.setHTTPCredentials`, `warning: method |context.setHTTPCredentials()| is deprecated. Instead of changing credentials, create another browser context with new credentials.`); deprecate(`context.setHTTPCredentials`, `warning: method |context.setHTTPCredentials()| is deprecated. Instead of changing credentials, create another browser context with new credentials.`);
return this._wrapApiCall('browserContext.setHTTPCredentials', async () => { return this._wrapApiCall('browserContext.setHTTPCredentials', async () => {
await this._channel.setHTTPCredentials({ httpCredentials: httpCredentials || undefined }); await this._channel.setHTTPCredentials({ httpCredentials: httpCredentials || undefined });

View file

@ -20,7 +20,7 @@ import type { Playwright as PlaywrightAPI } from './client/playwright';
import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher'; import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher';
import { Connection } from './client/connection'; import { Connection } from './client/connection';
import { BrowserServerLauncherImpl } from './browserServerImpl'; import { BrowserServerLauncherImpl } from './browserServerImpl';
import { isUnderTest } from './utils/utils'; import { isDevMode } from './utils/utils';
export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI { export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
const clientConnection = new Connection(); const clientConnection = new Connection();
@ -41,7 +41,7 @@ export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
dispatcherConnection.onmessage = message => setImmediate(() => clientConnection.dispatch(message)); dispatcherConnection.onmessage = message => setImmediate(() => clientConnection.dispatch(message));
clientConnection.onmessage = message => setImmediate(() => dispatcherConnection.dispatch(message)); clientConnection.onmessage = message => setImmediate(() => dispatcherConnection.dispatch(message));
if (isUnderTest()) if (isDevMode())
(playwrightAPI as any)._toImpl = (x: any) => dispatcherConnection._dispatchers.get(x._guid)!._object; (playwrightAPI as any)._toImpl = (x: any) => dispatcherConnection._dispatchers.get(x._guid)!._object;
return playwrightAPI; return playwrightAPI;
} }

View file

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { isUnderTest } from '../utils/utils'; import { isDevMode } from '../utils/utils';
export class ValidationError extends Error {} export class ValidationError extends Error {}
export type Validator = (arg: any, path: string) => any; export type Validator = (arg: any, path: string) => any;
@ -81,7 +81,7 @@ export const tObject = (s: { [key: string]: Validator }): Validator => {
if (!Object.is(value, undefined)) if (!Object.is(value, undefined))
result[key] = value; result[key] = value;
} }
if (isUnderTest()) { if (isDevMode()) {
for (const [key, value] of Object.entries(arg)) { for (const [key, value] of Object.entries(arg)) {
if (key.startsWith('__testHook')) if (key.startsWith('__testHook'))
result[key] = value; result[key] = value;

View file

@ -22,7 +22,7 @@ import * as stream from 'stream';
import { helper } from './helper'; import { helper } from './helper';
import { Progress } from './progress'; import { Progress } from './progress';
import * as types from './types'; import * as types from './types';
import { isUnderTest } from '../utils/utils'; import { isDevMode } from '../utils/utils';
export type Env = {[key: string]: string | number | boolean | undefined}; export type Env = {[key: string]: string | number | boolean | undefined};
@ -117,7 +117,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
listeners.push(helper.addEventListener(process, 'SIGINT', () => { listeners.push(helper.addEventListener(process, 'SIGINT', () => {
gracefullyClose().then(() => { gracefullyClose().then(() => {
// Give tests a chance to dispatch any async calls. // Give tests a chance to dispatch any async calls.
if (isUnderTest()) if (isDevMode())
setTimeout(() => process.exit(130), 0); setTimeout(() => process.exit(130), 0);
else else
process.exit(130); process.exit(130);

View file

@ -61,7 +61,7 @@ export function assert(value: any, message?: string): asserts value {
} }
export function debugAssert(value: any, message?: string): asserts value { export function debugAssert(value: any, message?: string): asserts value {
if (isUnderTest() && !value) if (isDevMode() && !value)
throw new Error(message); throw new Error(message);
} }
@ -86,12 +86,12 @@ export function isDebugMode(): boolean {
return isInDebugMode; return isInDebugMode;
} }
let _isUnderTest = false; let _isDevMode = false;
export function setUnderTest() { export function setDevMode() {
_isUnderTest = true; _isDevMode = true;
} }
export function isUnderTest(): boolean { export function isDevMode(): boolean {
return _isUnderTest; return _isDevMode;
} }
export function getFromENV(name: string) { export function getFromENV(name: string) {

View file

@ -7,10 +7,7 @@
}; };
} }
const path = require('path'); const playwright = require(require('path').join(playwrightPath, 'index'));
const { setUnderTest } = require(path.join(playwrightPath, 'lib', 'utils', 'utils'));
setUnderTest();
const playwright = require(path.join(playwrightPath, 'index'));
const browserServer = await playwright[browserTypeName].launchServer(launchOptions); const browserServer = await playwright[browserTypeName].launchServer(launchOptions);
browserServer.on('close', (exitCode, signal) => { browserServer.on('close', (exitCode, signal) => {

View file

@ -22,10 +22,9 @@ import type { LaunchOptions, BrowserType, Browser, BrowserContext, Page, Browser
import { TestServer } from '../utils/testserver'; import { TestServer } from '../utils/testserver';
import { Connection } from '../lib/client/connection'; import { Connection } from '../lib/client/connection';
import { Transport } from '../lib/protocol/transport'; import { Transport } from '../lib/protocol/transport';
import { setUnderTest } from '../lib/utils/utils';
import { installCoverageHooks } from './coverage'; import { installCoverageHooks } from './coverage';
import { parameters, registerFixture, registerWorkerFixture } from '../test-runner'; import { parameters, registerFixture, registerWorkerFixture } from '../test-runner';
import {mkdtempAsync, removeFolderAsync} from './utils'; import { mkdtempAsync, removeFolderAsync } from './utils';
export const options = { export const options = {
CHROMIUM: parameters.browserName === 'chromium', CHROMIUM: parameters.browserName === 'chromium',
@ -113,11 +112,6 @@ registerWorkerFixture('defaultBrowserOptions', async({browserName}, test) => {
}); });
registerWorkerFixture('playwright', async({browserName}, test) => { registerWorkerFixture('playwright', async({browserName}, test) => {
const playwrightCacheEntry = require.cache[require.resolve('../index')];
if (playwrightCacheEntry)
throw new Error('Could not set playwright to test mode because it was required directly from ' + playwrightCacheEntry.parent.id);
setUnderTest(); // Note: we must call setUnderTest before requiring Playwright
const {coverage, uninstall} = installCoverageHooks(browserName); const {coverage, uninstall} = installCoverageHooks(browserName);
if (options.WIRE) { if (options.WIRE) {
const connection = new Connection(); const connection = new Connection();