From 3fa4260453b7d5f34cdf708e4824add857da98e3 Mon Sep 17 00:00:00 2001 From: emilioalvap Date: Mon, 16 Dec 2024 15:14:50 +0100 Subject: [PATCH] Fix backwards compatibility to 1.14 --- src/core/index.ts | 4 +- src/core/runner.ts | 135 ++++++++++++++++++++++++++++++++++----------- src/dsl/journey.ts | 64 ++++++++++++++++++++- src/dsl/step.ts | 8 +++ 4 files changed, 176 insertions(+), 35 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index 56151d5e..2dafa5ce 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -30,7 +30,7 @@ import { JourneyWithAnnotations, StepWithAnnotations, } from '../dsl'; -import { runner } from "./globals" +import { runner as runnerGlobal } from './globals'; import { VoidCallback, HooksCallback, Location } from '../common_types'; import { wrapFnWithLocation } from '../helpers'; import { log } from './logger'; @@ -112,3 +112,5 @@ export const after = (callback: HooksCallback) => { } return runner.currentJourney._addHook('after', callback); }; + +export const runner = runnerGlobal; diff --git a/src/core/runner.ts b/src/core/runner.ts index 58a8bd48..9629f17c 100644 --- a/src/core/runner.ts +++ b/src/core/runner.ts @@ -45,10 +45,7 @@ import { StepResult, PushOptions, } from '../common_types'; -import { - PerformanceManager, - filterBrowserMessages, -} from '../plugins'; +import { PerformanceManager, filterBrowserMessages } from '../plugins'; import { Gatherer } from './gatherer'; import { log } from './logger'; import { Monitor, MonitorConfig } from '../dsl/monitor'; @@ -104,7 +101,7 @@ export default class Runner implements RunnerInfo { type: 'jpeg', quality: 80, timeout: 5000, - }) + }); /** * Write the screenshot image buffer with additional details (step * information) which could be extracted at the end of @@ -131,8 +128,20 @@ export default class Runner implements RunnerInfo { this.#hooks[type].push(callback); } + /** + * @deprecated Since version 1.17.0. Use _updateMonitor instead. + * Alias _addHook for backwards compatibility + */ + addHook(type: HookType, callback: HooksCallback) { + this._addHook(type, callback); + } + private buildHookArgs() { - return { env: this.config.environment, params: this.config.params, info: this as RunnerInfo }; + return { + env: this.config.environment, + params: this.config.params, + info: this as RunnerInfo, + }; } _updateMonitor(config: MonitorConfig) { @@ -143,11 +152,27 @@ export default class Runner implements RunnerInfo { this.#monitor.update(config); } + /** + * @deprecated Since version 1.17.0. Use _updateMonitor instead. + * Alias _addJourney for backwards compatibility + */ + updateMonitor(config: MonitorConfig) { + this._updateMonitor(config); + } + _addJourney(journey: Journey) { this.#journeys.push(journey); this.#currentJourney = journey; } + /** + * @deprecated Since version 1.17.0. Use _addJourney instead. + * Alias _addJourney for backwards compatibility + */ + addJourney(journey: Journey) { + this._addJourney(journey); + } + private setReporter(options: RunOptions) { /** * Set up the corresponding reporter and fallback @@ -181,10 +206,7 @@ export default class Runner implements RunnerInfo { await runParallel(journey._getHook('after'), args); } - async #runStep( - step: Step, - options: RunOptions - ): Promise { + async #runStep(step: Step, options: RunOptions): Promise { log(`Runner: start step (${step.name})`); const { metrics, screenshots, filmstrips, trace } = options; /** @@ -212,7 +234,7 @@ export default class Runner implements RunnerInfo { // invoke the step callback by extracting to a variable to get better stack trace const cb = step.cb; await cb(); - step.status = "succeeded" + step.status = 'succeeded'; } catch (error) { step.status = 'failed'; step.error = error; @@ -245,13 +267,10 @@ export default class Runner implements RunnerInfo { } } log(`Runner: end step (${step.name})`); - return data + return data; } - async #runSteps( - journey: Journey, - options: RunOptions - ) { + async #runSteps(journey: Journey, options: RunOptions) { const results: Array = []; const isOnlyExists = journey.steps.filter(s => s.only).length > 0; let skipStep = false; @@ -297,7 +316,7 @@ export default class Runner implements RunnerInfo { * caching all screenshots and clear them at end of each journey */ await mkdir(this.#screenshotPath, { recursive: true }); - const params = options.params + const params = options.params; this.#reporter?.onJourneyStart?.(journey, { timestamp: getTimestamp(), params, @@ -315,7 +334,10 @@ export default class Runner implements RunnerInfo { ) { // Enhance the journey results const pOutput = await Gatherer.pluginManager.output(); - const bConsole = filterBrowserMessages(pOutput.browserconsole, journey.status); + const bConsole = filterBrowserMessages( + pOutput.browserconsole, + journey.status + ); await this.#reporter?.onJourneyEnd?.(journey, { browserDelay: this.#browserDelay, timestamp: getTimestamp(), @@ -324,7 +346,7 @@ export default class Runner implements RunnerInfo { browserconsole: bConsole, }); await Gatherer.endRecording(); - await Gatherer.dispose(this.#driver) + await Gatherer.dispose(this.#driver); // clear screenshots cache after each journey await rm(this.#screenshotPath, { recursive: true, force: true }); return Object.assign(result, { @@ -361,12 +383,16 @@ export default class Runner implements RunnerInfo { this.#currentJourney = journey; log(`Runner: start journey (${journey.name})`); let result: JourneyResult = {}; - const hookArgs = { env: options.environment, params: options.params, info: this }; + const hookArgs = { + env: options.environment, + params: options.params, + info: this, + }; try { await this.#startJourney(journey, options); await this.#runBeforeHook(journey, hookArgs); const stepResults = await this.#runSteps(journey, options); - journey.status = "succeeded"; + journey.status = 'succeeded'; // Mark journey as failed if any one of the step fails for (const step of journey.steps) { if (step.status === 'failed') { @@ -385,13 +411,21 @@ export default class Runner implements RunnerInfo { await this.#runAfterHook(journey, hookArgs).catch(e => { journey.status = 'failed'; journey.error = e; - }) + }); result = await this.#endJourney(journey, result, options); } log(`Runner: end journey (${journey.name})`); return result; } + /** + * @deprecated Since version 1.17.0. Use _runJourney instead. + * Alias _runJourney for backwards compatibility + */ + runJourney(journey: Journey, options: RunOptions) { + this._runJourney(journey, options); + } + _buildMonitors(options: PushOptions) { /** * Update the global monitor configuration required for setting defaults @@ -426,15 +460,12 @@ export default class Runner implements RunnerInfo { * - filter out monitors based on matched tags and name after applying both * global and local monitor configurations */ - journey.cb({ params: options.params } as any); - const monitor = journey._getMonitor(); + + // TODO: Fix backwards compatibility with 1.14 and prior + (journey.cb ?? journey.callback)({ params: options.params } as any); + const monitor = journey.monitor ?? journey?._getMonitor(); monitor.update(this.#monitor?.config); - if ( - !monitor.isMatch( - options.grepOpts?.match, - options.grepOpts?.tags - ) - ) { + if (!monitor.isMatch(options.grepOpts?.match, options.grepOpts?.tags)) { continue; } monitor.validate(); @@ -443,6 +474,14 @@ export default class Runner implements RunnerInfo { return monitors; } + /** + * @deprecated Since version 1.17.0. Use _buildMonitors instead. + * Alias _buildMonitors for backwards compatibility + */ + buildMonitors(options: PushOptions) { + this._buildMonitors(options); + } + async #init(options: RunOptions) { this.setReporter(options); this.#reporter.onStart?.({ @@ -474,20 +513,34 @@ export default class Runner implements RunnerInfo { this.#journeys = onlyJournerys; } else { // filter journeys based on tags and skip annotations - this.#journeys = this.#journeys.filter(j => j._isMatch(grepOpts?.match, grepOpts?.tags) && !j.skip); + this.#journeys = this.#journeys.filter( + j => j._isMatch(grepOpts?.match, grepOpts?.tags) && !j.skip + ); } // Used by heartbeat to gather all registered journeys if (dryRun) { - this.#journeys.forEach(journey => this.#reporter.onJourneyRegister?.(journey)) + this.#journeys.forEach(journey => + this.#reporter.onJourneyRegister?.(journey) + ); } else if (this.#journeys.length > 0) { result = await this._runJourneys(options); } - await this.#runAfterAllHook(hookArgs).catch(async () => await this._reset()); + await this.#runAfterAllHook(hookArgs).catch( + async () => await this._reset() + ); await this._reset(); return result; } + /** + * @deprecated Since version 1.17.0. Use _run instead. + * Alias _run for backwards compatibility + */ + run(options: RunOptions): Promise { + return this._run(options); + } + async _runJourneys(options: RunOptions) { const result: RunResult = {}; const browserStart = monotonicTimeInSeconds(); @@ -504,6 +557,14 @@ export default class Runner implements RunnerInfo { return result; } + /** + * @deprecated Since version 1.17.0. Use _runJourneys instead. + * Alias _runJourneys for backwards compatibility + */ + runJourneys(options: RunOptions): Promise { + return this._runJourneys(options); + } + async _reset() { this.#currentJourney = null; this.#journeys = []; @@ -515,4 +576,12 @@ export default class Runner implements RunnerInfo { await rm(CACHE_PATH, { recursive: true, force: true }); await this.#reporter?.onEnd?.(); } + + /** + * @deprecated Since version 1.17.0. Use _reset instead. + * Alias _reset for backwards compatibility + */ + reset() { + return this._reset(); + } } diff --git a/src/dsl/journey.ts b/src/dsl/journey.ts index 21bdd5ec..046b1ef2 100644 --- a/src/dsl/journey.ts +++ b/src/dsl/journey.ts @@ -31,7 +31,13 @@ import { APIRequestContext, } from 'playwright-core'; import { Step } from './step'; -import { VoidCallback, HooksCallback, Params, Location, StatusValue } from '../common_types'; +import { + VoidCallback, + HooksCallback, + Params, + Location, + StatusValue, +} from '../common_types'; import { Monitor, MonitorConfig } from './monitor'; import { isMatch } from '../helpers'; import { RunnerInfo } from '../core/runner'; @@ -90,18 +96,50 @@ export class Journey { return step; } + /** + * @deprecated Since version 1.17.0. Use _addStep instead. + * Alias _addStep for backwards compatibility + */ + addStep(name: string, cb: VoidCallback, location?: Location) { + this._addStep(name, cb, location); + } + _addHook(type: HookType, cb: HooksCallback) { this.#hooks[type].push(cb); } + /** + * @deprecated Since version 1.17.0. Use _addHook instead. + * Alias _addHook for backwards compatibility + */ + addHook(type: HookType, cb: HooksCallback) { + this._addHook(type, cb); + } + _getHook(type: HookType) { return this.#hooks[type]; } + /** + * @deprecated Since version 1.17.0. Use _getHook instead. + * Alias _getHook for backwards compatibility + */ + getHook(type: HookType) { + this._getHook(type); + } + _getMonitor() { return this.#monitor; } + /** + * @deprecated Since version 1.17.0. Use _getHook instead. + * Alias _getHook for backwards compatibility + */ + get monitor() { + return this._getMonitor(); + } + _updateMonitor(config: MonitorConfig) { /** * Use defaults values from journey for monitor object (id, name and tags) @@ -117,16 +155,40 @@ export class Journey { this.#monitor.setFilter({ match: this.name }); } + /** + * @deprecated Since version 1.17.0. Use _updateMonitor instead. + * Alias _updateMonitor for backwards compatibility + */ + updateMonitor(config: MonitorConfig) { + this._updateMonitor(config); + } + get cb() { return this.#cb; } + /** + * @deprecated Since version 1.17.0. Use cb instead. + * Alias cb for backwards compatibility + */ + get callback() { + return this.#cb; + } + /** * Matches journeys based on the provided args. Proitize tags over match */ _isMatch(matchPattern: string, tagsPattern: Array) { return isMatch(this.tags, this.name, tagsPattern, matchPattern); } + + /** + * @deprecated Since version 1.17.0. Use _isMatch instead. + * Alias _isMatch for backwards compatibility + */ + get isMatch() { + return this.#cb; + } } type JourneyType = ( diff --git a/src/dsl/step.ts b/src/dsl/step.ts index e943779d..d533d235 100644 --- a/src/dsl/step.ts +++ b/src/dsl/step.ts @@ -54,6 +54,14 @@ export class Step { get cb() { return this.#cb; } + + /** + * @deprecated Since version 1.17.0. Use cb instead. + * Alias cb for backwards compatibility + */ + get callback() { + return this.#cb; + } } type StepType = (name: string, callback: VoidCallback) => Step;