Skip to content

Commit a94da92

Browse files
fix(checkout): stop replacing entire window object in connect tests
The window spy mock replaced window with a minimal object missing URL, document, and other globals. Leaked async operations (getAllBalances -> RemoteConfigFetcher) that use XMLHttpRequest would crash in CI because jsdom couldn't find URL on the mocked window. Now we only mock window.ethereum directly, preserving the real window globals. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 406840c commit a94da92

1 file changed

Lines changed: 24 additions & 34 deletions

File tree

packages/checkout/sdk/src/connect/connect.test.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,26 @@ import { WrappedBrowserProvider, WalletAction, WalletProviderName } from '../typ
88
import { CheckoutErrorType } from '../errors';
99
import { createProvider } from '../provider';
1010

11-
let windowSpy: any;
12-
1311
describe('connect', () => {
1412
const providerRequestMock: jest.Mock = jest.fn();
13+
let originalEthereum: any;
14+
1515
beforeEach(() => {
16-
windowSpy = jest.spyOn(window, 'window', 'get');
17-
18-
windowSpy.mockImplementation(() => ({
19-
ethereum: {
20-
request: providerRequestMock,
21-
isMetaMask: true,
22-
on: jest.fn(),
23-
removeListener: jest.fn(),
24-
},
25-
dispatchEvent: jest.fn(),
26-
addEventListener: jest.fn(),
27-
removeEventListener: jest.fn(),
28-
}));
16+
originalEthereum = (window as any).ethereum;
17+
(window as any).ethereum = {
18+
request: providerRequestMock,
19+
isMetaMask: true,
20+
on: jest.fn(),
21+
removeListener: jest.fn(),
22+
};
2923
});
3024

3125
afterEach(() => {
32-
windowSpy.mockRestore();
26+
if (originalEthereum === undefined) {
27+
delete (window as any).ethereum;
28+
} else {
29+
(window as any).ethereum = originalEthereum;
30+
}
3331
});
3432

3533
describe('checkIsWalletConnected', () => {
@@ -129,16 +127,11 @@ describe('connect', () => {
129127
});
130128

131129
it('should throw an error if the user rejects the connection request', async () => {
132-
windowSpy.mockImplementation(() => ({
133-
ethereum: {
134-
request: jest
135-
.fn()
136-
.mockRejectedValue(new Error('User rejected request')),
137-
},
138-
removeEventListener: jest.fn(),
139-
addEventListener: jest.fn(),
140-
dispatchEvent: jest.fn(),
141-
}));
130+
(window as any).ethereum = {
131+
request: jest
132+
.fn()
133+
.mockRejectedValue(new Error('User rejected request')),
134+
};
142135

143136
const { provider } = await createProvider(WalletProviderName.METAMASK);
144137

@@ -182,14 +175,11 @@ describe('connect', () => {
182175
});
183176

184177
it('should throw an error if the user rejects the permission request', async () => {
185-
windowSpy.mockImplementation(() => ({
186-
ethereum: {
187-
request: jest
188-
.fn()
189-
.mockRejectedValue(new Error('User rejected request')),
190-
},
191-
removeEventListener: () => {},
192-
}));
178+
(window as any).ethereum = {
179+
request: jest
180+
.fn()
181+
.mockRejectedValue(new Error('User rejected request')),
182+
};
193183

194184
const { provider } = await createProvider(WalletProviderName.METAMASK);
195185

0 commit comments

Comments
 (0)