Skip to content

Commit

Permalink
Add toEqualApolloQueryResult matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jan 14, 2025
1 parent bf08e88 commit f6ddd65
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/testing/matchers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
ApolloClient,
ApolloQueryResult,
DocumentNode,
OperationVariables,
} from "../../core/index.js";
Expand Down Expand Up @@ -69,6 +70,12 @@ interface ApolloCustomMatchers<R = void, T = {}> {
(value: any, options?: TakeOptions) => Promise<R>
: { error: "matcher needs to be called on an ObservableStream instance" };

toEqualApolloQueryResult: T extends ApolloQueryResult<infer TData> ?
(expected: ApolloQueryResult<TData>) => R
: T extends Promise<ApolloQueryResult<infer TData>> ?
(expected: ApolloQueryResult<TData>) => R
: { error: "matchers needs to be called on an ApolloQueryResult" };

toEqualQueryResult: T extends QueryResult<infer TData, infer TVariables> ?
(expected: Pick<QueryResult<TData, TVariables>, CheckedKeys>) => R
: T extends Promise<QueryResult<infer TData, infer TVariables>> ?
Expand Down
2 changes: 2 additions & 0 deletions src/testing/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { toEmitMatchedValue } from "./toEmitMatchedValue.js";
import { toEmitNext } from "./toEmitNext.js";
import { toEmitValue } from "./toEmitValue.js";
import { toEmitValueStrict } from "./toEmitValueStrict.js";
import { toEqualApolloQueryResult } from "./toEqualApolloQueryResult.js";
import { toEqualQueryResult } from "./toEqualQueryResult.js";

expect.extend({
Expand All @@ -20,6 +21,7 @@ expect.extend({
toEmitNext,
toEmitValue,
toEmitValueStrict,
toEqualApolloQueryResult,
toEqualQueryResult,
toBeDisposed,
toHaveSuspenseCacheEntryUsing,
Expand Down
44 changes: 44 additions & 0 deletions src/testing/matchers/toEqualApolloQueryResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { iterableEquality } from "@jest/expect-utils";
import type { MatcherFunction } from "expect";
import type { ApolloQueryResult } from "../../core/index.js";

export const toEqualApolloQueryResult: MatcherFunction<
[queryResult: ApolloQueryResult<any>]
> = function (actual, expected) {
const queryResult = actual as ApolloQueryResult<any>;
const hint = this.utils.matcherHint(
this.isNot ? ".not.toEqualApolloQueryResult" : "toEqualApolloQueryResult",
"queryResult",
"expected",
{ isNot: this.isNot, promise: this.promise }
);

const pass = this.equals(
queryResult,
expected,
// https://github.com/jestjs/jest/blob/22029ba06b69716699254bb9397f2b3bc7b3cf3b/packages/expect/src/matchers.ts#L62-L67
[...this.customTesters, iterableEquality],
true
);

return {
pass,
message: () => {
if (pass) {
return hint + `\n\nExpected: not ${this.utils.printExpected(expected)}`;
}

return (
hint +
"\n\n" +
this.utils.printDiffOrStringify(
expected,
queryResult,
"Expected",
"Received",
true
)
);
},
};
};

0 comments on commit f6ddd65

Please sign in to comment.