Skip to content

Commit

Permalink
feat(test-runner): add manual testing and open browser options
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsDenBakker committed Oct 2, 2020
1 parent 43d3681 commit fcc2e28
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/orange-carpets-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@web/test-runner-cli': patch
'@web/test-runner-core': patch
'@web/test-runner': patch
---

added manual testing and open browser options
5 changes: 5 additions & 0 deletions docs/docs/test-runner/cli-and-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ interface TestRunnerConfig {
// configuration for code coverage
coverageConfig?: CoverageConfig;

/** Starts test runner in manual testing mode. Ignores browsers option and prints manual testing URL. */
manual?: boolean;
/** Opens browser for manual testing. Requires the manual option to be set. */
open?: boolean;

// how long a browser can take to start up before failing. defaults to 30000
browserStartTimeout?: number;
// how long a test file can take to load. defaults to 10000
Expand Down
28 changes: 19 additions & 9 deletions packages/test-runner-cli/src/cli/TestRunnerCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import { TestRunnerLogger } from '../logger/TestRunnerLogger';
import { BufferedLogger } from '../reporter/BufferedLogger';
import { getManualDebugMenu } from './getManualDebugMenu';

export type MenuType = 'overview' | 'focus' | 'debug' | 'manual-debug';
export type MenuType = 'none' | 'overview' | 'focus' | 'debug' | 'manual-debug';

export const MENUS = {
NONE: 'none' as MenuType,
OVERVIEW: 'overview' as MenuType,
FOCUS_SELECT_FILE: 'focus' as MenuType,
DEBUG_SELECT_FILE: 'debug' as MenuType,
Expand All @@ -41,7 +42,7 @@ export class TestRunnerCli {
private terminal = new DynamicTerminal();
private reportedFilesByTestRun = new Map<number, Set<string>>();
private sessions: TestSessionManager;
private activeMenu: MenuType = MENUS.OVERVIEW;
private activeMenu: MenuType = MENUS.NONE;
private menuSucceededAndPendingFiles: string[] = [];
private menuFailedFiles: string[] = [];
private testCoverage?: TestCoverage;
Expand All @@ -67,9 +68,6 @@ export class TestRunnerCli {
this.setupRunnerEvents();

this.terminal.start();
if (this.config.watch) {
this.terminal.clear();
}

for (const reporter of this.config.reporters) {
reporter.start?.({
Expand All @@ -81,16 +79,19 @@ export class TestRunnerCli {
});
}

this.reportTestResults();
this.reportTestProgress();
this.switchMenu(this.config.manual ? MENUS.MANUAL_DEBUG : MENUS.OVERVIEW);

if (this.config.watch) {
if (this.config.watch || (this.config.manual && this.terminal.isInteractive)) {
this.terminal.observeDirectInput();
}

if (this.config.staticLogging || !this.terminal.isInteractive) {
this.logger.log(chalk.bold(`Running ${this.runner.testFiles.length} test files...\n`));
}

if (this.config.open) {
openBrowser(this.localAddress);
}
}

private setupTerminalEvents() {
Expand All @@ -107,7 +108,12 @@ export class TestRunnerCli {
case KEYCODES.CTRL_C:
case KEYCODES.CTRL_D:
case 'Q':
this.runner.stop();
if (
this.activeMenu === MENUS.OVERVIEW ||
(this.config.manual && this.activeMenu === MENUS.MANUAL_DEBUG)
) {
this.runner.stop();
}
return;
case 'D':
if (this.activeMenu === MENUS.OVERVIEW) {
Expand Down Expand Up @@ -297,6 +303,10 @@ export class TestRunnerCli {
}

private reportTestProgress(final = false) {
if (this.config.manual) {
return;
}

const logStatic = this.config.staticLogging || !this.terminal.isInteractive;
if (logStatic && !final) {
// print a static progress log only once every 10000ms
Expand Down
2 changes: 1 addition & 1 deletion packages/test-runner-cli/src/cli/getManualDebugMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export function getManualDebugMenu(config: TestRunnerCoreConfig): string[] {
`Network address: ${chalk.cyanBright(networkAddress)}`,
' ',
`${chalk.gray('Press')} D ${chalk.gray('to open the browser.')}`,
`${chalk.gray('Press')} ESC ${chalk.gray('to exit manual debug.')}`,
`${chalk.gray('Press')} ${config.manual ? 'Q' : 'ESC'} ${chalk.gray('to exit manual debug.')}`,
].filter(_ => !!_);
}
11 changes: 11 additions & 0 deletions packages/test-runner-cli/src/config/readCliArgsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ const defaultOptions: OptionDefinition[] = [
type: Boolean,
description: 'Disables rendering a progress bar dynamically to the terminal.',
},
{
name: 'manual',
type: Boolean,
description:
'Starts test runner in manual testing mode. Ignores browsers option and prints manual testing URL.',
},
{
name: 'open',
type: Boolean,
description: 'Opens browser for manual testing. Requires the manual option to be set.',
},
{
name: 'port',
type: Number,
Expand Down
5 changes: 5 additions & 0 deletions packages/test-runner-core/src/config/TestRunnerCoreConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export interface TestRunnerCoreConfig {
testsFinishTimeout: number;
staticLogging?: boolean;

/** Ignores browsers option and prints manual testing URL. */
manual?: boolean;
/** Opens browser for manual testing. Requires the manual option to be set. */
open?: boolean;

debug?: boolean;
mimeTypes?: Record<string, string>;
plugins?: TestRunnerPlugin[];
Expand Down
25 changes: 16 additions & 9 deletions packages/test-runner-core/src/runner/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export class TestRunner extends EventEmitter<EventMap> {
groupConfigs,
);
this.config = config;

if (this.config.manual && this.config.watch) {
throw new Error('Cannot combine the manual and watch options.');
}

this.testFiles = testFiles;
this.browsers = browsers;
this.browserNames = Array.from(new Set(this.browsers.map(b => b.name)));
Expand Down Expand Up @@ -73,17 +78,19 @@ export class TestRunner extends EventEmitter<EventMap> {
this.started = true;
this.startTime = Date.now();

for (const browser of this.browsers) {
if (browser.initialize) {
await browser.initialize(this.config, this.testFiles);
}
}
// the browser names can be updated after initialize
this.browserNames = Array.from(new Set(this.browsers.map(b => b.name)));

await this.server.start();

this.runTests(this.sessions.all());
if (!this.config.manual) {
for (const browser of this.browsers) {
if (browser.initialize) {
await browser.initialize(this.config, this.testFiles);
}
}

// the browser names can be updated after initialize
this.browserNames = Array.from(new Set(this.browsers.map(b => b.name)));
this.runTests(this.sessions.all());
}
} catch (error) {
this.stop(error);
}
Expand Down

0 comments on commit fcc2e28

Please sign in to comment.