chore: migrate tests to typescript
This commit is contained in:
committed by
Justin Starry
parent
f912c63b22
commit
8ada44456d
@ -1,20 +1,21 @@
|
||||
import bs58 from 'bs58';
|
||||
import BN from 'bn.js';
|
||||
import invariant from 'assert';
|
||||
import * as mockttp from 'mockttp';
|
||||
|
||||
import {mockRpcMessage} from './rpc-websockets';
|
||||
import {Account, Connection, PublicKey, Transaction} from '../../src';
|
||||
import type {Commitment} from '../../src/connection';
|
||||
|
||||
export const mockServer: mockttp.Mockttp =
|
||||
process.env.TEST_LIVE || mockttp.getLocal();
|
||||
export const mockServer: mockttp.Mockttp | undefined =
|
||||
process.env.TEST_LIVE === undefined ? mockttp.getLocal() : undefined;
|
||||
|
||||
let uniqueCounter = 0;
|
||||
export const uniqueSignature = () => {
|
||||
return bs58.encode(new BN(++uniqueCounter).toArray(null, 64));
|
||||
return bs58.encode(new BN(++uniqueCounter).toArray(undefined, 64));
|
||||
};
|
||||
export const uniqueBlockhash = () => {
|
||||
return bs58.encode(new BN(++uniqueCounter).toArray(null, 32));
|
||||
return bs58.encode(new BN(++uniqueCounter).toArray(undefined, 32));
|
||||
};
|
||||
|
||||
export const mockErrorMessage = 'Invalid';
|
||||
@ -30,13 +31,13 @@ export const mockRpcResponse = async ({
|
||||
error,
|
||||
withContext,
|
||||
}: {
|
||||
method: string,
|
||||
params: Array<any>,
|
||||
value?: any,
|
||||
error?: any,
|
||||
withContext?: boolean,
|
||||
method: string;
|
||||
params: Array<any>;
|
||||
value?: any;
|
||||
error?: any;
|
||||
withContext?: boolean;
|
||||
}) => {
|
||||
if (process.env.TEST_LIVE) return;
|
||||
if (!mockServer) return;
|
||||
|
||||
let result = value;
|
||||
if (withContext) {
|
||||
@ -70,8 +71,8 @@ const recentBlockhash = async ({
|
||||
connection,
|
||||
commitment,
|
||||
}: {
|
||||
connection: Connection,
|
||||
commitment?: Commitment,
|
||||
connection: Connection;
|
||||
commitment?: Commitment;
|
||||
}) => {
|
||||
const blockhash = uniqueBlockhash();
|
||||
const params = [];
|
||||
@ -101,17 +102,18 @@ const processTransaction = async ({
|
||||
commitment,
|
||||
err,
|
||||
}: {
|
||||
connection: Connection,
|
||||
transaction: Transaction,
|
||||
signers: Array<Account>,
|
||||
commitment: Commitment,
|
||||
err?: any,
|
||||
connection: Connection;
|
||||
transaction: Transaction;
|
||||
signers: Array<Account>;
|
||||
commitment: Commitment;
|
||||
err?: any;
|
||||
}) => {
|
||||
const blockhash = (await recentBlockhash({connection})).blockhash;
|
||||
transaction.recentBlockhash = blockhash;
|
||||
transaction.sign(...signers);
|
||||
|
||||
const encoded = transaction.serialize().toString('base64');
|
||||
invariant(transaction.signature !== null);
|
||||
const signature = bs58.encode(transaction.signature);
|
||||
await mockRpcResponse({
|
||||
method: 'sendTransaction',
|
||||
@ -146,9 +148,9 @@ const airdrop = async ({
|
||||
address,
|
||||
amount,
|
||||
}: {
|
||||
connection: Connection,
|
||||
address: PublicKey,
|
||||
amount: number,
|
||||
connection: Connection;
|
||||
address: PublicKey;
|
||||
amount: number;
|
||||
}) => {
|
||||
await mockRpcResponse({
|
||||
method: 'requestAirdrop',
|
@ -5,15 +5,15 @@ import sinon from 'sinon';
|
||||
import {Connection} from '../../src';
|
||||
|
||||
type RpcRequest = {
|
||||
method: string,
|
||||
params?: Array<any>,
|
||||
method: string;
|
||||
params?: Array<any>;
|
||||
};
|
||||
|
||||
type RpcResponse = {
|
||||
context: {
|
||||
slot: number,
|
||||
},
|
||||
value: any,
|
||||
slot: number;
|
||||
};
|
||||
value: any;
|
||||
};
|
||||
|
||||
const mockRpcSocket: Array<[RpcRequest, RpcResponse]> = [];
|
||||
@ -24,9 +24,9 @@ export const mockRpcMessage = ({
|
||||
params,
|
||||
result,
|
||||
}: {
|
||||
method: string,
|
||||
params: Array<any>,
|
||||
result: any,
|
||||
method: string;
|
||||
params: Array<any>;
|
||||
result: any;
|
||||
}) => {
|
||||
mockRpcSocket.push([
|
||||
{method, params},
|
||||
@ -46,9 +46,11 @@ export const stubRpcWebSocket = (connection: Connection) => {
|
||||
sandbox.stub(rpcWebSocket, 'close').callsFake(() => {
|
||||
mockClient.close();
|
||||
});
|
||||
sandbox.stub(rpcWebSocket, 'call').callsFake((method, params) => {
|
||||
return mockClient.call(method, params);
|
||||
});
|
||||
sandbox
|
||||
.stub(rpcWebSocket, 'call')
|
||||
.callsFake((method: string, params: any) => {
|
||||
return mockClient.call(method, params);
|
||||
});
|
||||
};
|
||||
|
||||
export const restoreRpcWebSocket = (connection: Connection) => {
|
||||
@ -85,7 +87,10 @@ class MockClient {
|
||||
|
||||
call(method: string, params: Array<any>): Promise<Object> {
|
||||
expect(mockRpcSocket.length).to.be.at.least(1);
|
||||
const [mockRequest, mockResponse] = mockRpcSocket.shift();
|
||||
const [mockRequest, mockResponse] = mockRpcSocket.shift() as [
|
||||
RpcRequest,
|
||||
RpcResponse,
|
||||
];
|
||||
|
||||
expect(method).to.eq(mockRequest.method);
|
||||
expect(params).to.eql(mockRequest.params);
|
Reference in New Issue
Block a user