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

feat(betterer ✨): use prettier with default printer for nicer output #496

Merged
merged 1 commit into from
Feb 15, 2021
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
4 changes: 2 additions & 2 deletions goldens/api/@betterer/betterer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ export declare type BettererResult = {

export declare type BettererResultValue = BettererResultValueBasic | BettererResultValueComplex;

export declare type BettererResultValueBasic = number;
export declare type BettererResultValueBasic = unknown;

export declare type BettererResultValueComplex = {
value: number;
value: unknown;
};

export declare type BettererRun = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"lerna": "^3.22.1",
"npm-run-all": "^4.1.5",
"react": "^16.13.1",
"prettier": "^2.1.1",
"prettier": "^2.2.1",
"ts-api-guardian": "^0.5.0",
"ts-jest": "^26.3.0",
"ts-node": "^9.0.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/betterer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"globby": "^11.0.1",
"lines-and-columns": "^1.1.6",
"minimatch": "^3.0.4",
"prettier": "^2.2.1",
"ts-node": "^9.0.0",
"tslib": "^2.0.3"
},
"devDependencies": {
"@types/callsite": "^1.0.30"
"@types/callsite": "^1.0.30",
"@types/prettier": "^1.12.0"
}
}
2 changes: 1 addition & 1 deletion packages/betterer/src/results/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class BettererResultΩ implements BettererResult {
return this._isNew;
}

