Skip to content

Commit cef5d64

Browse files
fix(x-client): mock external API calls in x-client tests
Tests were making real HTTP calls to api.x.immutable.com, causing failures in CI: starkCurve tests got undefined responses, and imx tests crashed Jest workers due to circular req/res references in Axios responses. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 281bdf2 commit cef5d64

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

packages/x-client/src/imx.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,22 @@ describe('IMXClient', () => {
3737
});
3838

3939
it('should instantiate a PRODUCTION IMXClient', async () => {
40+
let axiosRequest: AxiosRequestConfig = {};
41+
const adapter = jest.fn().mockImplementation(
42+
async (request: AxiosRequestConfig) => {
43+
axiosRequest = request;
44+
return { status: 200 };
45+
},
46+
);
47+
4048
const config = new ImxConfiguration({
4149
baseConfig: { environment: Environment.PRODUCTION },
4250
});
4351
const { assetApi } = new IMXClient(config);
44-
const assetsResponse = await assetApi.listAssets();
52+
const assetsResponse = await assetApi.listAssets({}, { adapter });
4553

4654
expect(assetsResponse.status).toEqual(200);
47-
expect(assetsResponse.config.headers?.['x-sdk-version']).toContain(
55+
expect(axiosRequest.headers?.['x-sdk-version']).toContain(
4856
'ts-immutable-sdk',
4957
);
5058
});

packages/x-client/src/utils/stark/starkCurve.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
} from './starkCurve';
1010
import { grindKey } from './legacy/crypto';
1111
import { createStarkSigner } from './starkSigner';
12+
import { getStarkPublicKeyFromImx } from './getStarkPublicKeyFromImx';
13+
14+
jest.mock('./getStarkPublicKeyFromImx');
1215

1316
describe('Key generation', () => {
1417
it('should generate random Stark key', async () => {
@@ -114,6 +117,13 @@ describe('Key generation', () => {
114117
];
115118
tests.forEach((test) => {
116119
it(test.name, async () => {
120+
const isAccountNotFound = test.name.includes('account not found');
121+
(getStarkPublicKeyFromImx as jest.Mock).mockResolvedValue(
122+
isAccountNotFound
123+
? { starkPublicKey: '', accountNotFound: true }
124+
: { starkPublicKey: test.wantPublicKey, accountNotFound: false },
125+
);
126+
117127
const expectedStarkPubKeyBN = new BN(
118128
encUtils.removeHexPrefix(test.wantPublicKey),
119129
16,

0 commit comments

Comments
 (0)