Skip to content

Commit

Permalink
faster error messages, optin
Browse files Browse the repository at this point in the history
  • Loading branch information
laat committed Oct 13, 2020
1 parent 8e039f5 commit a78942d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@apidevtools/json-schema-ref-parser": "^9.0.1",
"ajv": "^6.10.2",
"better-ajv-errors": "^0.6.7",
"chalk": "^4.1.0",
"expect": "^26.0.0",
"jest-matcher-utils": "^26.0.0",
"lodash": "^4.17.15"
Expand Down
32 changes: 32 additions & 0 deletions src/getErrorMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Ajv from "ajv";
import betterAjvErrors from "better-ajv-errors";
import chalk from "chalk";

export function getErrorMessage(
schema: any,
received: any,
errors: Ajv.ErrorObject[] | null | undefined,
{ bailFast }: { bailFast: boolean }
) {
const errs = errors || [];
if (!bailFast || errs.length < 20) {
return betterAjvErrors(schema, received, errs, {
indent: 2,
});
} else {
let messageToPrint = "received\n";
errs.forEach((error) => {
let line = `${error.dataPath} ${error.message}`;

if (error.keyword === "additionalProperties") {
line += `, but found '${
// @ts-ignore
error.params.additionalProperty
}'`;
}

messageToPrint += chalk.red(` ${line}\n`);
});
return messageToPrint;
}
}
14 changes: 7 additions & 7 deletions src/toMatchApiResponse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import betterAjvErrors from "better-ajv-errors";
import RefParser from "@apidevtools/json-schema-ref-parser";
import { MatcherState } from "expect";
import { matcherHint } from "jest-matcher-utils";
import RefParser from "@apidevtools/json-schema-ref-parser";
import get from "lodash/get";
import { ajvCompile } from "./ajvCompile";
import { getErrorMessage } from "./getErrorMessage";

export type Method =
| "delete"
Expand Down Expand Up @@ -40,7 +40,8 @@ declare global {
toMatchApiResponse(
schema: any,
method: Method,
path: string
path: string,
options?: { bailFast: boolean }
): Promise<void>;
}
}
Expand All @@ -50,7 +51,8 @@ export async function toMatchApiResponse(
receivedAny: any,
openapiSpec: any,
method: Method,
path: string
path: string,
{ bailFast = false }: { bailFast?: boolean } = {}
) {
const options = {
isNot: this.isNot,
Expand Down Expand Up @@ -143,9 +145,7 @@ export async function toMatchApiResponse(
message: () =>
matcherHint("toMatchOpenapiResponse", undefined, undefined, options) +
"\n\n" +
betterAjvErrors(schema, body, validate.errors, {
indent: 2,
}),
getErrorMessage(schema, body, validate.errors, { bailFast }),
pass: false,
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/toMatchRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe("toMatchRef$", () => {
// above max
await expect(2147483647 + 1).not.toMatchRef$(
testApi,
"#/components/schemas/Int32Number"
"#/components/schemas/Int32Number",
{ bailFast: true }
);
// below min
await expect(-2147483648 + -1).not.toMatchRef$(
Expand Down
18 changes: 11 additions & 7 deletions src/toMatchRef.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import betterAjvErrors from "better-ajv-errors";
import RefParser from "@apidevtools/json-schema-ref-parser";
import { MatcherState } from "expect";
import { matcherHint } from "jest-matcher-utils";
import RefParser from "@apidevtools/json-schema-ref-parser";
import { ajvCompile } from "./ajvCompile";
import { getErrorMessage } from "./getErrorMessage";

declare global {
namespace jest {
// tslint:disable-next-line:interface-name
Expand All @@ -12,15 +13,20 @@ declare global {
* @param schema openapi.json schema, parsed as object
* @param ref$ the type to match against
*/
toMatchRef$(schema: any, ref$: string): Promise<void>;
toMatchRef$(
schema: any,
ref$: string,
options?: { bailFast: boolean }
): Promise<void>;
}
}
}
export async function toMatchRef$(
this: MatcherState,
received: any,
openapiSpec: any,
ref$: string
ref$: string,
{ bailFast = false }: { bailFast?: boolean } = {}
) {
const options = {
isNot: this.isNot,
Expand All @@ -46,9 +52,7 @@ export async function toMatchRef$(
message: () =>
matcherHint("toMatchRef$", undefined, undefined, options) +
"\n\n" +
betterAjvErrors(desiredSpec, received, validate.errors, {
indent: 2,
}),
getErrorMessage(desiredSpec, received, validate.errors, { bailFast }),
pass: false,
};
}
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,14 @@ chalk@^4.0.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

ci-info@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46"
Expand Down

0 comments on commit a78942d

Please sign in to comment.