Skip to content

Commit

Permalink
remove redundant jest helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bangtoven committed Sep 17, 2024
1 parent ea5a805 commit d6d83f7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
14 changes: 0 additions & 14 deletions packages/wallet-sdk/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,3 @@ global.TextEncoder = TextEncoder;

// @ts-expect-error Use util TextDecoder
global.TextDecoder = TextDecoder;

expect.extend({
toThrowEIPError(received, code, message = expect.any(String)) {
const expected = expect.objectContaining({
code,
message,
});
return {
pass: this.equals(received, expected),
message: () =>
this.utils.printDiffOrStringify(expected, received, 'Expected', 'Received', true),
};
},
});
38 changes: 19 additions & 19 deletions packages/wallet-sdk/src/CoinbaseWalletProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ describe('Request Handling', () => {
});

it('throws error when handling invalid request', async () => {
await expect(provider.request({} as RequestArguments)).rejects.toThrowEIPError(
standardErrorCodes.rpc.invalidParams,
"'args.method' must be a non-empty string."
);
await expect(provider.request({} as RequestArguments)).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: "'args.method' must be a non-empty string.",
});
});

it('throws error for requests with unsupported or deprecated method', async () => {
const deprecated = ['eth_sign', 'eth_signTypedData_v2'];
const unsupported = ['eth_subscribe', 'eth_unsubscribe'];

for (const method of [...deprecated, ...unsupported]) {
await expect(provider.request({ method })).rejects.toThrowEIPError(
standardErrorCodes.provider.unsupportedMethod
);
await expect(provider.request({ method })).rejects.toMatchObject({
code: standardErrorCodes.provider.unsupportedMethod,
});
}
});
});
Expand Down Expand Up @@ -131,10 +131,10 @@ describe('Signer configuration', () => {
const error = new Error('Signer selection failed');
mockFetchSignerType.mockRejectedValue(error);

await expect(provider.request({ method: 'eth_requestAccounts' })).rejects.toThrowEIPError(
standardErrorCodes.rpc.internal,
error.message
);
await expect(provider.request({ method: 'eth_requestAccounts' })).rejects.toMatchObject({
code: standardErrorCodes.rpc.internal,
message: error.message,
});
expect(mockHandshake).not.toHaveBeenCalled();
expect(mockStoreSignerType).not.toHaveBeenCalled();
});
Expand All @@ -144,10 +144,10 @@ describe('Signer configuration', () => {
mockFetchSignerType.mockResolvedValue('scw');
mockHandshake.mockRejectedValue(error);

await expect(provider.request({ method: 'eth_requestAccounts' })).rejects.toThrowEIPError(
standardErrorCodes.rpc.internal,
error.message
);
await expect(provider.request({ method: 'eth_requestAccounts' })).rejects.toMatchObject({
code: standardErrorCodes.rpc.internal,
message: error.message,
});
expect(mockHandshake).toHaveBeenCalled();
expect(mockStoreSignerType).not.toHaveBeenCalled();
});
Expand All @@ -169,10 +169,10 @@ describe('Signer configuration', () => {
});

it('should throw error if signer is not initialized', async () => {
await expect(provider.request({ method: 'personal_sign' })).rejects.toThrowEIPError(
standardErrorCodes.provider.unauthorized,
`Must call 'eth_requestAccounts' before other methods`
);
await expect(provider.request({ method: 'personal_sign' })).rejects.toMatchObject({
code: standardErrorCodes.provider.unauthorized,
message: `Must call 'eth_requestAccounts' before other methods`,
});
});

it('should set signer to null', async () => {
Expand Down
55 changes: 32 additions & 23 deletions packages/wallet-sdk/src/sign/walletlink/WalletLinkSigner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ describe('LegacyProvider', () => {
method: 'personal_sign',
params: ['0x123456789abcdef', 'Super safe message'],
})
).rejects.toThrowEIPError(
standardErrorCodes.rpc.invalidParams,
'Invalid Ethereum address: Super safe message'
);
).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'Invalid Ethereum address: Super safe message',
});
});
});

Expand Down Expand Up @@ -425,10 +425,10 @@ describe('LegacyProvider', () => {
method: 'wallet_addEthereumChain',
params: [{}],
});
}).rejects.toThrowEIPError(
standardErrorCodes.rpc.invalidParams,
'chainName is a required field'
);
}).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'chainName is a required field',
});
});

test('wallet_addEthereumChain native currency', async () => {
Expand All @@ -442,10 +442,10 @@ describe('LegacyProvider', () => {
},
],
});
}).rejects.toThrowEIPError(
standardErrorCodes.rpc.invalidParams,
'nativeCurrency is a required field'
);
}).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'nativeCurrency is a required field',
});
});

test('wallet_switchEthereumChain', async () => {
Expand Down Expand Up @@ -476,10 +476,10 @@ describe('LegacyProvider', () => {
},
],
});
}).rejects.toThrowEIPError(
standardErrorCodes.provider.unsupportedChain,
expect.stringContaining('Unrecognized chain ID')
);
}).rejects.toMatchObject({
code: standardErrorCodes.provider.unsupportedChain,
message: expect.stringContaining('Unrecognized chain ID'),
});
});

test('wallet_watchAsset', async () => {
Expand All @@ -503,7 +503,10 @@ describe('LegacyProvider', () => {
method: 'wallet_watchAsset',
params: [{}],
})
).rejects.toThrowEIPError(standardErrorCodes.rpc.invalidParams, 'Type is required');
).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'Type is required',
});
});

test('wallet_watchAsset w/o valid asset type', async () => {
Expand All @@ -516,10 +519,10 @@ describe('LegacyProvider', () => {
},
],
})
).rejects.toThrowEIPError(
standardErrorCodes.rpc.invalidParams,
"Asset of type 'ERC721' is not supported"
);
).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: "Asset of type 'ERC721' is not supported",
});
});

test('wallet_watchAsset to throw option required error', async () => {
Expand All @@ -532,7 +535,10 @@ describe('LegacyProvider', () => {
},
],
})
).rejects.toThrowEIPError(standardErrorCodes.rpc.invalidParams, 'Options are required');
).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'Options are required',
});
});

test('wallet_watchAsset to throw address required error', async () => {
Expand All @@ -546,7 +552,10 @@ describe('LegacyProvider', () => {
},
],
})
).rejects.toThrowEIPError(standardErrorCodes.rpc.invalidParams, 'Address is required');
).rejects.toMatchObject({
code: standardErrorCodes.rpc.invalidParams,
message: 'Address is required',
});
});
});
});

0 comments on commit d6d83f7

Please sign in to comment.