Skip to content

Commit cda321c

Browse files
authored
Remove itAsync from remaining relevant tests (#12210)
1 parent bb93405 commit cda321c

File tree

7 files changed

+1050
-1193
lines changed

7 files changed

+1050
-1193
lines changed

src/__tests__/graphqlSubscriptions.ts

+76-134
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { ApolloClient, FetchResult } from "../core";
44
import { InMemoryCache } from "../cache";
55
import { ApolloError, PROTOCOL_ERRORS_SYMBOL } from "../errors";
66
import { QueryManager } from "../core/QueryManager";
7-
import { itAsync, mockObservableLink } from "../testing";
7+
import { mockObservableLink } from "../testing";
88
import { GraphQLError } from "graphql";
9-
import { spyOnConsole } from "../testing/internal";
9+
import { ObservableStream, spyOnConsole } from "../testing/internal";
1010
import { getDefaultOptionsForQueryManagerTests } from "../testing/core/mocking/mockQueryManager";
1111

1212
describe("GraphQL Subscriptions", () => {
@@ -47,62 +47,40 @@ describe("GraphQL Subscriptions", () => {
4747
};
4848
});
4949

50-
itAsync(
51-
"should start a subscription on network interface and unsubscribe",
52-
(resolve, reject) => {
53-
const link = mockObservableLink();
54-
// This test calls directly through Apollo Client
55-
const client = new ApolloClient({
56-
link,
57-
cache: new InMemoryCache({ addTypename: false }),
58-
});
50+
it("should start a subscription on network interface and unsubscribe", async () => {
51+
const link = mockObservableLink();
52+
// This test calls directly through Apollo Client
53+
const client = new ApolloClient({
54+
link,
55+
cache: new InMemoryCache({ addTypename: false }),
56+
});
5957

60-
let count = 0;
61-
const sub = client.subscribe(defaultOptions).subscribe({
62-
next(result) {
63-
count++;
64-
expect(result).toEqual(results[0].result);
58+
const stream = new ObservableStream(client.subscribe(defaultOptions));
59+
link.simulateResult(results[0]);
6560

66-
// Test unsubscribing
67-
if (count > 1) {
68-
throw new Error("next fired after unsubscribing");
69-
}
70-
sub.unsubscribe();
71-
resolve();
72-
},
73-
});
61+
await expect(stream).toEmitValue(results[0].result);
7462

75-
link.simulateResult(results[0]);
76-
}
77-
);
63+
stream.unsubscribe();
64+
});
7865

79-
itAsync("should subscribe with default values", (resolve, reject) => {
66+
it("should subscribe with default values", async () => {
8067
const link = mockObservableLink();
8168
// This test calls directly through Apollo Client
8269
const client = new ApolloClient({
8370
link,
8471
cache: new InMemoryCache({ addTypename: false }),
8572
});
8673

87-
let count = 0;
88-
const sub = client.subscribe(options).subscribe({
89-
next(result) {
90-
expect(result).toEqual(results[0].result);
74+
const stream = new ObservableStream(client.subscribe(options));
9175

92-
// Test unsubscribing
93-
if (count > 1) {
94-
throw new Error("next fired after unsubscribing");
95-
}
96-
sub.unsubscribe();
76+
link.simulateResult(results[0]);
9777

98-
resolve();
99-
},
100-
});
78+
await expect(stream).toEmitValue(results[0].result);
10179

102-
link.simulateResult(results[0]);
80+
stream.unsubscribe();
10381
});
10482

105-
itAsync("should multiplex subscriptions", (resolve, reject) => {
83+
it("should multiplex subscriptions", async () => {
10684
const link = mockObservableLink();
10785
const queryManager = new QueryManager(
10886
getDefaultOptionsForQueryManagerTests({
@@ -112,88 +90,57 @@ describe("GraphQL Subscriptions", () => {
11290
);
11391

11492
const obs = queryManager.startGraphQLSubscription(options);
115-
116-
let counter = 0;
117-
118-
// tslint:disable-next-line
119-
obs.subscribe({
120-
next(result) {
121-
expect(result).toEqual(results[0].result);
122-
counter++;
123-
if (counter === 2) {
124-
resolve();
125-
}
126-
},
127-
}) as any;
128-
129-
// Subscribe again. Should also receive the same result.
130-
// tslint:disable-next-line
131-
obs.subscribe({
132-
next(result) {
133-
expect(result).toEqual(results[0].result);
134-
counter++;
135-
if (counter === 2) {
136-
resolve();
137-
}
138-
},
139-
}) as any;
93+
const stream1 = new ObservableStream(obs);
94+
const stream2 = new ObservableStream(obs);
14095

14196
link.simulateResult(results[0]);
97+
98+
await expect(stream1).toEmitValue(results[0].result);
99+
await expect(stream2).toEmitValue(results[0].result);
142100
});
143101

144-
itAsync(
145-
"should receive multiple results for a subscription",
146-
(resolve, reject) => {
147-
const link = mockObservableLink();
148-
let numResults = 0;
149-
const queryManager = new QueryManager(
150-
getDefaultOptionsForQueryManagerTests({
151-
link,
152-
cache: new InMemoryCache({ addTypename: false }),
153-
})
154-
);
155-
156-
// tslint:disable-next-line
157-
queryManager.startGraphQLSubscription(options).subscribe({
158-
next(result) {
159-
expect(result).toEqual(results[numResults].result);
160-
numResults++;
161-
if (numResults === 4) {
162-
resolve();
163-
}
164-
},
165-
}) as any;
102+
it("should receive multiple results for a subscription", async () => {
103+
const link = mockObservableLink();
104+
const queryManager = new QueryManager(
105+
getDefaultOptionsForQueryManagerTests({
106+
link,
107+
cache: new InMemoryCache({ addTypename: false }),
108+
})
109+
);
166110

167-
for (let i = 0; i < 4; i++) {
168-
link.simulateResult(results[i]);
169-
}
111+
const stream = new ObservableStream(
112+
queryManager.startGraphQLSubscription(options)
113+
);
114+
115+
for (let i = 0; i < 4; i++) {
116+
link.simulateResult(results[i]);
170117
}
171-
);
172-
173-
itAsync(
174-
"should not cache subscription data if a `no-cache` fetch policy is used",
175-
(resolve, reject) => {
176-
const link = mockObservableLink();
177-
const cache = new InMemoryCache({ addTypename: false });
178-
const client = new ApolloClient({
179-
link,
180-
cache,
181-
});
182118

183-
expect(cache.extract()).toEqual({});
119+
await expect(stream).toEmitValue(results[0].result);
120+
await expect(stream).toEmitValue(results[1].result);
121+
await expect(stream).toEmitValue(results[2].result);
122+
await expect(stream).toEmitValue(results[3].result);
123+
await expect(stream).not.toEmitAnything();
124+
});
184125

185-
options.fetchPolicy = "no-cache";
186-
const sub = client.subscribe(options).subscribe({
187-
next() {
188-
expect(cache.extract()).toEqual({});
189-
sub.unsubscribe();
190-
resolve();
191-
},
192-
});
126+
it("should not cache subscription data if a `no-cache` fetch policy is used", async () => {
127+
const link = mockObservableLink();
128+
const cache = new InMemoryCache({ addTypename: false });
129+
const client = new ApolloClient({
130+
link,
131+
cache,
132+
});
193133

194-
link.simulateResult(results[0]);
195-
}
196-
);
134+
expect(cache.extract()).toEqual({});
135+
136+
options.fetchPolicy = "no-cache";
137+
const stream = new ObservableStream(client.subscribe(options));
138+
139+
link.simulateResult(results[0]);
140+
141+
await expect(stream).toEmitNext();
142+
expect(cache.extract()).toEqual({});
143+
});
197144

198145
it("should throw an error if the result has errors on it", () => {
199146
const link = mockObservableLink();
@@ -492,27 +439,22 @@ describe("GraphQL Subscriptions", () => {
492439
});
493440
});
494441

495-
itAsync(
496-
"should pass a context object through the link execution chain",
497-
(resolve, reject) => {
498-
const link = mockObservableLink();
499-
const client = new ApolloClient({
500-
cache: new InMemoryCache(),
501-
link,
502-
});
442+
it("should pass a context object through the link execution chain", async () => {
443+
const link = mockObservableLink();
444+
const client = new ApolloClient({
445+
cache: new InMemoryCache(),
446+
link,
447+
});
503448

504-
client.subscribe(options).subscribe({
505-
next() {
506-
expect(link.operation?.getContext().someVar).toEqual(
507-
options.context.someVar
508-
);
509-
resolve();
510-
},
511-
});
449+
const stream = new ObservableStream(client.subscribe(options));
512450

513-
link.simulateResult(results[0]);
514-
}
515-
);
451+
link.simulateResult(results[0]);
452+
453+
await expect(stream).toEmitNext();
454+
expect(link.operation?.getContext().someVar).toEqual(
455+
options.context.someVar
456+
);
457+
});
516458

517459
it("should throw an error if the result has protocolErrors on it", async () => {
518460
const link = mockObservableLink();

src/__tests__/optimistic.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe("optimistic mutation results", () => {
240240
});
241241

242242
it("handles errors produced by one mutation in a series", async () => {
243-
expect.assertions(12);
243+
expect.assertions(11);
244244
const client = await setup(
245245
{
246246
request: { query: mutation },
@@ -303,7 +303,7 @@ describe("optimistic mutation results", () => {
303303
});
304304

305305
it("can run 2 mutations concurrently and handles all intermediate states well", async () => {
306-
expect.assertions(36);
306+
expect.assertions(35);
307307
function checkBothMutationsAreApplied(
308308
expectedText1: any,
309309
expectedText2: any
@@ -466,7 +466,7 @@ describe("optimistic mutation results", () => {
466466
});
467467

468468
it("handles errors produced by one mutation in a series", async () => {
469-
expect.assertions(12);
469+
expect.assertions(11);
470470
const client = await setup(
471471
{
472472
request: { query: mutation },
@@ -528,7 +528,7 @@ describe("optimistic mutation results", () => {
528528
});
529529

530530
it("can run 2 mutations concurrently and handles all intermediate states well", async () => {
531-
expect.assertions(36);
531+
expect.assertions(35);
532532
function checkBothMutationsAreApplied(
533533
expectedText1: any,
534534
expectedText2: any
@@ -831,7 +831,7 @@ describe("optimistic mutation results", () => {
831831
});
832832

833833
it("will use a passed variable in optimisticResponse", async () => {
834-
expect.assertions(8);
834+
expect.assertions(7);
835835
const client = await setup({
836836
request: { query: mutation, variables },
837837
result: mutationResult,
@@ -893,7 +893,7 @@ describe("optimistic mutation results", () => {
893893
});
894894

895895
it("will not update optimistically if optimisticResponse returns IGNORE sentinel object", async () => {
896-
expect.assertions(7);
896+
expect.assertions(6);
897897

898898
const client = await setup({
899899
request: { query: mutation, variables },
@@ -1068,7 +1068,7 @@ describe("optimistic mutation results", () => {
10681068
};
10691069

10701070
it("will insert a single itemAsync to the beginning", async () => {
1071-
expect.assertions(9);
1071+
expect.assertions(8);
10721072
const client = await setup({
10731073
request: { query: mutation },
10741074
result: mutationResult,
@@ -1116,7 +1116,7 @@ describe("optimistic mutation results", () => {
11161116
});
11171117

11181118
it("two array insert like mutations", async () => {
1119-
expect.assertions(11);
1119+
expect.assertions(10);
11201120
const client = await setup(
11211121
{
11221122
request: { query: mutation },
@@ -1198,7 +1198,7 @@ describe("optimistic mutation results", () => {
11981198
});
11991199

12001200
it("two mutations, one fails", async () => {
1201-
expect.assertions(12);
1201+
expect.assertions(11);
12021202
const client = await setup(
12031203
{
12041204
request: { query: mutation },
@@ -1452,7 +1452,7 @@ describe("optimistic mutation results", () => {
14521452
};
14531453

14541454
it("will insert a single itemAsync to the beginning", async () => {
1455-
expect.assertions(8);
1455+
expect.assertions(7);
14561456
const client = await setup({
14571457
request: { query: mutation },
14581458
delay: 300,
@@ -1510,7 +1510,7 @@ describe("optimistic mutation results", () => {
15101510
});
15111511

15121512
it("two array insert like mutations", async () => {
1513-
expect.assertions(11);
1513+
expect.assertions(10);
15141514
const client = await setup(
15151515
{
15161516
request: { query: mutation },
@@ -1610,7 +1610,7 @@ describe("optimistic mutation results", () => {
16101610
});
16111611

16121612
it("two mutations, one fails", async () => {
1613-
expect.assertions(12);
1613+
expect.assertions(11);
16141614
const client = await setup(
16151615
{
16161616
request: { query: mutation },
@@ -2299,7 +2299,7 @@ describe("optimistic mutation - githunt comments", () => {
22992299
};
23002300

23012301
it("can post a new comment", async () => {
2302-
expect.assertions(3);
2302+
expect.assertions(2);
23032303
const mutationVariables = {
23042304
repoFullName: "org/repo",
23052305
commentContent: "New Comment",

0 commit comments

Comments
 (0)