Skip to content

Commit

Permalink
test: always setUnderTest in index.js, rename to setDevMode (#3662)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgozman authored Aug 28, 2020
1 parent 7444de4 commit 5c0f933
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 31 deletions.
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
* 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 { Electron } = require('./lib/server/electron/electron');
const { setupInProcess } = require('./lib/inprocess');
const path = require('path');

const playwright = new Playwright(__dirname, require(path.join(__dirname, 'browsers.json'))['browsers']);
playwright.electron = new Electron();
if (process.env.PWCHANNEL === 'none') {
playwright._toImpl = x => x;
module.exports = playwright;
} else {
module.exports = setupInProcess(playwright);
}
module.exports = setupInProcess(playwright);
4 changes: 2 additions & 2 deletions src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Events } from './events';
import { TimeoutSettings } from '../utils/timeoutSettings';
import { Waiter } from './waiter';
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> {
_pages = new Set<Page>();
Expand Down Expand Up @@ -158,7 +158,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
}

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.`);
return this._wrapApiCall('browserContext.setHTTPCredentials', async () => {
await this._channel.setHTTPCredentials({ httpCredentials: httpCredentials || undefined });
Expand Down
4 changes: 2 additions & 2 deletions src/inprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { Playwright as PlaywrightAPI } from './client/playwright';
import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher';
import { Connection } from './client/connection';
import { BrowserServerLauncherImpl } from './browserServerImpl';
import { isUnderTest } from './utils/utils';
import { isDevMode } from './utils/utils';

export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
const clientConnection = new Connection();
Expand All @@ -41,7 +41,7 @@ export function setupInProcess(playwright: PlaywrightImpl): PlaywrightAPI {
dispatcherConnection.onmessage = message => setImmediate(() => clientConnection.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;
return playwrightAPI;
}
4 changes: 2 additions & 2 deletions src/protocol/validatorPrimitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { isUnderTest } from '../utils/utils';
import { isDevMode } from '../utils/utils';

export class ValidationError extends Error {}
export type Validator = (arg: any, path: string) => any;
Expand Down Expand Up @@ -81,7 +81,7 @@ export const tObject = (s: { [key: string]: Validator }): Validator => {
if (!Object.is(value, undefined))
result[key] = value;
}
if (isUnderTest()) {
if (isDevMode()) {
for (const [key, value] of Object.entries(arg)) {
if (key.startsWith('__testHook'))
result[key] = value;
Expand Down
4 changes: 2 additions & 2 deletions src/server/processLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as stream from 'stream';
import { helper } from './helper';
import { Progress } from './progress';
import * as types from './types';
import { isUnderTest } from '../utils/utils';
import { isDevMode } from '../utils/utils';

export type Env = {[key: string]: string | number | boolean | undefined};

Expand Down Expand Up @@ -117,7 +117,7 @@ export async function launchProcess(options: LaunchProcessOptions): Promise<Laun
listeners.push(helper.addEventListener(process, 'SIGINT', () => {
gracefullyClose().then(() => {
// Give tests a chance to dispatch any async calls.
if (isUnderTest())
if (isDevMode())
setTimeout(() => process.exit(130), 0);
else
process.exit(130);
Expand Down
12 changes: 6 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function assert(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);
}

Expand All @@ -86,12 +86,12 @@ export function isDebugMode(): boolean {
return isInDebugMode;
}

let _isUnderTest = false;
export function setUnderTest() {
_isUnderTest = true;
let _isDevMode = false;
export function setDevMode() {
_isDevMode = true;
}
export function isUnderTest(): boolean {
return _isUnderTest;
export function isDevMode(): boolean {
return _isDevMode;
}

export function getFromENV(name: string) {
Expand Down
5 changes: 1 addition & 4 deletions test/fixtures/closeme.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
};
}

const path = require('path');
const { setUnderTest } = require(path.join(playwrightPath, 'lib', 'utils', 'utils'));
setUnderTest();
const playwright = require(path.join(playwrightPath, 'index'));
const playwright = require(require('path').join(playwrightPath, 'index'));

const browserServer = await playwright[browserTypeName].launchServer(launchOptions);
browserServer.on('close', (exitCode, signal) => {
Expand Down
8 changes: 1 addition & 7 deletions test/playwright.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ import type { LaunchOptions, BrowserType, Browser, BrowserContext, Page, Browser
import { TestServer } from '../utils/testserver';
import { Connection } from '../lib/client/connection';
import { Transport } from '../lib/protocol/transport';
import { setUnderTest } from '../lib/utils/utils';
import { installCoverageHooks } from './coverage';
import { parameters, registerFixture, registerWorkerFixture } from '../test-runner';
import {mkdtempAsync, removeFolderAsync} from './utils';
import { mkdtempAsync, removeFolderAsync } from './utils';

export const options = {
CHROMIUM: parameters.browserName === 'chromium',
Expand Down Expand Up @@ -113,11 +112,6 @@ registerWorkerFixture('defaultBrowserOptions', 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);
if (options.WIRE) {
const connection = new Connection();
Expand Down

0 comments on commit 5c0f933

Please sign in to comment.