Skip to content

Commit

Permalink
remove logic from wrapFnWithLocation fn and add Suite abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiqueclarke committed Feb 15, 2023
1 parent 191bb58 commit 03897ef
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 27 deletions.
33 changes: 8 additions & 25 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@
* THE SOFTWARE.
*
*/
import { Journey, JourneyCallback, JourneyOptions } from '../dsl';
import { Journey, JourneyCallback, JourneyOptions, Suite } from '../dsl';
import Runner from './runner';
import { VoidCallback, HooksCallback, Location } from '../common_types';
import { normalizeOptions } from '../options';
import { wrapFnWithLocation } from '../helpers';
import { getCombinations, getCombinationName } from '../matrix';
import { log } from './logger';
import { MonitorConfig } from '../dsl/monitor';


/* TODO: Testing
* Local vs global matrix: Local matrix fully overwrites global matrix, rather than merging
* Adjustments: Duplicates in adjustments do not run extra journeys
*
* Regular params are combina
/**
* Use a gloabl Runner which would be accessed by the runtime and
Expand All @@ -57,27 +55,12 @@ export const journey = wrapFnWithLocation(
if (typeof options === 'string') {
options = { name: options, id: options };
}
const { matrix: globalMatrix } = normalizeOptions({});
const { matrix: journeyMatrix } = options;
if (!globalMatrix && !journeyMatrix) {
const j = new Journey(options, callback, location);
runner.addJourney(j);
return j;
}

// local journey matrix takes priority over global matrix
const matrix = journeyMatrix || globalMatrix;

if (!matrix.values) {
throw new Error('Please specify values for your testing matrix');
}

const combinations = getCombinations(matrix);
combinations.forEach(matrixParams => {
const name = getCombinationName((options as JourneyOptions)?.name, matrixParams);
const j = new Journey({...options as JourneyOptions, name}, callback, location, matrixParams);
runner.addJourney(j);
})
const suite = new Suite(location);
const j = new Journey(options, callback, location);
suite.addJourney(j);
runner.addJourney(j);
runner.addSuite(suite);
return j;
}
);

Expand Down
51 changes: 49 additions & 2 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { join } from 'path';
import { mkdir, rm, writeFile } from 'fs/promises';
import { Journey } from '../dsl/journey';
import { Journey, Suite } from '../dsl/journey';
import { Step } from '../dsl/step';
import { reporters, Reporter } from '../reporters';
import {
Expand All @@ -35,6 +35,7 @@ import {
runParallel,
generateUniqueId,
} from '../helpers';
import { getCombinationName, getCombinations } from '../matrix';
import {
HooksCallback,
Params,
Expand All @@ -44,6 +45,7 @@ import {
RunOptions,
JourneyResult,
StepResult,
Location,
} from '../common_types';
import { PluginManager } from '../plugins';
import { PerformanceManager } from '../plugins';
Expand All @@ -68,6 +70,7 @@ export default class Runner {
#reporter: Reporter;
#currentJourney?: Journey = null;
journeys: Journey[] = [];
suites: Map<Location, Suite> = new Map();
hooks: SuiteHooks = { beforeAll: [], afterAll: [] };
hookError: Error | undefined;
monitor?: Monitor;
Expand Down Expand Up @@ -142,6 +145,10 @@ export default class Runner {
this.#currentJourney = journey;
}

addSuite(suite: Suite) {
this.suites.set(suite.location, suite);
}

setReporter(options: RunOptions) {
/**
* Set up the corresponding reporter and fallback
Expand Down Expand Up @@ -421,6 +428,38 @@ export default class Runner {
return monitors;
}

async parseMatrix(options: RunOptions) {
this.journeys.forEach(journey => {
const { matrix: globalMatrix } = options;
const { matrix: localMatrix } = journey;
// local journey matrix takes priority over global matrix
const matrix = localMatrix || globalMatrix;

if (!matrix) {
return;
}

if (!matrix.values) {
throw new Error('Please specify values for your testing matrix');
}

const suite = this.suites.get(journey.location);
suite.clearJourneys();

const combinations = getCombinations(matrix);
combinations.forEach(matrixParams => {
const j = journey.clone();
const name = getCombinationName(j.name, matrixParams);
j.name = name;
j.id = name;
j.params = matrixParams;
this.addJourney(j);
suite.addJourney(j);
});
})

}

async run(options: RunOptions) {
const result: RunResult = {};
if (this.#active) {
Expand All @@ -435,7 +474,15 @@ export default class Runner {
}).catch(e => (this.hookError = e));

const { dryRun, match, tags } = options;
for (const journey of this.journeys) {

this.parseMatrix(options);

const journeys = Array.from(this.suites.values()).reduce((acc, suite) => {
const suiteJourneys = suite.entries;
return [...acc, ...suiteJourneys];
}, []);

for (const journey of journeys) {
/**
* Used by heartbeat to gather all registered journeys
*/
Expand Down
54 changes: 54 additions & 0 deletions src/dsl/journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ export class Journey {
name: string;
id?: string;
tags?: string[];
matrix: Matrix;
callback: JourneyCallback;
location?: Location;
steps: Step[] = [];
hooks: Hooks = { before: [], after: [] };
monitor: Monitor;
params: Params = {};
parent!: Suite;

constructor(
options: JourneyOptions,
Expand All @@ -73,6 +75,7 @@ export class Journey {
this.name = options.name;
this.id = options.id || options.name;
this.tags = options.tags;
this.matrix = options.matrix;
this.callback = callback;
this.location = location;
this.updateMonitor({});
Expand Down Expand Up @@ -124,4 +127,55 @@ export class Journey {
const matchess = micromatch(this.tags || ['*'], pattern);
return matchess.length > 0;
}

private _serialize(): any {
return {
options: {
name: this.name,
id: this.id,
tags: this.tags,
},
callback: this.callback,
location: this.location,
steps: this.steps,
hooks: this.hooks,
monitor: this.monitor,
params: this.params,
};
}

clone(): Journey {
const data = this._serialize();
const test = Journey._parse(data);
return test;
}

static _parse(data: any): Journey {
const journey = new Journey(data.options, data.callback, data.location, data.params);
return journey;
}
}

export class Suite {
location: Location;
private _entries: Journey[] = [];

constructor(
location: Location,
) {
this.location = location
}

get entries() {
return this._entries;
}

addJourney(journey: Journey) {
journey.parent = this;
this._entries.push(journey);
}

clearJourneys() {
this._entries = [];
}
}

0 comments on commit 03897ef

Please sign in to comment.