Skip to content

Commit

Permalink
test: roll to folio@0.4.0-alpha2
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Apr 22, 2021
1 parent 34e03fc commit a931123
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 223 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"eslint-plugin-notice": "^0.9.10",
"eslint-plugin-react-hooks": "^4.2.0",
"file-loader": "^6.1.0",
"folio": "=0.3.21-alpha",
"folio": "=0.4.0-alpha2",
"formidable": "^1.2.2",
"html-webpack-plugin": "^4.4.1",
"ncp": "^2.0.0",
Expand Down
6 changes: 2 additions & 4 deletions tests/config/android.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import * as folio from 'folio';
import * as path from 'path';
import { test as pageTest } from './pageTest';
import { test as androidTest } from './androidTest';
import { ServerEnv } from './serverEnv';
import { AndroidEnv, AndroidPageEnv } from './androidEnv';

const config: folio.Config = {
Expand All @@ -41,6 +40,5 @@ if (process.env.CI) {
]);
}

const serverEnv = new ServerEnv('10.0.2.2');
pageTest.runWith(folio.merge(serverEnv, new AndroidPageEnv()), { tag: 'android' });
androidTest.runWith(folio.merge(serverEnv, new AndroidEnv()), { tag: 'android' });
pageTest.runWith(new AndroidPageEnv(), { tag: 'android', options: { loopback: '10.0.2.2' } });
androidTest.runWith(new AndroidEnv(), { tag: 'android', options: { loopback: '10.0.2.2' } });
22 changes: 11 additions & 11 deletions tests/config/androidEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { Env, WorkerInfo, TestInfo } from 'folio';
import * as folio from 'folio';
import type { AndroidDevice, BrowserContext } from '../../index';
import * as os from 'os';
import { AndroidTestArgs } from './androidTest';
Expand All @@ -23,11 +23,11 @@ import { PageTestArgs } from './pageTest';
require('../../lib/utils/utils').setUnderTest();
const playwright: typeof import('../../index') = require('../../index');

