Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-cooks-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Default `MockedProvider` to keep Apollo Client DevTools disabled unless explicitly configured.
2 changes: 1 addition & 1 deletion src/testing/react/MockedProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class MockedProvider extends React.Component<
defaultOptions: mockLinkDefaultOptions,
}),
localState,
devtools,
devtools: devtools ?? { enabled: false },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't solve the actual problem.
Any other ApolloClient instance created in similar situations will still cause the same result.

});

this.state = {
Expand Down
25 changes: 25 additions & 0 deletions src/testing/react/__tests__/MockedProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ describe("General use", () => {
errorThrown = false;
});

it("should not connect to Apollo Client DevTools by default", () => {
delete (window as any).__APOLLO_CLIENT__;

const provider = new MockedProvider({});

expect((provider as any).state.client.devtoolsConfig.enabled).toBe(false);
expect((window as any).__APOLLO_CLIENT__).toBeUndefined();

provider.componentWillUnmount();
});

it("should allow Apollo Client DevTools to be enabled", () => {
jest.useFakeTimers();
delete (window as any).__APOLLO_CLIENT__;

const provider = new MockedProvider({ devtools: { enabled: true } });

expect((provider as any).state.client.devtoolsConfig.enabled).toBe(true);
expect((window as any).__APOLLO_CLIENT__).toBeDefined();

delete (window as any).__APOLLO_CLIENT__;
provider.componentWillUnmount();
jest.useRealTimers();
Comment on lines +104 to +115

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
fd -a 'MockedProvider.test.tsx|MockedProvider.tsx|.*devtools.*' . | sed 's#^\./##' | head -100

echo "== target file outline/size =="
if [ -f src/testing/react/__tests__/MockedProvider.test.tsx ]; then
  wc -l src/testing/react/__tests__/MockedProvider.test.tsx
  ast-grep outline src/testing/react/__tests__/MockedProvider.test.tsx || true
  sed -n '1,180p' src/testing/react/__tests__/MockedProvider.test.tsx
fi

echo "== related devtools/MockedProvider sources =="
rg -n "devtoolsConfig|__APOLLO_CLIENT__|devtools:|setTimeout|setInterval|teardown|useFakeTimers|useRealTimers" src testing . -g '!node_modules' -g '!dist' -g '!build' | head -200

Repository: apollographql/apollo-client

Length of output: 25398


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
p = Path('src/testing/react/__tests__/MockedProvider.test.tsx')
if not p.exists():
    print("target_missing")
    raise SystemExit
s = p.read_text()
for needle in ['delete (window as any).__APOLLO_CLIENT__', 'MockedProvider({ devtools: { enabled: true } })', '.flushAllTimers', 'runOnlyPendingTimers', 'runOnlyPendingTimersAsync', 'flushAllTimersAsync', 'useRealTimers', "afterEach"]:
    print(f"{needle!r}: {needle in s}")
# Print relevant block with line numbers.
lines = s.splitlines()
for i,l in enumerate(lines,1):
    if 100 <= i <= 125:
        print(f"{i}: {l}")
PY

Repository: apollographql/apollo-client

Length of output: 1394


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== ApolloClient devtools code =="
sed -n '1080,1215p' src/core/ApolloClient.ts

echo "== MockedProvider code =="
sed -n '1,105p' src/testing/react/MockedProvider.tsx

echo "== enableFakeTimers helper =="
sed -n '1,80p' src/testing/internal/disposables/enableFakeTimers.ts

echo "== setup fake timers globally if present =="
sed -n '1,140p' src/config/jest/setup.ts

Repository: apollographql/apollo-client

Length of output: 10089


Flush the DevTools fallback timer while window is still active.

connectToDevTools() schedules the 10s fallback timeout before MockedProvider can call client.stop(), but this test never advances or asserts pending timers before jsdom teardown. Run pending timers and assert they clean up while window is still available, and restore real timers in a finally block so assertion failures cannot leave other tests running on fake timers.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/testing/react/__tests__/MockedProvider.test.tsx` around lines 104 - 115,
Update the “should allow Apollo Client DevTools to be enabled” test to flush the
pending DevTools fallback timer and assert timer cleanup while window is still
active, after unmounting the MockedProvider. Wrap fake-timer setup and test
execution in a finally block that always restores real timers, preserving the
existing DevTools and global-client assertions.

});

it("should mock the data", async () => {
using _disabledAct = disableActEnvironment();
const { takeSnapshot } = await renderHookToSnapshotStream(
Expand Down