Skip to content

Commit 206c6b9

Browse files
committed
fix mock interfearance in tests
1 parent 7108ed4 commit 206c6b9

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

packages/gator-permissions-snap/test/client/accountApiClient.test.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,8 @@ describe('AccountApiClient', () => {
766766
});
767767

768768
it('retries up to the specified number of attempts', async () => {
769-
// All calls fail with 500 status - provide 8 mock responses (4 for balance API + 4 for metadata API)
770-
for (let i = 0; i < 8; i++) {
769+
// All calls fail with 500 status - provide 7 mock responses (4 for balance API + 3 for metadata API)
770+
for (let i = 0; i < 7; i++) {
771771
mockFetch.mockResolvedValueOnce({
772772
ok: false,
773773
status: 500,
@@ -793,28 +793,44 @@ describe('AccountApiClient', () => {
793793
});
794794

795795
it('uses default retry options when none provided', async () => {
796+
// Create a completely fresh mock for this test to avoid interference
797+
const freshMockFetch = jest.fn();
798+
799+
// Recreate the client with the fresh mock
800+
client = new AccountApiClient({
801+
accountBaseUrl: mockApiBaseUrl,
802+
tokensBaseUrl: mockTokensApiBaseUrl,
803+
fetch: freshMockFetch,
804+
timeoutMs: 5000, // Shorter timeout for tests
805+
maxResponseSizeBytes: 1024 * 1024, // 1MB
806+
});
807+
796808
// Mock balance API - first call fails with 500 status
797-
mockFetch.mockResolvedValueOnce({
809+
freshMockFetch.mockResolvedValueOnce({
798810
ok: false,
799811
status: 500,
800812
headers: {
801813
get: jest.fn().mockReturnValue('1024'),
802814
},
803-
// No json method - this will cause the httpClient to throw the 500 error before trying to parse JSON
815+
json: async () => {
816+
throw new Error('Server error: 500');
817+
},
804818
});
805819

806820
// Mock metadata API - first call fails with 500 status
807-
mockFetch.mockResolvedValueOnce({
821+
freshMockFetch.mockResolvedValueOnce({
808822
ok: false,
809823
status: 500,
810824
headers: {
811825
get: jest.fn().mockReturnValue('1024'),
812826
},
813-
// No json method - this will cause the httpClient to throw the 500 error before trying to parse JSON
827+
json: async () => {
828+
throw new Error('Server error: 500');
829+
},
814830
});
815831

816832
// Mock balance API - second call succeeds
817-
mockFetch.mockResolvedValueOnce({
833+
freshMockFetch.mockResolvedValueOnce({
818834
ok: true,
819835
headers: {
820836
get: jest.fn().mockReturnValue('1024'),
@@ -839,7 +855,7 @@ describe('AccountApiClient', () => {
839855
});
840856

841857
// Mock metadata API - second call succeeds
842-
mockFetch.mockResolvedValueOnce({
858+
freshMockFetch.mockResolvedValueOnce({
843859
ok: true,
844860
headers: {
845861
get: jest.fn().mockReturnValue('1024'),
@@ -868,7 +884,7 @@ describe('AccountApiClient', () => {
868884
'https://dev-static.cx.metamask.io/api/v1/tokenIcons/1/0x0000000000000000000000000000000000000000.png',
869885
});
870886

871-
expect(mockFetch).toHaveBeenCalledTimes(4); // 2 balance calls + 2 metadata calls
887+
expect(freshMockFetch).toHaveBeenCalledTimes(4); // 2 balance calls + 2 metadata calls (with default retry)
872888
});
873889

874890
it('succeeds on first attempt when no retry is needed', async () => {

0 commit comments

Comments
 (0)