From cd9c79bec042fc41f488a37a03409b5a55a30b63 Mon Sep 17 00:00:00 2001 From: Dario Gieselaar Date: Thu, 28 Jan 2021 17:43:51 +0100 Subject: [PATCH] [APM] Add skip() method to registry.when (#89572) Closes #89431. --- .../apm_api_integration/common/registry.ts | 100 ++++++++++-------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/x-pack/test/apm_api_integration/common/registry.ts b/x-pack/test/apm_api_integration/common/registry.ts index 8c918eae5a5a..ba43db2fd583 100644 --- a/x-pack/test/apm_api_integration/common/registry.ts +++ b/x-pack/test/apm_api_integration/common/registry.ts @@ -36,55 +36,69 @@ let configName: APMFtrConfigName | undefined; let running: boolean = false; +function when( + title: string, + conditions: RunCondition | RunCondition[], + callback: (condition: RunCondition) => void, + skip?: boolean +) { + const allConditions = castArray(conditions); + + if (!allConditions.length) { + throw new Error('At least one condition should be defined'); + } + + if (running) { + throw new Error("Can't add tests when running"); + } + + const frame = maybe(callsites()[1]); + + const file = frame?.getFileName(); + + if (!file) { + throw new Error('Could not infer file for suite'); + } + + allConditions.forEach((matchedCondition) => { + callbacks.push({ + ...matchedCondition, + runs: [ + { + cb: () => { + const suite: ReturnType = (skip ? describe.skip : describe)( + title, + () => { + callback(matchedCondition); + } + ) as any; + + suite.file = file; + suite.eachTest((test) => { + test.file = file; + }); + }, + }, + ], + }); + }); +} + +when.skip = ( + title: string, + conditions: RunCondition | RunCondition[], + callback: (condition: RunCondition) => void +) => { + when(title, conditions, callback, true); +}; + export const registry = { init: (config: APMFtrConfigName) => { configName = config; callbacks.length = 0; running = false; }, - when: ( - title: string, - conditions: RunCondition | RunCondition[], - callback: (condition: RunCondition) => void - ) => { - const allConditions = castArray(conditions); - - if (!allConditions.length) { - throw new Error('At least one condition should be defined'); - } - - if (running) { - throw new Error("Can't add tests when running"); - } - - const frame = maybe(callsites()[1]); - - const file = frame?.getFileName(); - - if (!file) { - throw new Error('Could not infer file for suite'); - } - - allConditions.forEach((matchedCondition) => { - callbacks.push({ - ...matchedCondition, - runs: [ - { - cb: () => { - const suite = describe(title, () => { - callback(matchedCondition); - }); - - suite.file = file; - suite.eachTest((test) => { - test.file = file; - }); - }, - }, - ], - }); - }); - }, + when, run: (context: FtrProviderContext) => { if (!configName) { throw new Error(`registry was not init() before running`);