diff --git a/packages/kbn-test/src/functional_tests/lib/index.js b/packages/kbn-test/src/functional_tests/lib/index.ts similarity index 85% rename from packages/kbn-test/src/functional_tests/lib/index.js rename to packages/kbn-test/src/functional_tests/lib/index.ts index 8581ceecdf6d..93700a692dc8 100644 --- a/packages/kbn-test/src/functional_tests/lib/index.js +++ b/packages/kbn-test/src/functional_tests/lib/index.ts @@ -8,6 +8,6 @@ export { runKibanaServer } from './run_kibana_server'; export { runElasticsearch } from './run_elasticsearch'; -export { runFtr, hasTests, assertNoneExcluded } from './run_ftr'; +export { runFtr, hasTests, assertNoneExcluded, CreateFtrOptions, CreateFtrParams } from './run_ftr'; export { KIBANA_ROOT, KIBANA_FTR_SCRIPT, FUNCTIONAL_CONFIG_PATH, API_CONFIG_PATH } from './paths'; export { runCli } from './run_cli'; diff --git a/packages/kbn-test/src/functional_tests/lib/run_cli.js b/packages/kbn-test/src/functional_tests/lib/run_cli.ts similarity index 89% rename from packages/kbn-test/src/functional_tests/lib/run_cli.js rename to packages/kbn-test/src/functional_tests/lib/run_cli.ts index 9de9d7300531..3e2cb50ff2e7 100644 --- a/packages/kbn-test/src/functional_tests/lib/run_cli.js +++ b/packages/kbn-test/src/functional_tests/lib/run_cli.ts @@ -10,16 +10,18 @@ import { inspect } from 'util'; import chalk from 'chalk'; import getopts from 'getopts'; - +/* eslint-disable no-console */ export class CliError extends Error { - constructor(message, exitCode = 1) { + constructor(message: string) { super(message); - this.exitCode = exitCode; Error.captureStackTrace(this, CliError); } } -export async function runCli(getHelpText, run) { +export async function runCli( + getHelpText: () => string, + run: (options: getopts.ParsedOptions) => Promise +) { try { const userOptions = getopts(process.argv.slice(2)) || {}; if (userOptions.help) { diff --git a/packages/kbn-test/src/functional_tests/lib/run_elasticsearch.ts b/packages/kbn-test/src/functional_tests/lib/run_elasticsearch.ts index da83d8285a6b..68e7a4992fcf 100644 --- a/packages/kbn-test/src/functional_tests/lib/run_elasticsearch.ts +++ b/packages/kbn-test/src/functional_tests/lib/run_elasticsearch.ts @@ -14,7 +14,7 @@ import { createTestEsCluster } from '../../es'; interface RunElasticsearchOptions { log: ToolingLog; - esFrom: string; + esFrom?: string; } export async function runElasticsearch({ config, diff --git a/packages/kbn-test/src/functional_tests/lib/run_ftr.js b/packages/kbn-test/src/functional_tests/lib/run_ftr.ts similarity index 66% rename from packages/kbn-test/src/functional_tests/lib/run_ftr.js rename to packages/kbn-test/src/functional_tests/lib/run_ftr.ts index 40937b8b4fc2..f9e109928ddc 100644 --- a/packages/kbn-test/src/functional_tests/lib/run_ftr.js +++ b/packages/kbn-test/src/functional_tests/lib/run_ftr.ts @@ -5,14 +5,37 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { ToolingLog } from '@kbn/dev-utils'; import { FunctionalTestRunner, readConfigFile } from '../../functional_test_runner'; import { CliError } from './run_cli'; +export interface CreateFtrOptions { + /** installation dir from which to run Kibana */ + installDir: string; + log: ToolingLog; + /** Whether to exit test run at the first failure */ + bail?: boolean; + grep: string; + updateBaselines?: boolean; + suiteFiles?: { + include?: string[]; + exclude?: string[]; + }; + suiteTags?: { + include?: string[]; + exclude?: string[]; + }; + updateSnapshots?: boolean; +} + +export interface CreateFtrParams { + configPath: string; + options: CreateFtrOptions; +} async function createFtr({ configPath, options: { installDir, log, bail, grep, updateBaselines, suiteFiles, suiteTags, updateSnapshots }, -}) { +}: CreateFtrParams) { const config = await readConfigFile(log, configPath); return { @@ -28,18 +51,18 @@ async function createFtr({ updateBaselines, updateSnapshots, suiteFiles: { - include: [...suiteFiles.include, ...config.get('suiteFiles.include')], - exclude: [...suiteFiles.exclude, ...config.get('suiteFiles.exclude')], + include: [...(suiteFiles?.include || []), ...config.get('suiteFiles.include')], + exclude: [...(suiteFiles?.exclude || []), ...config.get('suiteFiles.exclude')], }, suiteTags: { - include: [...suiteTags.include, ...config.get('suiteTags.include')], - exclude: [...suiteTags.exclude, ...config.get('suiteTags.exclude')], + include: [...(suiteTags?.include || []), ...config.get('suiteTags.include')], + exclude: [...(suiteTags?.exclude || []), ...config.get('suiteTags.exclude')], }, }), }; } -export async function assertNoneExcluded({ configPath, options }) { +export async function assertNoneExcluded({ configPath, options }: CreateFtrParams) { const { config, ftr } = await createFtr({ configPath, options }); if (config.get('testRunner')) { @@ -61,7 +84,7 @@ export async function assertNoneExcluded({ configPath, options }) { } } -export async function runFtr({ configPath, options }) { +export async function runFtr({ configPath, options }: CreateFtrParams) { const { ftr } = await createFtr({ configPath, options }); const failureCount = await ftr.run(); @@ -72,7 +95,7 @@ export async function runFtr({ configPath, options }) { } } -export async function hasTests({ configPath, options }) { +export async function hasTests({ configPath, options }: CreateFtrParams) { const { ftr, config } = await createFtr({ configPath, options }); if (config.get('testRunner')) { diff --git a/packages/kbn-test/src/functional_tests/lib/run_kibana_server.js b/packages/kbn-test/src/functional_tests/lib/run_kibana_server.ts similarity index 69% rename from packages/kbn-test/src/functional_tests/lib/run_kibana_server.js rename to packages/kbn-test/src/functional_tests/lib/run_kibana_server.ts index 6f91304fffa2..6305e522b392 100644 --- a/packages/kbn-test/src/functional_tests/lib/run_kibana_server.js +++ b/packages/kbn-test/src/functional_tests/lib/run_kibana_server.ts @@ -5,11 +5,12 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { ProcRunner } from '@kbn/dev-utils'; import { resolve, relative } from 'path'; import { KIBANA_ROOT, KIBANA_EXEC, KIBANA_EXEC_PATH } from './paths'; +import type { Config } from '../../functional_test_runner'; -function extendNodeOptions(installDir) { +function extendNodeOptions(installDir?: string) { if (!installDir) { return {}; } @@ -26,7 +27,15 @@ function extendNodeOptions(installDir) { }; } -export async function runKibanaServer({ procs, config, options }) { +export async function runKibanaServer({ + procs, + config, + options, +}: { + procs: ProcRunner; + config: Config; + options: { installDir?: string; extraKbnOpts?: string[] }; +}) { const { installDir } = options; const runOptions = config.get('kbnTestServer.runOptions'); const env = config.get('kbnTestServer.env'); @@ -45,7 +54,7 @@ export async function runKibanaServer({ procs, config, options }) { }); } -function getKibanaCmd(installDir) { +function getKibanaCmd(installDir?: string) { if (installDir) { return process.platform.startsWith('win') ? resolve(installDir, 'bin/kibana.bat') @@ -61,14 +70,17 @@ function getKibanaCmd(installDir) { * passed, we run from source code. We also allow passing in extra * Kibana server options, so we tack those on here. */ -function collectCliArgs(config, { installDir, extraKbnOpts }) { - const buildArgs = config.get('kbnTestServer.buildArgs') || []; - const sourceArgs = config.get('kbnTestServer.sourceArgs') || []; - const serverArgs = config.get('kbnTestServer.serverArgs') || []; +function collectCliArgs( + config: Config, + { installDir, extraKbnOpts }: { installDir?: string; extraKbnOpts?: string[] } +) { + const buildArgs: string[] = config.get('kbnTestServer.buildArgs') || []; + const sourceArgs: string[] = config.get('kbnTestServer.sourceArgs') || []; + const serverArgs: string[] = config.get('kbnTestServer.serverArgs') || []; return pipe( serverArgs, - (args) => (installDir ? args.filter((a) => a !== '--oss') : args), + (args) => (installDir ? args.filter((a: string) => a !== '--oss') : args), (args) => (installDir ? [...buildArgs, ...args] : [KIBANA_EXEC_PATH, ...sourceArgs, ...args]), (args) => args.concat(extraKbnOpts || []) ); @@ -78,7 +90,7 @@ function collectCliArgs(config, { installDir, extraKbnOpts }) { * Filter the cli args to remove duplications and * overridden options */ -function filterCliArgs(args) { +function filterCliArgs(args: string[]) { return args.reduce((acc, val, ind) => { // If original argv has a later basepath setting, skip this val. if (isBasePathSettingOverridden(args, val, ind)) { @@ -95,7 +107,7 @@ function filterCliArgs(args) { } return [...acc, val]; - }, []); + }, [] as string[]); } /** @@ -103,7 +115,7 @@ function filterCliArgs(args) { * previous function. The first function's input * is the arr array. */ -function pipe(arr, ...fns) { +function pipe(arr: any[], ...fns: Array<(...args: any[]) => any>) { return fns.reduce((acc, fn) => { return fn(acc); }, arr); @@ -113,16 +125,16 @@ function pipe(arr, ...fns) { * Checks whether a specific parameter is allowed to appear multiple * times in the Kibana parameters. */ -function allowsDuplicate(val) { +function allowsDuplicate(val: string) { return ['--plugin-path'].includes(val.split('=')[0]); } -function isBasePathSettingOverridden(args, val, ind) { +function isBasePathSettingOverridden(args: string[], val: string, index: number) { const key = val.split('=')[0]; const basePathKeys = ['--no-base-path', '--server.basePath']; if (basePathKeys.includes(key)) { - if (findIndexFrom(args, ++ind, (opt) => basePathKeys.includes(opt.split('=')[0])) > -1) { + if (findIndexFrom(args, ++index, (opt) => basePathKeys.includes(opt.split('=')[0])) > -1) { return true; } } @@ -130,6 +142,6 @@ function isBasePathSettingOverridden(args, val, ind) { return false; } -function findIndexFrom(array, index, ...args) { - return [...array].slice(index).findIndex(...args); +function findIndexFrom(array: string[], index: number, predicate: (element: string) => boolean) { + return [...array].slice(index).findIndex(predicate); } diff --git a/packages/kbn-test/src/functional_tests/tasks.js b/packages/kbn-test/src/functional_tests/tasks.ts similarity index 76% rename from packages/kbn-test/src/functional_tests/tasks.js rename to packages/kbn-test/src/functional_tests/tasks.ts index 4d12fb5ea5ec..76443293be75 100644 --- a/packages/kbn-test/src/functional_tests/tasks.js +++ b/packages/kbn-test/src/functional_tests/tasks.ts @@ -9,7 +9,7 @@ import { relative } from 'path'; import * as Rx from 'rxjs'; import { startWith, switchMap, take } from 'rxjs/operators'; -import { withProcRunner } from '@kbn/dev-utils'; +import { withProcRunner, ToolingLog } from '@kbn/dev-utils'; import dedent from 'dedent'; import { @@ -19,13 +19,14 @@ import { assertNoneExcluded, hasTests, KIBANA_FTR_SCRIPT, + CreateFtrOptions, } from './lib'; import { readConfigFile } from '../functional_test_runner/lib'; -const makeSuccessMessage = (options) => { +const makeSuccessMessage = (options: StartServerOptions) => { const installDirFlag = options.installDir ? ` --kibana-install-dir=${options.installDir}` : ''; - const configPaths = Array.isArray(options.config) ? options.config : [options.config]; + const configPaths: string[] = Array.isArray(options.config) ? options.config : [options.config]; const pathsMessage = options.useDefaultConfig ? '' : configPaths @@ -47,14 +48,17 @@ const makeSuccessMessage = (options) => { /** * Run servers and tests for each config - * @param {object} options Optional - * @property {string[]} options.configs Array of paths to configs - * @property {function} options.log An instance of the ToolingLog - * @property {string} options.installDir Optional installation dir from which to run Kibana - * @property {boolean} options.bail Whether to exit test run at the first failure - * @property {string} options.esFrom Optionally run from source instead of snapshot */ -export async function runTests(options) { +interface RunTestsParams extends CreateFtrOptions { + /** Array of paths to configs */ + configs: string[]; + /** run from source instead of snapshot */ + esFrom?: string; + createLogger: () => ToolingLog; + extraKbnOpts: string[]; + assertNoneExcluded: boolean; +} +export async function runTests(options: RunTestsParams) { if (!process.env.KBN_NP_PLUGINS_BUILT && !options.assertNoneExcluded) { const log = options.createLogger(); log.warning('❗️❗️❗️'); @@ -88,6 +92,7 @@ export async function runTests(options) { continue; } + // eslint-disable-next-line no-console console.log(`--- Running ${relative(process.cwd(), configPath)}`); await withProcRunner(log, async (procs) => { @@ -117,15 +122,20 @@ export async function runTests(options) { } } -/** - * Start only servers using single config - * @param {object} options Optional - * @property {string} options.config Path to a config file - * @property {function} options.log An instance of the ToolingLog - * @property {string} options.installDir Optional installation dir from which to run Kibana - * @property {string} options.esFrom Optionally run from source instead of snapshot - */ -export async function startServers(options) { +interface StartServerOptions { + /** Path to a config file */ + config: string; + log: ToolingLog; + /** installation dir from which to run Kibana */ + installDir?: string; + /** run from source instead of snapshot */ + esFrom?: string; + createLogger: () => ToolingLog; + extraKbnOpts: string[]; + useDefaultConfig?: boolean; +} + +export async function startServers(options: StartServerOptions) { const log = options.createLogger(); const opts = { ...options, @@ -158,7 +168,7 @@ export async function startServers(options) { }); } -async function silence(log, milliseconds) { +async function silence(log: ToolingLog, milliseconds: number) { await log .getWritten$() .pipe( diff --git a/packages/kbn-test/src/functional_tests/test_helpers.js b/packages/kbn-test/src/functional_tests/test_helpers.ts similarity index 90% rename from packages/kbn-test/src/functional_tests/test_helpers.js rename to packages/kbn-test/src/functional_tests/test_helpers.ts index eebd3851bf4e..4131f23770a0 100644 --- a/packages/kbn-test/src/functional_tests/test_helpers.js +++ b/packages/kbn-test/src/functional_tests/test_helpers.ts @@ -10,7 +10,7 @@ import { format } from 'util'; -export function checkMockConsoleLogSnapshot(logMock) { +export function checkMockConsoleLogSnapshot(logMock: jest.Mock) { const output = logMock.mock.calls .reduce((acc, args) => `${acc}${format(...args)}\n`, '') .replace(/(^ at.+[>)\d]$\n?)+/m, ' ...stack trace...');