Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(betterer 🔧): simplify reporter calls #370

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions packages/betterer/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { BettererError } from '@betterer/errors';
import assert from 'assert';

import { BettererConfig } from '../config';
import { BettererReporter } from '../reporters';
import { BettererReporterΩ } from '../reporters';
import { requireUncached } from '../require';
import { BettererResults, BettererResultΩ } from '../results';
import {
BettererDiff,
BettererTest,
BettererTestBase,
BettererTestMap,
Expand All @@ -18,7 +17,7 @@ import {
import { BettererFilePaths } from '../watcher';
import { BettererRunΩ } from './run';
import { BettererSummaryΩ } from './summary';
import { BettererContext, BettererRun, BettererRunNames, BettererRuns, BettererSummary } from './types';
import { BettererContext, BettererRunNames, BettererRuns, BettererSummary } from './types';

export type BettererRunner = (runs: BettererRuns) => Promise<void>;

Expand All @@ -29,7 +28,7 @@ export class BettererContextΩ implements BettererContext {

private _running: Promise<void> | null = null;

constructor(public readonly config: BettererConfig, private _reporter?: BettererReporter) {
constructor(public readonly config: BettererConfig, private _reporter: BettererReporterΩ) {
this._results = new BettererResults(this.config.resultsPath);
}

Expand All @@ -42,7 +41,7 @@ export class BettererContextΩ implements BettererContext {
this._initFilters();
}

public async start(runner: BettererRunner, filePaths: BettererFilePaths = []): Promise<BettererSummary> {
public async run(runner: BettererRunner, filePaths: BettererFilePaths = []): Promise<BettererSummary> {
const runs = await Promise.all(
Object.keys(this._tests)
.filter((name) => {
Expand All @@ -55,40 +54,24 @@ export class BettererContextΩ implements BettererContext {
const { isSkipped, config } = test;
const expected = await this._results.getExpectedResult(name, config);
const expectedΩ = expected as BettererResultΩ;
return new BettererRunΩ(this, name, config, expectedΩ, filePaths, isSkipped);
return new BettererRunΩ(this._reporter, name, config, expectedΩ, filePaths, isSkipped);
})
);
const obsolete = await this._initObsolete();
await this._reporter?.runsStart?.(runs, filePaths);
await this._reporter.runsStart(runs, filePaths);
this._running = runner(runs);
await this._running;
await this._reporter?.runsEnd?.(runs, filePaths);
await this._reporter.runsEnd(runs, filePaths);
const expected = await this._results.read();
const result = await this._results.print(runs);
const hasDiff = !!expected && expected !== result;
this._summary = new BettererSummaryΩ(runs, obsolete, result, hasDiff && !this.config.allowDiff ? expected : null);
return this._summary;
}

public async runStart(run: BettererRun): Promise<void> {
await this._reporter?.runStart?.(run);
}

public runDiff(run: BettererRun): BettererDiff {
return this._results.getDiff(run);
}

public async runEnd(run: BettererRun): Promise<void> {
await this._reporter?.runEnd?.(run);
}

public async runError(run: BettererRun, error: BettererError): Promise<void> {
await this._reporter?.runError?.(run, error);
}

public async end(): Promise<void> {
assert(this._summary);
await this._reporter?.contextEnd?.(this, this._summary);
await this._reporter.contextEnd(this, this._summary);
}

public async save(): Promise<void> {
Expand Down
21 changes: 8 additions & 13 deletions packages/betterer/src/context/run.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BettererError } from '@betterer/errors';
import assert from 'assert';

import { BettererReporterΩ } from '../reporters';
import { BettererResult } from '../results';
import { BettererDiff, BettererTestConfig } from '../test';
import { BettererFilePaths } from '../watcher';
import { BettererContextΩ } from './context';
import { BettererContext, BettererRun } from './types';
import { BettererRun } from './types';

enum BettererRunStatus {
better,
Expand All @@ -29,7 +29,7 @@ export class BettererRunΩ implements BettererRun {
private _isRan = false;

constructor(
private readonly _context: BettererContext,
private readonly _reporter: BettererReporterΩ,
private readonly _name: string,
private readonly _test: BettererTestConfig,
private readonly _expected: BettererResult,
Expand Down Expand Up @@ -116,13 +116,11 @@ export class BettererRunΩ implements BettererRun {
}

public async end(): Promise<void> {
const contextΩ = this._context as BettererContextΩ;
await contextΩ.runEnd(this);
await this._reporter.runEnd(this);
}

public async failed(e: BettererError): Promise<void> {
const contextΩ = this._context as BettererContextΩ;
await contextΩ.runError(this, e);
await this._reporter.runError(this, e);
assert.strictEqual(this._status, BettererRunStatus.pending);
this._status = BettererRunStatus.failed;
}
Expand All @@ -138,8 +136,7 @@ export class BettererRunΩ implements BettererRun {
public async start(): Promise<void> {
const startTime = Date.now();
this._isExpired = startTime > this._test.deadline;
const contextΩ = this._context as BettererContextΩ;
await contextΩ.runStart(this);
await this._reporter.runStart(this);
this._timestamp = startTime;
}

Expand All @@ -149,14 +146,12 @@ export class BettererRunΩ implements BettererRun {

public update(result: BettererResult): void {
this._updateResult(BettererRunStatus.update, result);
const contextΩ = this._context as BettererContextΩ;
this._diff = contextΩ.runDiff(this);
this._diff = this.test.differ(this.expected.result, this.result.result);
}

public worse(result: BettererResult): void {
this._updateResult(BettererRunStatus.worse, result);
const contextΩ = this._context as BettererContextΩ;
this._diff = contextΩ.runDiff(this);
this._diff = this.test.differ(this.expected.result, this.result.result);
}

private _updateResult(status: BettererRunStatus, result: BettererResult, isComplete = false) {
Expand Down
2 changes: 1 addition & 1 deletion packages/betterer/src/reporters/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { BettererMultiReporterΩ } from './reporter-multi';
export { BettererReporterΩ } from './reporter';
export { loadReporters, DEFAULT_REPORTER, WATCH_REPORTER } from './loader';
export { BettererReporter, BettererReporterModule, BettererReporterNames } from './types';
8 changes: 4 additions & 4 deletions packages/betterer/src/reporters/loader.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { BettererError } from '@betterer/errors';
import { requireUncached } from '../require';
import { isFunction } from '../utils';
import { BettererMultiReporterΩ } from './reporter-multi';
import { BettererReporterΩ } from './reporter';
import { BettererReporter, BettererReporterModule, BettererReporterNames } from './types';

export const DEFAULT_REPORTER = '@betterer/reporter';
export const WATCH_REPORTER = '@betterer/watch-reporter';

const HOOK_NAMES = Object.keys(BettererMultiReporterΩ.prototype) as ReadonlyArray<keyof BettererReporter>;
const HOOK_NAMES = Object.keys(BettererReporterΩ.prototype) as ReadonlyArray<keyof BettererReporter>;

export function loadReporters(reporterNames: BettererReporterNames): BettererMultiReporterΩ {
export function loadReporters(reporterNames: BettererReporterNames): BettererReporterΩ {
const reporters: Array<BettererReporter> = reporterNames.map((name) => {
try {
const module: BettererReporterModule = requireUncached(name);
Expand All @@ -22,7 +22,7 @@ export function loadReporters(reporterNames: BettererReporterNames): BettererMul
throw new BettererError(`could not require "${name}". 😔`, e);
}
});
return new BettererMultiReporterΩ(reporters);
return new BettererReporterΩ(reporters);
}

function validate(result: unknown): asserts result is BettererReporter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BettererContext, BettererRun, BettererRuns, BettererSummary } from '../
import { BettererFilePaths } from '../watcher';
import { BettererReporter } from './types';

export class BettererMultiReporterΩ implements BettererReporter {
export class BettererReporterΩ implements BettererReporter {
constructor(private _reporters: Array<BettererReporter>) {}

async configError(partialConfig: BettererConfigPartial, error: BettererError): Promise<void> {
Expand Down
8 changes: 2 additions & 6 deletions packages/betterer/src/results/results.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'assert';

import { BettererRun, BettererRuns } from '../context';
import { BettererRuns } from '../context';
import { read } from '../reader';
import { BettererTestConfig, BettererDiff } from '../test';
import { BettererTestConfig } from '../test';
import { write } from '../writer';
import { parse } from './parser';
import { print } from './printer';
Expand All @@ -29,10 +29,6 @@ export class BettererResults {
return new BettererResultΩ();
}

public getDiff(run: BettererRun): BettererDiff {
return run.test.differ(run.expected.result, run.result.result);
}

public read(): Promise<string | null> {
return read(this._resultsPath);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/betterer/src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BettererFilePaths } from '../watcher';
import { BettererResultΩ } from '../results';

export async function parallel(context: BettererContextΩ, filePaths: BettererFilePaths): Promise<BettererSummary> {
return context.start(async (runs) => {
return context.run(async (runs) => {
await Promise.all(
runs.map(async (run) => {
const runΩ = run as BettererRunΩ;
Expand All @@ -17,7 +17,7 @@ export async function parallel(context: BettererContextΩ, filePaths: BettererFi
}

export async function serial(context: BettererContextΩ): Promise<BettererSummary> {
return context.start(async (runs) => {
return context.run(async (runs) => {
await runs.reduce(async (p, run) => {
await p;
const runΩ = run as BettererRunΩ;
Expand Down
Loading