Skip to content

Commit

Permalink
Use .rejects instead of catch on promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 11, 2024
1 parent e9d84ba commit ae8a237
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,9 @@ describe("client", () => {
cache: new InMemoryCache({ addTypename: false }),
});

await client.query({ query }).catch((error: ApolloError) => {
expect(error.networkError).toBeDefined();
expect(error.networkError!.message).toEqual(networkError.message);
});
await expect(client.query({ query })).rejects.toThrow(
new ApolloError({ networkError })
);
});

it("should not warn when receiving multiple results from apollo-link network interface", () => {
Expand Down Expand Up @@ -1742,28 +1741,19 @@ describe("client", () => {
"to receive multiple results from the cache and the network, or consider " +
"using a different fetchPolicy, such as cache-first or network-only.";

function checkCacheAndNetworkError(callback: () => any) {
try {
callback();
throw new Error("not reached");
} catch (thrown) {
expect((thrown as Error).message).toBe(cacheAndNetworkError);
}
}

// Test that cache-and-network can only be used on watchQuery, not query.
it("warns when used with client.query", () => {
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
});

checkCacheAndNetworkError(() =>
client.query({
expect(() => {
void client.query({
query,
fetchPolicy: "cache-and-network" as FetchPolicy,
})
);
});
}).toThrow(new Error(cacheAndNetworkError));
});

it("warns when used with client.query with defaultOptions", () => {
Expand All @@ -1777,14 +1767,15 @@ describe("client", () => {
},
});

checkCacheAndNetworkError(() =>
client.query({
query,
// This undefined value should be ignored in favor of
// defaultOptions.query.fetchPolicy.
fetchPolicy: void 0,
})
);
expect(
() =>
void client.query({
query,
// This undefined value should be ignored in favor of
// defaultOptions.query.fetchPolicy.
fetchPolicy: void 0,
})
).toThrow(new Error(cacheAndNetworkError));
});

it("fetches from cache first, then network", async () => {
Expand Down Expand Up @@ -2067,15 +2058,9 @@ describe("client", () => {
cache: new InMemoryCache({ addTypename: false }),
});

try {
await client.mutate({ mutation });
throw new Error("Returned a result when it should not have.");
} catch (e) {
const error = e as ApolloError;

expect(error.networkError).toBeDefined();
expect(error.networkError!.message).toBe(networkError.message);
}
await expect(client.mutate({ mutation })).rejects.toThrow(
new ApolloError({ networkError })
);
});

it("should pass a GraphQL error correctly on a mutation", async () => {
Expand Down

0 comments on commit ae8a237

Please sign in to comment.