Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
Eliminate jest-fetch-mock-fork (#2173)
Browse files Browse the repository at this point in the history
# Summary

In this massive PR we eliminate the `fetch` mocking framework. The _only_ module that should be mocking all the way down to `fetch` is the module that depends directly on `fetch` – `@solana/rpc-transport-http`. Everything else should be able to mock one or several layers higher.

Closes #1867.
  • Loading branch information
steveluscher authored Feb 24, 2024
1 parent 9f08044 commit ac2c1c8
Show file tree
Hide file tree
Showing 81 changed files with 469 additions and 452 deletions.
2 changes: 2 additions & 0 deletions packages/compat/src/__tests__/transaction-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Buffer } from 'node:buffer';

import { Address } from '@solana/addresses';
import { AccountRole, IInstruction } from '@solana/instructions';
import { SignatureBytes } from '@solana/keys';
Expand Down
6 changes: 2 additions & 4 deletions packages/library-legacy-sham/src/__tests__/public-key-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ describe('PublicKeySham', () => {
expect(publicKey.encode()).toEqual(Buffer.from(VALID_ADDRESS_BYTES));
});
} else {
// FIXME(https://github.com/solana-labs/solana-web3.js/issues/1867)
it.failing('fatals', () => {
it('fatals', () => {
expect(() => {
publicKey.encode();
}).toThrow();
Expand Down Expand Up @@ -73,8 +72,7 @@ describe('PublicKeySham', () => {
expect(publicKey.toBuffer()).toEqual(Buffer.from(VALID_ADDRESS_BYTES));
});
} else {
// FIXME(https://github.com/solana-labs/solana-web3.js/issues/1867)
it.failing('fatals', () => {
it('fatals', () => {
expect(() => {
publicKey.toBuffer();
}).toThrow();
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@solana/tsconfig": "workspace:*",
"@swc/jest": "^0.2.29",
"@types/jest": "^29.5.11",
"@types/node": "^20.11.20",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.3.0",
"agadoo": "^3.0.0",
Expand All @@ -90,7 +91,6 @@
"eslint-plugin-sort-keys-fix": "^1.1.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-fetch-mock-fork": "^3.0.4",
"jest-runner-eslint": "^2.1.2",
"jest-runner-prettier": "^1.0.0",
"prettier": "^3.1",
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-account-info-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Address } from '@solana/addresses';
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetAccountInfoApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';
Expand All @@ -14,8 +13,6 @@ const CONTEXT_MATCHER = expect.objectContaining({
describe('getAccountInfo', () => {
let rpc: Rpc<GetAccountInfoApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-balance-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import type { Address } from '@solana/addresses';
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetBalanceApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getBalance', () => {
let rpc: Rpc<GetBalanceApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-block-height.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetBlockHeightApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getBlockHeight', () => {
let rpc: Rpc<GetBlockHeightApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});
(['confirmed', 'finalized', 'processed'] as Commitment[]).forEach(commitment => {
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-block-production-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Address } from '@solana/addresses';
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetBlockProductionApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';
Expand All @@ -14,8 +13,6 @@ const CONTEXT_MATCHER = expect.objectContaining({
describe('getBlockProduction', () => {
let rpc: Rpc<GetBlockProductionApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-block-time-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetBlockTimeApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getBlockTime', () => {
let rpc: Rpc<GetBlockTimeApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-cluster-nodes-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { open } from 'node:fs/promises';

import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { GetClusterNodesApi } from '../index';
Expand Down Expand Up @@ -64,8 +63,6 @@ async function getNodeInfoFromLogFile() {
describe('getClusterNodes', () => {
let mockRpc: Rpc<GetClusterNodesApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
mockRpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-epoch-info-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetEpochInfoApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getEpochInfo', () => {
let rpc: Rpc<GetEpochInfoApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-epoch-schedule-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';

import { GetEpochScheduleApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getEpochSchedule', () => {
let rpc: Rpc<GetEpochScheduleApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-fee-for-message-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Blockhash, Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import type { SerializedMessageBytesBase64 } from '@solana/transactions';
import fetchMock from 'jest-fetch-mock-fork';

import { GetFeeForMessageApi, GetLatestBlockhashApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';
Expand Down Expand Up @@ -59,8 +58,6 @@ function getMockTransactionMessage(blockhash: Blockhash) {
describe('getFeeForMessage', () => {
let rpc: Rpc<GetFeeForMessageApi & GetLatestBlockhashApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';

import { GetFirstAvailableBlockApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getFirstAvailableBlock', () => {
let rpc: Rpc<GetFirstAvailableBlockApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});
describe('when called with no parameters', () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-genesis-hash-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { open } from 'node:fs/promises';

import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { GetGenesisHashApi } from '../index';
Expand All @@ -28,8 +27,6 @@ async function getGenesisHashFromLogFile() {
describe('getGenesisHash', () => {
let rpc: Rpc<GetGenesisHashApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
38 changes: 20 additions & 18 deletions packages/rpc-api/src/__tests__/get-health-test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import type { Rpc } from '@solana/rpc-spec';
import { createRpc, type Rpc } from '@solana/rpc-spec';
import type { SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetHealthApi } from '../index';
import { createSolanaRpcApi, GetHealthApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getHealth', () => {
let rpc: Rpc<GetHealthApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

describe('when the node is healthy', () => {
let rpc: Rpc<GetHealthApi>;
beforeEach(() => {
rpc = createLocalhostSolanaRpc();
});
it('returns "ok"', async () => {
expect.assertions(1);
const healthPromise = rpc.getHealth().send();
Expand All @@ -22,16 +18,22 @@ describe('getHealth', () => {
});

describe('when the node is unhealthy', () => {
let rpc: Rpc<GetHealthApi>;
const errorMessage = 'Node is unhealthy';
const errorCode = -32005;
const errorObject = {
code: errorCode,
message: errorMessage,
name: 'RpcError',
};
beforeEach(() => {
rpc = createRpc({
api: createSolanaRpcApi(),
transport: jest.fn().mockRejectedValue(errorObject),
});
});
it('returns an error message', async () => {
expect.assertions(1);
const errorMessage = 'Node is unhealthy';
const errorCode = -32005;
const errorObject = {
code: errorCode,
message: errorMessage,
name: 'RpcError',
};
fetchMock.mockRejectOnce(errorObject);
const healthPromise = rpc.getHealth().send();
await expect(healthPromise).rejects.toMatchObject({
code: errorCode satisfies (typeof SolanaRpcErrorCode)['JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY'],
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-identity-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { open } from 'node:fs/promises';
import type { Address } from '@solana/addresses';
import { getBase58Decoder } from '@solana/codecs-strings';
import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { GetIdentityApi } from '../index';
Expand Down Expand Up @@ -33,8 +32,6 @@ async function getValidatorAddress() {
describe('getIdentity', () => {
let rpc: Rpc<GetIdentityApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-inflation-governor-test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { Rpc } from '@solana/rpc-spec';
import type { Commitment } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetInflationGovernorApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getInflationGovernor', () => {
let rpc: Rpc<GetInflationGovernorApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});
// TODO: I honestly have no clue how to test this
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-inflation-rate-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Rpc } from '@solana/rpc-spec';
import fetchMock from 'jest-fetch-mock-fork';

import { GetInflationRateApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getInflationRate', () => {
let rpc: Rpc<GetInflationRateApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});
// TODO: I honestly have no clue how to test this
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-inflation-reward-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetInflationRewardApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';

describe('getInflationReward', () => {
let rpc: Rpc<GetInflationRewardApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});
[{ minContextSlot: 0n }, null].forEach(minContextConfig => {
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-largest-accounts-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Address } from '@solana/addresses';
import { getBase58Decoder } from '@solana/codecs-strings';
import type { Rpc } from '@solana/rpc-spec';
import type { Commitment } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { GetLargestAccountsApi } from '../index';
Expand Down Expand Up @@ -40,8 +39,6 @@ async function getNodeAddress(path: string) {
describe('getLargestAccounts', () => {
let rpc: Rpc<GetLargestAccountsApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-latest-blockhash-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Rpc } from '@solana/rpc-spec';
import { RpcError } from '@solana/rpc-spec-types';
import type { Commitment, SolanaRpcErrorCode } from '@solana/rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import { GetLatestBlockhashApi } from '../index';
import { createLocalhostSolanaRpc } from './__setup__';
Expand All @@ -13,8 +12,6 @@ const CONTEXT_MATCHER = expect.objectContaining({
describe('getLatestBlockhash', () => {
let rpc: Rpc<GetLatestBlockhashApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-api/src/__tests__/get-leader-schedule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Rpc } from '@solana/rpc-spec';
import type { Commitment } from '@solana/rpc-types';
import assert from 'assert';
import { open } from 'fs/promises';
import fetchMock from 'jest-fetch-mock-fork';
import path from 'path';

import { GetLeaderScheduleApi } from '../index';
Expand Down Expand Up @@ -34,8 +33,6 @@ async function getValidatorAddress() {
describe('getLeaderSchedule', () => {
let rpc: Rpc<GetLeaderScheduleApi>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createLocalhostSolanaRpc();
});

Expand Down
Loading

0 comments on commit ac2c1c8

Please sign in to comment.