public get value(): number {
public get value(): unknown {
const result = this.result;
return isComplexBettererResult(result) ? result.value : result;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/betterer/src/results/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ export type BettererExpectedResult = {
};
export type BettererExpectedResults = Record<string, BettererExpectedResult>;

export type BettererResultValueBasic = number;
export type BettererResultValueBasic = unknown;
export type BettererResultValueComplex = {
value: number;
value: unknown;
};
export type BettererResultValue = BettererResultValueBasic | BettererResultValueComplex;

Expand Down
29 changes: 19 additions & 10 deletions packages/betterer/src/test/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BettererError } from '@betterer/errors';
import { BettererLogger, diffΔ } from '@betterer/logger';
import { format } from 'prettier';

import { BettererResultValue } from '../results';
import { isFunction } from '../utils';
import {
Expand All @@ -12,7 +14,7 @@ import {

export function createTestConfig<DeserialisedType extends BettererResultValue, SerialisedType, DiffType>(
config: BettererTestConfigPartial<DeserialisedType, SerialisedType, DiffType>
): BettererTestConfig<DeserialisedType, SerialisedType, DiffType> | BettererTestConfig<number> {
): BettererTestConfig<DeserialisedType, SerialisedType, DiffType> | BettererTestConfig<unknown> {
if (config.constraint == null) {
throw new BettererError('for a test to work, it must have a `constraint` function. ❌');
}
Expand All @@ -27,18 +29,25 @@ export function createTestConfig<DeserialisedType extends BettererResultValue, S
if (config.goal == null) {
throw new BettererError('for a test to work, it must have a `goal` function. ❌');
}
return { ...config, deadline } as BettererTestConfig<DeserialisedType, SerialisedType, DiffType>;
return {
...config,
deadline,
printer: config.printer || defaultPrinter
} as BettererTestConfig<DeserialisedType, SerialisedType, DiffType>;
}

const goal = createGoal(config);
return {
...config,
differ: defaultDiffer,
printer: defaultPrinter,
serialiser: { deserialise: defaultDeserialiser, serialise: defaultSerialiser },
serialiser: {
deserialise: defaultDeserialiser,
serialise: defaultSerialiser
},
goal,
deadline
} as BettererTestConfig<number>;
} as BettererTestConfig<unknown>;
}

function createDeadline<DeserialisedType extends BettererResultValue, SerialisedType, DiffType>(
Expand Down Expand Up @@ -70,10 +79,10 @@ function isComplex<DeserialisedType extends BettererResultValue, SerialisedType,
config: BettererTestConfigPartial<DeserialisedType, SerialisedType, DiffType>
): config is BettererTestConfigComplexPartial<DeserialisedType, SerialisedType, DiffType> {
const maybeComplex = config as BettererTestConfigComplexPartial<DeserialisedType, SerialisedType, DiffType>;
return !!(maybeComplex.differ && maybeComplex.printer && maybeComplex.serialiser);
return !!(maybeComplex.differ && maybeComplex.serialiser);
}

export function defaultDiffer(expected: number, result: number): BettererDiff<number, null> {
export function defaultDiffer(expected: unknown, result: unknown): BettererDiff<unknown, null> {
return {
expected,
result,
Expand All @@ -87,14 +96,14 @@ export function defaultDiffer(expected: number, result: number): BettererDiff<nu
};
}

function defaultPrinter(serialised: number): string {
return JSON.stringify(serialised);
function defaultPrinter(serialised: unknown): string {
return format(JSON.stringify(serialised), { parser: 'json' });
}

function defaultDeserialiser(serialised: number): number {
function defaultDeserialiser(serialised: unknown): unknown {
return serialised;
}

function defaultSerialiser(deserialised: number): number {
function defaultSerialiser(deserialised: unknown): unknown {
return deserialised;
}
2 changes: 1 addition & 1 deletion packages/betterer/src/test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type BettererTestConfigComplexPartial<DeserialisedType extends BettererRe
constraint: BettererTestConstraint<DeserialisedType>;
test: BettererTestFunction<DeserialisedType>;
differ: BettererDiffer<DeserialisedType, DiffType>;
printer: BettererPrinter<SerialisedType>;
printer?: BettererPrinter<SerialisedType>;
serialiser: BettererSerialiser<DeserialisedType, SerialisedType>;
goal: DeserialisedType | BettererTestGoal<DeserialisedType>;
deadline?: Date | string;
Expand Down
6 changes: 4 additions & 2 deletions test/__snapshots__/betterer-better.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,13 @@ Array [
exports[`betterer should work when a test gets better 2`] = `
"// BETTERER RESULTS V2.
exports[\`should shrink\`] = {
value: \`1\`
value: \`1
\`
};

exports[\`should grow\`] = {
value: \`1\`
value: \`1
\`
};
"
`;
3 changes: 2 additions & 1 deletion test/__snapshots__/betterer-config-ts-esm.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ Array [
exports[`betterer should work with a .betterer.ts file that uses ES modules 2`] = `
"// BETTERER RESULTS V2.
exports[\`gets better\`] = {
value: \`1\`
value: \`1
\`
};
"
`;
3 changes: 2 additions & 1 deletion test/__snapshots__/betterer-config-ts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ Array [
exports[`betterer should work with a .betterer.ts file 2`] = `
"// BETTERER RESULTS V2.
exports[\`gets better\`] = {
value: \`1\`
value: \`1
\`
};
"
`;
6 changes: 4 additions & 2 deletions test/__snapshots__/betterer-deadline.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ Array [
exports[`betterer should do nothing when a test is not past its deadline 2`] = `
"// BETTERER RESULTS V2.
exports[\`should grow\`] = {
value: \`0\`
value: \`0
\`
};
"
`;
Expand Down Expand Up @@ -179,7 +180,8 @@ Array [
exports[`betterer should mark a test as expired when is is past its deadline 2`] = `
"// BETTERER RESULTS V2.
exports[\`should grow\`] = {
value: \`0\`
value: \`0
\`
};
"
`;
3 changes: 2 additions & 1 deletion test/__snapshots__/betterer-named-exports.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ Array [
exports[`betterer should work with named exports in the config file 2`] = `
"// BETTERER RESULTS V2.
exports[\`getsBetter\`] = {
value: \`1\`
value: \`1
\`
};
"
`;
9 changes: 6 additions & 3 deletions test/__snapshots__/betterer-only.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,18 @@ Array [
exports[`betterer should run specific tests 2`] = `
"// BETTERER RESULTS V2.
exports[\`test 1\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`test 2\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`test 3\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`test 4\`] = {
Expand Down
55 changes: 55 additions & 0 deletions test/__snapshots__/betterer-printer.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`betterer should handle printing for pathological cases 1`] = `
"// BETTERER RESULTS V2.
exports[\`big array\`] = {
value: \`[
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\",
\\"./some/long/path/to/some/file\\"
]
\`
};

exports[\`big object\`] = {
value: \`{
\\"a\\": \\"./some/long/path/to/some/file\\",
\\"b\\": \\"./some/long/path/to/some/file\\",
\\"c\\": \\"./some/long/path/to/some/file\\",
\\"d\\": \\"./some/long/path/to/some/file\\",
\\"e\\": \\"./some/long/path/to/some/file\\",
\\"f\\": \\"./some/long/path/to/some/file\\",
\\"g\\": \\"./some/long/path/to/some/file\\",
\\"h\\": \\"./some/long/path/to/some/file\\",
\\"i\\": \\"./some/long/path/to/some/file\\",
\\"j\\": \\"./some/long/path/to/some/file\\",
\\"k\\": \\"./some/long/path/to/some/file\\",
\\"l\\": \\"./some/long/path/to/some/file\\",
\\"m\\": \\"./some/long/path/to/some/file\\",
\\"n\\": \\"./some/long/path/to/some/file\\",
\\"o\\": \\"./some/long/path/to/some/file\\",
\\"p\\": \\"./some/long/path/to/some/file\\",
\\"q\\": \\"./some/long/path/to/some/file\\",
\\"r\\": \\"./some/long/path/to/some/file\\",
\\"s\\": \\"./some/long/path/to/some/file\\"
}
\`
};
"
`;
6 changes: 4 additions & 2 deletions test/__snapshots__/betterer-same.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,13 @@ Array [
exports[`betterer should work when a test is the same 2`] = `
"// BETTERER RESULTS V2.
exports[\`doesn't get bigger\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`doesn't get smaller\`] = {
value: \`0\`
value: \`0
\`
};
"
`;
3 changes: 2 additions & 1 deletion test/__snapshots__/betterer-skip.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ Array [
exports[`betterer should skip a test 2`] = `
"// BETTERER RESULTS V2.
exports[\`test 1\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`test 2\`] = {
Expand Down
6 changes: 4 additions & 2 deletions test/__snapshots__/betterer-worse.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,13 @@ Run \`betterer --update\` to force an update of the results file. 🆙
exports[`betterer should work when a test gets worse 2`] = `
"// BETTERER RESULTS V2.
exports[\`should shrink\`] = {
value: \`0\`
value: \`0
\`
};

exports[\`should grow\`] = {
value: \`2\`
value: \`2
\`
};
"
`;
Loading