diff --git a/packages/sdk/test/common/constants.ts b/packages/sdk/test/common/constants.ts index 7fd212f..a4764f3 100644 --- a/packages/sdk/test/common/constants.ts +++ b/packages/sdk/test/common/constants.ts @@ -54,6 +54,4 @@ export const BLOCK_NUMBER_MAINNET = BigInt(21020558); export const BLOCK_NUMBER_ARBITRUM = BigInt(266447962); export const TENDERLY_KEY = getMaybeEnv("VITE_TENDERLY_KEY"); -export const MOCK_API = getMaybeEnv("VITE_MOCK_API") - ? Boolean(getMaybeEnv("VITE_MOCK_API")) - : true; +export const MOCK_API = getMaybeEnv("VITE_MOCK_API") === "true"; diff --git a/packages/sdk/test/mocks/handlers.ts b/packages/sdk/test/mocks/handlers.ts index b21a541..5b9e222 100644 --- a/packages/sdk/test/mocks/handlers.ts +++ b/packages/sdk/test/mocks/handlers.ts @@ -7,42 +7,39 @@ import { usdcMainnetArbitrumFees as fees, } from "./data"; import { getCurrentTimeSeconds } from "../../src"; -import { MOCK_API } from "../common/constants"; -export const handlers = MOCK_API - ? [ - // getAvailableRoutes - http.get(`${TEST_BASE_URL}/available-routes`, async ({ request }) => { - const url = new URL(request.url); - console.log(url); - return HttpResponse.json(route); - }), - // getSupportedChains - http.get(`${TEST_BASE_URL}/chains`, async ({ request }) => { - const url = new URL(request.url); - const givenChainIds = new Set( - url.searchParams.getAll("chainId").map(Number), - ); - const expectedChainIds = new Set( - MAINNET_SUPPORTED_CHAINS.map((chain) => chain.id), - ); - // check if client has in-fact fetched chain info for ALL chains, if not we force test to fail downstream - if (givenChainIds.size !== expectedChainIds.size) { - return HttpResponse.json({ - status: 500, - data: undefined, - }); - } - return HttpResponse.json(mainnetChainInfo); - }), - // getSuggestedFees - http.get(`${TEST_BASE_URL}/suggested-fees`, async ({ request }) => { - console.log(request.url); - const data = { - ...fees, - timestamp: getCurrentTimeSeconds(), - }; - return HttpResponse.json(data); - }), - ] - : []; +export const handlers = [ + // getAvailableRoutes + http.get(`${TEST_BASE_URL}/available-routes`, async ({ request }) => { + const url = new URL(request.url); + console.log(url); + return HttpResponse.json(route); + }), + // getSupportedChains + http.get(`${TEST_BASE_URL}/chains`, async ({ request }) => { + const url = new URL(request.url); + const givenChainIds = new Set( + url.searchParams.getAll("chainId").map(Number), + ); + const expectedChainIds = new Set( + MAINNET_SUPPORTED_CHAINS.map((chain) => chain.id), + ); + // check if client has in-fact fetched chain info for ALL chains, if not we force test to fail downstream + if (givenChainIds.size !== expectedChainIds.size) { + return HttpResponse.json({ + status: 500, + data: undefined, + }); + } + return HttpResponse.json(mainnetChainInfo); + }), + // getSuggestedFees + http.get(`${TEST_BASE_URL}/suggested-fees`, async ({ request }) => { + console.log(request.url); + const data = { + ...fees, + timestamp: getCurrentTimeSeconds(), + }; + return HttpResponse.json(data); + }), +]; diff --git a/packages/sdk/test/mocks/setup.ts b/packages/sdk/test/mocks/setup.ts index 5dd6516..67fc9cb 100644 --- a/packages/sdk/test/mocks/setup.ts +++ b/packages/sdk/test/mocks/setup.ts @@ -1,11 +1,16 @@ import { setupServer } from "msw/node"; import { handlers } from "./handlers"; import { afterAll, afterEach, beforeAll } from "vitest"; +import { MOCK_API } from "../common/constants"; const server = setupServer(...handlers); // set up -beforeAll(() => server.listen({ onUnhandledRequest: "bypass" })); +beforeAll(() => { + if (MOCK_API) { + server.listen({ onUnhandledRequest: "bypass" }); + } +}); // tear down afterAll(() => server.close());