[APM] Add skip() method to registry.when (#89572)

Closes #89431.
This commit is contained in:
Dario Gieselaar 2021-01-28 17:43:51 +01:00 committed by GitHub
parent 80b720da11
commit cd9c79bec0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<typeof describe> = (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`);