export class AndroidEnv implements Env<AndroidTestArgs> {
export class AndroidEnv implements folio.Env<AndroidTestArgs> {
protected _device?: AndroidDevice;
protected _browserVersion: string;

async beforeAll(workerInfo: WorkerInfo) {
async setupWorker({}, workerInfo: folio.WorkerInfo) {
this._device = (await playwright._android.devices())[0];
await this._device.shell('am force-stop org.chromium.webview_shell');
await this._device.shell('am force-stop com.android.chrome');
Expand All @@ -40,7 +40,7 @@ export class AndroidEnv implements Env<AndroidTestArgs> {
this._device.setDefaultTimeout(90000);
}

async beforeEach(testInfo: TestInfo) {
async setupTest({}, testInfo: folio.TestInfo) {
// Use chromium screenshots.
testInfo.snapshotPathSegment = 'chromium';
testInfo.data = {
Expand Down Expand Up @@ -70,23 +70,23 @@ export class AndroidEnv implements Env<AndroidTestArgs> {
};
}

async afterAll(workerInfo: WorkerInfo) {
async teardownWorker({}, workerInfo: folio.WorkerInfo) {
if (this._device)
await this._device.close();
this._device = undefined;
}
}

export class AndroidPageEnv extends AndroidEnv implements Env<PageTestArgs> {
export class AndroidPageEnv extends AndroidEnv implements folio.Env<PageTestArgs> {
private _context?: BrowserContext;

async beforeAll(workerInfo: WorkerInfo) {
await super.beforeAll(workerInfo);
async setupWorker(args: any, workerInfo: folio.WorkerInfo) {
await super.setupWorker(args, workerInfo);
this._context = await this._device!.launchBrowser();
}

async beforeEach(testInfo: TestInfo) {
const result = await super.beforeEach(testInfo);
async setupTest(args: any, testInfo: folio.TestInfo) {
const result = await super.setupTest(args, testInfo);
const page = await this._context!.newPage();
return {
...result,
Expand All @@ -96,7 +96,7 @@ export class AndroidPageEnv extends AndroidEnv implements Env<PageTestArgs> {
};
}

async afterEach(testInfo: TestInfo) {
async teardownTest(args: any, testInfo: folio.TestInfo) {
for (const page of this._context!.pages())
await page.close();
}
Expand Down
5 changes: 2 additions & 3 deletions tests/config/androidTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
* limitations under the License.
*/

import { newTestType } from 'folio';
import type { AndroidDevice } from '../../index';
import type { CommonTestArgs } from './pageTest';
import type { ServerTestArgs } from './serverTest';
import { test as base } from './serverTest';
export { expect } from 'folio';

export type AndroidTestArgs = CommonTestArgs & {
androidDevice: AndroidDevice;
};

export const test = newTestType<AndroidTestArgs & ServerTestArgs>();
export const test = base.declare<AndroidTestArgs>();
52 changes: 31 additions & 21 deletions tests/config/browserEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { Env, WorkerInfo, TestInfo } from 'folio';
import * as folio from 'folio';
import type { Browser, BrowserContext, BrowserContextOptions, BrowserType, LaunchOptions } from '../../index';
import { start } from '../../lib/outofprocess';
import { PlaywrightClient } from '../../lib/remote/playwrightClient';
Expand All @@ -27,6 +27,7 @@ import * as childProcess from 'child_process';
import { PlaywrightTestArgs } from './playwrightTest';
import { BrowserTestArgs } from './browserTest';
import { RemoteServer, RemoteServerOptions } from './remoteServer';
import { installCoverageHooks } from './coverage';

const mkdtempAsync = util.promisify(fs.mkdtemp);

Expand All @@ -36,12 +37,13 @@ type TestOptions = {
mode: 'default' | 'driver' | 'service';
video?: boolean;
traceDir?: string;
coverageName?: string;
};

class DriverMode {
private _playwrightObject: any;

async setup(workerInfo: WorkerInfo) {
async setup(workerInfo: folio.WorkerInfo) {
this._playwrightObject = await start();
return this._playwrightObject;
}
Expand All @@ -56,7 +58,7 @@ class ServiceMode {
private _client: any;
private _serviceProcess: childProcess.ChildProcess;

async setup(workerInfo: WorkerInfo) {
async setup(workerInfo: folio.WorkerInfo) {
const port = 10507 + workerInfo.workerIndex;
this._serviceProcess = childProcess.fork(path.join(__dirname, '..', '..', 'lib', 'cli', 'cli.js'), ['run-server', String(port)], {
stdio: 'pipe'
Expand Down Expand Up @@ -88,15 +90,15 @@ class ServiceMode {
}

class DefaultMode {
async setup(workerInfo: WorkerInfo) {
async setup(workerInfo: folio.WorkerInfo) {
return require('../../index');
}

async teardown() {
}
}

export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
export class PlaywrightEnv implements folio.Env<PlaywrightTestArgs> {
private _mode: DriverMode | ServiceMode | DefaultMode;
protected _browserName: BrowserName;
protected _options: LaunchOptions & TestOptions;
Expand All @@ -106,6 +108,7 @@ export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
private _userDataDirs: string[] = [];
private _persistentContext: BrowserContext | undefined;
private _remoteServer: RemoteServer | undefined;
private _coverage: ReturnType<typeof installCoverageHooks>;

constructor(browserName: BrowserName, options: LaunchOptions & TestOptions) {
this._browserName = browserName;
Expand All @@ -117,8 +120,9 @@ export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
}[this._options.mode];
}

async beforeAll(workerInfo: WorkerInfo) {
async setupWorker({}, workerInfo: folio.WorkerInfo) {
require('../../lib/utils/utils').setUnderTest();
this._coverage = installCoverageHooks(this._options.coverageName || this._browserName);
this._playwright = await this._mode.setup(workerInfo);
this._browserType = this._playwright[this._browserName];
this._browserOptions = {
Expand Down Expand Up @@ -156,7 +160,7 @@ export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
return this._remoteServer;
}

async beforeEach(testInfo: TestInfo) {
async setupTest({}, testInfo: folio.TestInfo) {
// Different screenshots per browser.
testInfo.snapshotPathSegment = this._browserName;
testInfo.data = {
Expand Down Expand Up @@ -196,7 +200,7 @@ export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
};
}

async afterEach(testInfo: TestInfo) {
async teardownTest({}, testInfo: folio.TestInfo) {
if (this._persistentContext) {
await this._persistentContext.close();
this._persistentContext = undefined;
Expand All @@ -209,12 +213,18 @@ export class PlaywrightEnv implements Env<PlaywrightTestArgs> {
this._userDataDirs = [];
}

async afterAll(workerInfo: WorkerInfo) {
async teardownWorker({}, workerInfo: folio.WorkerInfo) {
await this._mode.teardown();
const { coverage, uninstall } = this._coverage;
uninstall();
const coveragePath = path.join(__dirname, '..', 'coverage-report', workerInfo.workerIndex + '.json');
const coverageJSON = Array.from(coverage.keys()).filter(key => coverage.get(key));
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
}
}

export class BrowserEnv extends PlaywrightEnv implements Env<BrowserTestArgs> {
export class BrowserEnv extends PlaywrightEnv implements folio.Env<BrowserTestArgs> {
private _browser: Browser | undefined;
private _contextOptions: BrowserContextOptions;
private _contexts: BrowserContext[] = [];
Expand All @@ -225,15 +235,15 @@ export class BrowserEnv extends PlaywrightEnv implements Env<BrowserTestArgs> {
this._contextOptions = options;
}

async beforeAll(workerInfo: WorkerInfo) {
await super.beforeAll(workerInfo);
async setupWorker(args: any, workerInfo: folio.WorkerInfo) {
await super.setupWorker(args, workerInfo);
this._browser = await this._browserType.launch(this._browserOptions);
this._browserVersion = this._browser.version();
}

async beforeEach(testInfo: TestInfo) {
const result = await super.beforeEach(testInfo);
const debugName = path.relative(testInfo.config.outputDir, testInfo.outputPath('')).replace(/[\/\\]/g, '-');
async setupTest(args: any, testInfo: folio.TestInfo) {
const result = await super.setupTest(args, testInfo);
const debugName = path.relative(testInfo.config.outputDir, testInfo.outputDir).replace(/[\/\\]/g, '-');
const contextOptions = {
recordVideo: this._options.video ? { dir: testInfo.outputPath('') } : undefined,
_traceDir: this._options.traceDir,
Expand All @@ -257,24 +267,24 @@ export class BrowserEnv extends PlaywrightEnv implements Env<BrowserTestArgs> {
};
}

async afterEach(testInfo: TestInfo) {
async teardownTest(args: any, testInfo: folio.TestInfo) {
for (const context of this._contexts)
await context.close();
this._contexts = [];
await super.afterEach(testInfo);
await super.teardownTest(args, testInfo);
}

async afterAll(workerInfo: WorkerInfo) {
async teardownWorker(args: any, workerInfo: folio.WorkerInfo) {
if (this._browser)
await this._browser.close();
this._browser = undefined;
await super.afterAll(workerInfo);
await super.teardownWorker(args, workerInfo);
}
}

export class PageEnv extends BrowserEnv {
async beforeEach(testInfo: TestInfo) {
const result = await super.beforeEach(testInfo);
async setupTest(args: any, testInfo: folio.TestInfo) {
const result = await super.setupTest(args, testInfo);
const context = await result.contextFactory();
const page = await context.newPage();
return {
Expand Down
7 changes: 3 additions & 4 deletions tests/config/browserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* limitations under the License.
*/

import { newTestType } from 'folio';
import { test as base } from './serverTest';
import type { Browser, BrowserContextOptions, BrowserContext } from '../../index';
import type { PlaywrightTestArgs } from './playwrightTest';
import type { ServerTestArgs } from './serverTest';
export { expect } from 'folio';

export type BrowserTestArgs = PlaywrightTestArgs & {
Expand All @@ -26,5 +25,5 @@ export type BrowserTestArgs = PlaywrightTestArgs & {
contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;
};

export const test = newTestType<BrowserTestArgs & ServerTestArgs>();
export const slowTest = newTestType<BrowserTestArgs & ServerTestArgs>();
export const test = base.declare<BrowserTestArgs>();
export const slowTest = base.declare<BrowserTestArgs>();
Loading

0 comments on commit a931123

Please sign in to comment.