Skip to content

Commit eec20d1

Browse files
authored
Silence more core tests warnings (#12523)
1 parent 73d960b commit eec20d1

File tree

4 files changed

+124
-107
lines changed

4 files changed

+124
-107
lines changed

Diff for: src/__tests__/client.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4284,7 +4284,8 @@ describe("custom document transforms", () => {
42844284
cache: new InMemoryCache(),
42854285
});
42864286

4287-
await client.query({ query });
4287+
// Pass no-cache to silence cache write warnings
4288+
await client.query({ query, fetchPolicy: "no-cache" });
42884289

42894290
expect(document!).toMatchDocument(gql`
42904291
query TestQuery {

Diff for: src/__tests__/graphqlSubscriptions.ts

+96-85
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { gql } from "graphql-tag";
22

33
import { InMemoryCache } from "@apollo/client/cache";
4+
import type { TypedDocumentNode } from "@apollo/client/core";
45
import { ApolloClient } from "@apollo/client/core";
56
import {
67
CombinedGraphQLErrors,
@@ -19,37 +20,21 @@ describe("GraphQL Subscriptions", () => {
1920
"Vyacheslav Kim",
2021
"Changping Chen",
2122
"Amanda Liu",
22-
].map((name) => ({ result: { data: { user: { name } } }, delay: 10 }));
23-
24-
let options: any;
25-
let defaultOptions: any;
26-
beforeEach(() => {
27-
options = {
28-
query: gql`
29-
subscription UserInfo($name: String) {
30-
user(name: $name) {
31-
name
32-
}
33-
}
34-
`,
35-
variables: {
36-
name: "Changping Chen",
37-
},
38-
context: {
39-
someVar: "Some value",
40-
},
41-
};
42-
43-
defaultOptions = {
44-
query: gql`
45-
subscription UserInfo($name: String = "Changping Chen") {
46-
user(name: $name) {
47-
name
48-
}
49-
}
50-
`,
51-
};
52-
});
23+
].map((name) => ({
24+
result: { data: { user: { __typename: "User" as const, name } } },
25+
delay: 10,
26+
}));
27+
28+
const subscription: TypedDocumentNode<
29+
{ user: { __typename: "User"; name: string } },
30+
{ name?: string }
31+
> = gql`
32+
subscription UserInfo($name: String) {
33+
user(name: $name) {
34+
name
35+
}
36+
}
37+
`;
5338

5439
it("should start a subscription on network interface and unsubscribe", async () => {
5540
const link = new MockSubscriptionLink();
@@ -59,7 +44,17 @@ describe("GraphQL Subscriptions", () => {
5944
cache: new InMemoryCache(),
6045
});
6146

62-
const stream = new ObservableStream(client.subscribe(defaultOptions));
47+
const stream = new ObservableStream(
48+
client.subscribe({
49+
query: gql`
50+
subscription UserInfo($name: String = "Changping Chen") {
51+
user(name: $name) {
52+
name
53+
}
54+
}
55+
`,
56+
})
57+
);
6358
link.simulateResult(results[0]);
6459

6560
await expect(stream).toEmitTypedValue(results[0].result);
@@ -75,7 +70,12 @@ describe("GraphQL Subscriptions", () => {
7570
cache: new InMemoryCache(),
7671
});
7772

78-
const stream = new ObservableStream(client.subscribe(options));
73+
const stream = new ObservableStream(
74+
client.subscribe({
75+
query: subscription,
76+
variables: { name: "Changping Chen" },
77+
})
78+
);
7979

8080
link.simulateResult(results[0]);
8181

@@ -91,7 +91,10 @@ describe("GraphQL Subscriptions", () => {
9191
cache: new InMemoryCache(),
9292
});
9393

94-
const obs = client.subscribe(options);
94+
const obs = client.subscribe({
95+
query: subscription,
96+
variables: { name: "Changping Chen" },
97+
});
9598
const stream1 = new ObservableStream(obs);
9699
const stream2 = new ObservableStream(obs);
97100

@@ -108,7 +111,12 @@ describe("GraphQL Subscriptions", () => {
108111
cache: new InMemoryCache(),
109112
});
110113

111-
const stream = new ObservableStream(client.subscribe(options));
114+
const stream = new ObservableStream(
115+
client.subscribe({
116+
query: subscription,
117+
variables: { name: "Changping Chen" },
118+
})
119+
);
112120

113121
for (let i = 0; i < 4; i++) {
114122
link.simulateResult(results[i]);
@@ -131,8 +139,13 @@ describe("GraphQL Subscriptions", () => {
131139

132140
expect(cache.extract()).toEqual({});
133141

134-
options.fetchPolicy = "no-cache";
135-
const stream = new ObservableStream(client.subscribe(options));
142+
const stream = new ObservableStream(
143+
client.subscribe({
144+
query: subscription,
145+
fetchPolicy: "no-cache",
146+
variables: { name: "Changping Chen" },
147+
})
148+
);
136149

137150
link.simulateResult(results[0]);
138151

@@ -147,7 +160,10 @@ describe("GraphQL Subscriptions", () => {
147160
cache: new InMemoryCache(),
148161
});
149162

150-
const obs = client.subscribe(options);
163+
const obs = client.subscribe({
164+
query: subscription,
165+
variables: { name: "Changping Chen" },
166+
});
151167
const stream = new ObservableStream(obs);
152168

153169
link.simulateResult({
@@ -195,7 +211,10 @@ describe("GraphQL Subscriptions", () => {
195211
cache: new InMemoryCache(),
196212
});
197213

198-
const obs = client.subscribe(options);
214+
const obs = client.subscribe({
215+
query: subscription,
216+
variables: { name: "Changping Chen" },
217+
});
199218
const stream = new ObservableStream(obs);
200219

201220
link.simulateResult({
@@ -247,7 +266,10 @@ describe("GraphQL Subscriptions", () => {
247266
cache: new InMemoryCache(),
248267
});
249268

250-
const obs = client.subscribe(options);
269+
const obs = client.subscribe({
270+
query: subscription,
271+
variables: { name: "Changping Chen" },
272+
});
251273
const stream = new ObservableStream(obs);
252274

253275
link.simulateResult({ error: new Error("Oops") });
@@ -309,7 +331,11 @@ describe("GraphQL Subscriptions", () => {
309331
cache: new InMemoryCache(),
310332
});
311333

312-
const obs = client.subscribe({ ...options, errorPolicy: "all" });
334+
const obs = client.subscribe({
335+
query: subscription,
336+
errorPolicy: "all",
337+
variables: { name: "Changping Chen" },
338+
});
313339
const stream = new ObservableStream(obs);
314340

315341
link.simulateResult({ error: new Error("Oops") });
@@ -325,27 +351,22 @@ describe("GraphQL Subscriptions", () => {
325351
const { httpLink, enqueueProtocolErrors } =
326352
mockMultipartSubscriptionStream();
327353

328-
const query = gql`
329-
subscription UserInfo($name: String) {
330-
user(name: $name) {
331-
name
332-
}
333-
}
334-
`;
335354
const client = new ApolloClient({
336355
link: httpLink,
337356
cache: new InMemoryCache(),
338357
});
339358

340359
const obs = client.subscribe({
341-
query,
360+
query: subscription,
342361
variables: { name: "Iron Man" },
343362
errorPolicy: "all",
344363
});
345364
const stream = new ObservableStream(obs);
346365

347-
// Silence expected warning about missing field for cache write
348-
using _consoleSpy = spyOnConsole("warn");
366+
// Silence warning about missing field for cache write
367+
// TODO: Investigate this to see if we can silence this since this should
368+
// not be expected.
369+
using _consoleSpy = spyOnConsole("error");
349370

350371
enqueueProtocolErrors([
351372
{
@@ -372,21 +393,14 @@ describe("GraphQL Subscriptions", () => {
372393
});
373394

374395
it('does not emit anything for GraphQL errors with no data in next result when `errorPolicy` is "ignore"', async () => {
375-
const query = gql`
376-
subscription UserInfo($name: String) {
377-
user(name: $name) {
378-
name
379-
}
380-
}
381-
`;
382396
const link = new MockSubscriptionLink();
383397
const client = new ApolloClient({
384398
link,
385399
cache: new InMemoryCache(),
386400
});
387401

388402
const obs = client.subscribe({
389-
query,
403+
query: subscription,
390404
variables: { name: "Iron Man" },
391405
errorPolicy: "ignore",
392406
});
@@ -406,21 +420,14 @@ describe("GraphQL Subscriptions", () => {
406420
});
407421

408422
it('does not emit anything for network errors with no data in next result when `errorPolicy` is "ignore"', async () => {
409-
const query = gql`
410-
subscription UserInfo($name: String) {
411-
user(name: $name) {
412-
name
413-
}
414-
}
415-
`;
416423
const link = new MockSubscriptionLink();
417424
const client = new ApolloClient({
418425
link,
419426
cache: new InMemoryCache(),
420427
});
421428

422429
const obs = client.subscribe({
423-
query,
430+
query: subscription,
424431
variables: { name: "Iron Man" },
425432
errorPolicy: "ignore",
426433
});
@@ -434,27 +441,20 @@ describe("GraphQL Subscriptions", () => {
434441
it('does not emit anything and completes observable for protocolErrors when `errorPolicy` is "ignore"', async () => {
435442
const { httpLink, enqueueProtocolErrors } =
436443
mockMultipartSubscriptionStream();
437-
const query = gql`
438-
subscription UserInfo($name: String) {
439-
user(name: $name) {
440-
name
441-
}
442-
}
443-
`;
444444
const client = new ApolloClient({
445445
link: httpLink,
446446
cache: new InMemoryCache(),
447447
});
448448

449449
const obs = client.subscribe({
450-
query,
450+
query: subscription,
451451
variables: { name: "Iron Man" },
452452
errorPolicy: "ignore",
453453
});
454454
const stream = new ObservableStream(obs);
455455

456-
// Silence expected warning about missing field for cache write
457-
using _consoleSpy = spyOnConsole("warn");
456+
// Silence warning about missing field for cache write
457+
using _consoleSpy = spyOnConsole("error");
458458

459459
enqueueProtocolErrors([
460460
{
@@ -475,7 +475,9 @@ describe("GraphQL Subscriptions", () => {
475475
cache: new InMemoryCache(),
476476
});
477477

478-
const stream = new ObservableStream(client.subscribe(defaultOptions));
478+
const stream = new ObservableStream(
479+
client.subscribe({ query: subscription })
480+
);
479481

480482
setTimeout(() => link.simulateComplete(), 50);
481483

@@ -489,15 +491,19 @@ describe("GraphQL Subscriptions", () => {
489491
link,
490492
});
491493

492-
const stream = new ObservableStream(client.subscribe(options));
494+
const stream = new ObservableStream(
495+
client.subscribe({
496+
query: subscription,
497+
variables: { name: "Changping Chen" },
498+
context: { someVar: "Some value" },
499+
})
500+
);
493501

494502
link.simulateResult(results[0]);
495503

496504
await expect(stream).toEmitTypedValue(results[0].result);
497505

498-
expect(link.operation?.getContext().someVar).toEqual(
499-
options.context.someVar
500-
);
506+
expect(link.operation?.getContext().someVar).toEqual("Some value");
501507
});
502508

503509
it("emits an error if the result has protocolErrors on it", async () => {
@@ -509,11 +515,14 @@ describe("GraphQL Subscriptions", () => {
509515
cache: new InMemoryCache(),
510516
});
511517

512-
const obs = client.subscribe(options);
518+
const obs = client.subscribe({
519+
query: subscription,
520+
variables: { name: "Changping Chen" },
521+
});
513522
const stream = new ObservableStream(obs);
514523

515-
// Silence expected warning about missing field for cache write
516-
using _consoleSpy = spyOnConsole("warn");
524+
// Silence warning about missing field for cache write
525+
using _consoleSpy = spyOnConsole("error");
517526

518527
enqueueProtocolErrors([
519528
{
@@ -535,5 +544,7 @@ describe("GraphQL Subscriptions", () => {
535544
},
536545
]),
537546
});
547+
548+
await expect(stream).toComplete();
538549
});
539550
});

Diff for: src/__tests__/mutationResults.ts

+2
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,8 @@ describe("mutation results", () => {
672672
});
673673

674674
try {
675+
// Silence mock link warning
676+
using _ = spyOnConsole("warn");
675677
await expect(
676678
client.mutate({
677679
mutation: mutationTodo,

0 commit comments

Comments
 (0)