Skip to content

Commit

Permalink
FTR support mocha dry run (elastic#125915)
Browse files Browse the repository at this point in the history
This PR adds support for the mocha CLI flag `--dry-run` in our functional test execution.
  • Loading branch information
pheyos authored Feb 23, 2022
1 parent 3195a54 commit b7db60a
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 188 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@
"@types/mime-types": "^2.1.0",
"@types/minimatch": "^2.0.29",
"@types/minimist": "^1.2.1",
"@types/mocha": "^8.2.0",
"@types/mocha": "^9.1.0",
"@types/mock-fs": "^4.13.1",
"@types/moment-timezone": "^0.5.12",
"@types/mustache": "^0.8.31",
Expand Down Expand Up @@ -775,7 +775,7 @@
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^24.5.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-mocha": "^9.0.0",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-no-unsanitized": "^3.1.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prefer-object-spread": "^1.2.1",
Expand Down Expand Up @@ -832,10 +832,10 @@
"micromatch": "3.1.10",
"minimist": "^1.2.5",
"mkdirp": "0.5.1",
"mocha": "^8.2.1",
"mocha-junit-reporter": "^2.0.0",
"mochawesome": "^6.2.1",
"mochawesome-merge": "^4.2.0",
"mocha": "^9.1.0",
"mocha-junit-reporter": "^2.0.2",
"mochawesome": "^7.0.1",
"mochawesome-merge": "^4.2.1",
"mock-fs": "^5.1.2",
"mock-http-server": "1.3.0",
"ms-chromium-edge-driver": "^0.4.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-test/src/functional_test_runner/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function runFtrCli() {
{
mochaOpts: {
bail: flags.bail,
dryRun: flags['dry-run'],
grep: flags.grep || undefined,
invert: flags.invert,
},
Expand Down Expand Up @@ -148,6 +149,7 @@ export function runFtrCli() {
'u',
'throttle',
'headless',
'dry-run',
],
default: {
config: 'test/functional/config.js',
Expand All @@ -174,6 +176,7 @@ export function runFtrCli() {
--kibana-install-dir directory where the Kibana install being tested resides
--throttle enable network throttling in Chrome browser
--headless run browser in headless mode
--dry-run report tests without executing them
`,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* Side Public License, v 1.
*/

import Path from 'path';
import { ToolingLog } from '@kbn/dev-utils';
import { REPO_ROOT } from '@kbn/utils';

import { Suite, Test } from './fake_mocha_types';
import {
Expand Down Expand Up @@ -74,7 +76,28 @@ export class FunctionalTestRunner {
return (await providers.invokeProviderFn(customTestRunner)) || 0;
}

const mocha = await setupMocha(this.lifecycle, this.log, config, providers, this.esVersion);
let reporter;
let reporterOptions;
if (config.get('mochaOpts.dryRun')) {
// override default reporter for dryRun results
const targetFile = Path.resolve(REPO_ROOT, 'target/functional-tests/dryRunOutput.json');
reporter = 'json';
reporterOptions = {
output: targetFile,
};
this.log.info(`Dry run results will be stored in ${targetFile}`);
}

const mocha = await setupMocha(
this.lifecycle,
this.log,
config,
providers,
this.esVersion,
reporter,
reporterOptions
);

await this.lifecycle.beforeTests.trigger(mocha.suite);
this.log.info('Starting tests');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const schema = Joi.object()
mochaOpts: Joi.object()
.keys({
bail: Joi.boolean().default(false),
dryRun: Joi.boolean().default(false),
grep: Joi.string(),
invert: Joi.boolean().default(false),
slow: Joi.number().default(30000),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ import { validateCiGroupTags } from './validate_ci_group_tags';
* @param {EsVersion} esVersion
* @return {Promise<Mocha>}
*/
export async function setupMocha(lifecycle, log, config, providers, esVersion) {
export async function setupMocha(
lifecycle,
log,
config,
providers,
esVersion,
reporter,
reporterOptions
) {
// configure mocha
const mocha = new Mocha({
...config.get('mochaOpts'),
reporter: await providers.loadExternalService('mocha reporter', MochaReporterProvider),
reporter:
reporter || (await providers.loadExternalService('mocha reporter', MochaReporterProvider)),
reporterOptions,
});

// global beforeEach hook in root suite triggers before all others
Expand Down

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

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

6 changes: 6 additions & 0 deletions packages/kbn-test/src/functional_tests/cli/run_tests/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const options = {
debug: { desc: 'Run in debug mode.' },
quiet: { desc: 'Only log errors.' },
silent: { desc: 'Log nothing.' },
'dry-run': { desc: 'Report tests without executing them.' },
};

export function displayHelp() {
Expand Down Expand Up @@ -136,6 +137,11 @@ export function processOptions(userOptions, defaultConfigPaths) {
userOptions.assertNoneExcluded = !!userOptions['assert-none-excluded'];
delete userOptions['assert-none-excluded'];

if (userOptions['dry-run']) {
userOptions.dryRun = userOptions['dry-run'];
delete userOptions['dry-run'];
}

function createLogger() {
return new ToolingLog({
level: pickLevelFromFlags(userOptions),
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-test/src/functional_tests/lib/run_ftr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface CreateFtrOptions {
};
updateSnapshots?: boolean;
esVersion: EsVersion;
dryRun?: boolean;
}

export interface CreateFtrParams {
Expand All @@ -45,6 +46,7 @@ async function createFtr({
suiteTags,
updateSnapshots,
esVersion,
dryRun,
},
}: CreateFtrParams) {
const config = await readConfigFile(log, esVersion, configPath);
Expand All @@ -58,6 +60,7 @@ async function createFtr({
mochaOpts: {
bail: !!bail,
grep,
dryRun: !!dryRun,
},
kbnTestServer: {
installDir,
Expand Down
Loading

0 comments on commit b7db60a

Please sign in to comment.