Skip to content
Merged
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
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const baseConfig = {
global: {
branches: 70.68,
functions: 72.32,
lines: 71.27,
statements: 71.39,
lines: 71.39,
statements: 71.51,
},
},

Expand Down
21 changes: 21 additions & 0 deletions src/initializeInpageProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ describe('setGlobalProvider', () => {
new Event('ethereum#initialized'),
);
});

it('should not throw an error if the global ethereum provider is already set', () => {
const errorSpy = jest.spyOn(console, 'error');

const mockProvider = {} as unknown as MetaMaskInpageProvider;
Object.defineProperty(window, 'ethereum', {
get() {
return {};
},
set() {
throw new Error('window.ethereum already set');
},
configurable: false,
});
expect(() => setGlobalProvider(mockProvider)).not.toThrow();

expect(errorSpy).toHaveBeenCalledWith(
'MetaMask encountered an error setting the global Ethereum provider - this is likely due to another Ethereum wallet extension also setting the global Ethereum provider:',
expect.any(Error),
);
});
});

describe('announceCaip294WalletData', () => {
Expand Down
11 changes: 9 additions & 2 deletions src/initializeInpageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@ export function initializeProvider({
export function setGlobalProvider(
providerInstance: MetaMaskInpageProvider,
): void {
(window as Record<string, any>).ethereum = providerInstance;
window.dispatchEvent(new Event('ethereum#initialized'));
try {
(window as Record<string, any>).ethereum = providerInstance;
window.dispatchEvent(new Event('ethereum#initialized'));
} catch (error) {
console.error(
'MetaMask encountered an error setting the global Ethereum provider - this is likely due to another Ethereum wallet extension also setting the global Ethereum provider:',
error,
);
}
}

/**
Expand Down
Loading