Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for capabilities flag #295

Merged
merged 5 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ describe('CLI', () => {
message: 'Cannot add property foo, object is not extensible',
});
});

it('support capability flag', async () => {
const cli = new CLIMock([
join(FIXTURES_DIR, 'example.journey.ts'),
'-j',
'--capability',
'metrics',
]);
await cli.waitFor('step/end');
const output = JSON.parse(cli.output());
expect(output.payload.metrics).toBeDefined();
expect(await cli.exitCode).toBe(0);
});

it('show warn for unknown capability flag', async () => {
const cli = new CLIMock([
join(FIXTURES_DIR, 'fake.journey.ts'),
'-j',
'--capability',
'unknown',
]);
try {
await cli.exitCode;
} catch (e) {
expect(e.message).toMatch('Missing capability "unknown"');
}
});
});

class CLIMock {
Expand Down
42 changes: 42 additions & 0 deletions __tests__/fixtures/example.journey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* MIT License
*
* Copyright (c) 2020-present, Elastic NV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

import { journey, step, beforeAll, afterAll } from '../../';
import { Server } from '../utils/server';

let server: Server;

beforeAll(async () => {
server = await Server.create();
});
afterAll(async () => {
await server.close();
});

journey('example journey', ({ page }) => {
step('go to test page', async () => {
await page.goto(server.TEST_PAGE);
});
});
5 changes: 3 additions & 2 deletions src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export type PluginOutput = {
};

export type CliArgs = {
capability?: Array<string>;
config?: string;
environment?: string;
outfd?: number;
Expand All @@ -132,8 +133,8 @@ export type CliArgs = {
json?: boolean;
pattern?: string;
inline: boolean;
require: string[];
require: Array<string>;
debug?: boolean;
suiteParams?: string;
richEvents?: true;
richEvents?: boolean;
};
1 change: 1 addition & 0 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type RunOptions = Omit<
| 'suiteParams'
| 'reporter'
| 'richEvents'
| 'capability'
> & {
params?: Params;
reporter?: CliArgs['reporter'] | Reporter;
Expand Down
53 changes: 36 additions & 17 deletions src/parse_args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
*
*/

import { program } from 'commander';
import { program, Option } from 'commander';
import { CliArgs } from './common_types';
import { reporters } from './reporters';

const availableReporters = Object.keys(reporters)
.map(r => String(r))
.join();

/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const { name, version } = require('../package.json');

program
.name(`npx ${name}`)
.usage('[options] [dir] [files] file')
Expand All @@ -43,9 +40,10 @@ program
.option('-s, --suite-params <jsonstring>', 'suite variables', '{}')
.option('-e, --environment <envname>', 'e.g. production', 'development')
.option('-j, --json', 'output newline delimited JSON')
.option(
'--reporter <reporter>',
`output repoter format, can be one of ${availableReporters}`
.addOption(
new Option('--reporter <value>', `output repoter format`).choices(
Object.keys(reporters)
)
)
.option('-d, --debug', 'print debug logs info')
.option(
Expand All @@ -58,18 +56,11 @@ program
.option('--sandbox', 'enable chromium sandboxing')
.option('--rich-events', 'Mimics a heartbeat run')
.option(
'--ws-endpoint <endpoint>',
'Browser WebSocket endpoint to connect to'
)
.option(
'--pause-on-error',
'pause on error until a keypress is made in the console. Useful during development'
'--capability <features...>',
'Enable capabilities through feature flags'
)
.option('--screenshots', 'take screenshot for each step')
.option('--filmstrips', 'record detailed filmstrip info for all journeys')
.option('--trace', 'record chrome trace events for all journeys')
.option('--network', 'capture network information for all journeys')
.option('--metrics', 'capture performance metrics for each step')
.option(
'--dry-run',
"don't actually execute anything, report only registered journeys"
Expand All @@ -80,6 +71,14 @@ program
'specify a file descriptor for logs. Default is stdout',
parseInt
)
.option(
'--ws-endpoint <endpoint>',
'Browser WebSocket endpoint to connect to'
)
.option(
'--pause-on-error',
'pause on error until a keypress is made in the console. Useful during development'
)
.version(version)
.description('Run synthetic tests');

Expand All @@ -96,5 +95,25 @@ if (options.richEvents) {
options.network = true;
}

if (options.capability) {
const supportedCapabilities = ['trace', 'filmstrips', 'metrics'];
/**
* trace - record chrome trace events(LCP, FCP, CLS, etc.) for all journeys
* filmstrips - record detailed filmstrips for all journeys
* metrics - capture performance metrics (DOM Nodes, Heap size, etc.) for each step
*/
for (const flag of options.capability) {
if (supportedCapabilities.includes(flag)) {
options[flag] = true;
} else {
console.warn(
`Missing capability "${flag}", current supported capabilities are ${supportedCapabilities.join(
''
)}`
);
}
}
}

export { options };
export default command;