Skip to content

Commit dd1cdd4

Browse files
committed
mute deprecation warnings in tests, fix one missed invariant warning
1 parent 7420783 commit dd1cdd4

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

src/core/__tests__/ObservableQuery.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import { expectTypeOf } from "expect-type";
2727
import { SubscriptionObserver } from "zen-observable-ts";
2828
import { waitFor } from "@testing-library/react";
2929
import { ObservableStream, spyOnConsole } from "../../testing/internal";
30-
import { muteDeprecations } from "../../utilities/deprecation";
30+
import {
31+
muteDeprecations,
32+
withDisabledDeprecations,
33+
} from "../../utilities/deprecation";
3134

3235
export const mockFetchQuery = (queryManager: QueryManager<any>) => {
3336
const fetchConcastWithInfo = queryManager["fetchConcastWithInfo"];
@@ -1827,6 +1830,7 @@ describe("ObservableQuery", () => {
18271830

18281831
it("should warn if passed { variables } and query does not declare $variables", async () => {
18291832
using _ = spyOnConsole("warn");
1833+
using __ = withDisabledDeprecations();
18301834

18311835
const queryWithVarsVar = gql`
18321836
query QueryWithVarsVar($vars: [String!]) {

src/link/core/ApolloLink.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
createOperation,
1515
transformOperation,
1616
} from "../utils/index.js";
17+
import { warnDeprecated } from "../../utilities/deprecation/index.js";
1718

1819
function passthrough(op: Operation, forward: NextLink) {
1920
return (forward ? forward(op) : Observable.of()) as Observable<FetchResult>;
@@ -145,9 +146,11 @@ export class ApolloLink {
145146
observer?: Observer<FetchResult>
146147
): false | void {
147148
if (__DEV__) {
148-
invariant.warn(
149-
"[ApolloLink] `onError` is deprecated and will be removed with Apollo Client 4.0. Please discontinue using it."
150-
);
149+
warnDeprecated("onError", () => {
150+
invariant.warn(
151+
"[ApolloLink] `onError` is deprecated and will be removed with Apollo Client 4.0. Please discontinue using it."
152+
);
153+
});
151154
}
152155
if (observer && observer.error) {
153156
observer.error(error);

src/testing/react/__tests__/MockedProvider.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { QueryResult } from "../../../react/types/types";
1111
import { ApolloLink, FetchResult } from "../../../link/core";
1212
import { Observable } from "zen-observable-ts";
1313
import { ApolloError } from "../../../errors";
14+
import { withDisabledDeprecations } from "../../../utilities/deprecation";
1415

1516
const variables = {
1617
username: "mock_username",
@@ -870,6 +871,8 @@ describe("General use", () => {
870871

871872
it("shows a warning in the console when there is no matched mock", async () => {
872873
const consoleSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
874+
875+
using _ = withDisabledDeprecations();
873876
let finished = false;
874877
function Component({ ...variables }: Variables) {
875878
const { loading } = useQuery<Data, Variables>(query, { variables });
@@ -915,6 +918,7 @@ describe("General use", () => {
915918

916919
it("silences console warning for unmatched mocks when `showWarnings` is `false`", async () => {
917920
const consoleSpy = jest.spyOn(console, "warn");
921+
using _ = withDisabledDeprecations();
918922
let finished = false;
919923
function Component({ ...variables }: Variables) {
920924
const { loading } = useQuery<Data, Variables>(query, { variables });
@@ -957,6 +961,7 @@ describe("General use", () => {
957961

958962
it("silences console warning for unmatched mocks when passing `showWarnings` to `MockLink` directly", async () => {
959963
const consoleSpy = jest.spyOn(console, "warn");
964+
using _ = withDisabledDeprecations();
960965
let finished = false;
961966
function Component({ ...variables }: Variables) {
962967
const { loading } = useQuery<Data, Variables>(query, { variables });

src/utilities/deprecation/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Slot } from "optimism";
2-
import { invariant, global } from "../globals/index.js";
2+
import { invariant, global as untypedGlobal } from "../globals/index.js";
3+
4+
const muteAllDeprecations = Symbol.for("apollo.deprecations");
5+
const global = untypedGlobal as { [muteAllDeprecations]?: boolean };
36

47
const slot = new Slot<string[]>();
58

@@ -92,10 +95,7 @@ export type DeprecationName =
9295
| NonNullable<PossibleDeprecations[keyof PossibleDeprecations]>[number];
9396

9497
function isMuted(name: DeprecationName) {
95-
return (
96-
!!(global as any)[Symbol.for("apollo.deprecations")] ||
97-
(slot.getValue() || []).includes(name)
98-
);
98+
return global[muteAllDeprecations] || (slot.getValue() || []).includes(name);
9999
}
100100

101101
export function muteDeprecations<TResult, TArgs extends any[], TThis = any>(
@@ -131,3 +131,13 @@ export function warnDeprecated(name: DeprecationName, cb: () => void) {
131131
cb();
132132
}
133133
}
134+
135+
export function withDisabledDeprecations() {
136+
const prev = global[muteAllDeprecations];
137+
global[muteAllDeprecations] = true;
138+
return {
139+
[Symbol.dispose]() {
140+
global[muteAllDeprecations] = prev;
141+
},
142+
};
143+
}

0 commit comments

Comments
 (0)