Skip to content

Commit

Permalink
Fix: web3.js test typescript errors
Browse files Browse the repository at this point in the history
Fixes the typescript errors throughout the
web3.js test suite
  • Loading branch information
TJDawson10 committed Dec 26, 2022
1 parent f2ba16e commit 13a9f75
Show file tree
Hide file tree
Showing 30 changed files with 70 additions and 23 deletions.
1 change: 1 addition & 0 deletions web3.js/test/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import {Buffer} from 'buffer';
import { describe } from 'mocha';

import {Account} from '../src/account';

Expand Down
15 changes: 11 additions & 4 deletions web3.js/test/bpf-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'mz/fs';
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';

import {
Expand All @@ -12,6 +13,7 @@ import {
import {url} from './url';
import {BPF_LOADER_PROGRAM_ID} from '../src/bpf-loader';
import {helpers} from './mocks/rpc-http';
import { fail } from 'assert';

use(chaiAsPromised);

Expand Down Expand Up @@ -160,10 +162,15 @@ if (process.env.TEST_LIVE) {
])
).value;
const expectedReturnData = new Uint8Array([1, 2, 3]);
var decodedData = Buffer.from(returnData.data[0], returnData.data[1]);
expect(err).to.be.null;
expect(returnData.programId).to.eql(program.publicKey.toString());
expect(decodedData).to.eql(expectedReturnData);

if (returnData) {
var decodedData = Buffer.from(returnData.data[0], returnData.data[1]);
expect(err).to.be.null;
expect(returnData.programId).to.eql(program.publicKey.toString());
expect(decodedData).to.eql(expectedReturnData);
} else {
fail("return data must be defined!");
}
});

it('deprecated - simulate transaction without signature verification', async () => {
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/cluster.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {clusterApiUrl} from '../src/utils/cluster';

Expand Down
1 change: 1 addition & 0 deletions web3.js/test/connection-subscriptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chai from 'chai';
import { describe } from 'mocha';
import {Client} from 'rpc-websockets';
import {stub, SinonStubbedInstance, SinonSpy, spy} from 'sinon';
import sinonChai from 'sinon-chai';
Expand Down
40 changes: 24 additions & 16 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import bs58 from 'bs58';
import {Buffer} from 'buffer';
import * as splToken from '@solana/spl-token';
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';
import {Agent as HttpAgent} from 'http';
import {Agent as HttpsAgent} from 'https';
import {AbortController} from 'node-abort-controller';
import {match, mock, spy, useFakeTimers, SinonFakeTimers} from 'sinon';
import sinonChai from 'sinon-chai';

Expand Down Expand Up @@ -75,6 +75,7 @@ import type {
import {VersionedTransaction} from '../src/transaction/versioned';
import {MessageV0} from '../src/message/v0';
import {encodeData} from '../src/instruction';
import { fail } from 'assert';

use(chaiAsPromised);
use(sinonChai);
Expand Down Expand Up @@ -171,10 +172,16 @@ describe('Connection', function () {
it('should allow middleware to augment request', async () => {
let connection = new Connection(url, {
fetchMiddleware: (url, options, fetch) => {
options.headers = Object.assign(options.headers, {
Authorization: 'Bearer 123',
});
fetch(url, options);
if (options) {
options.headers = Object.assign(options.headers!, {
Authorization: 'Bearer 123',
});

fetch(url, options);

} else {
fail("options must be defined!");
}
},
});

Expand Down Expand Up @@ -254,7 +261,7 @@ describe('Connection', function () {
it('should not attribute fetch errors to the middleware', async () => {
let connection = new Connection(url, {
fetchMiddleware: (url, _options, fetch) => {
fetch(url, 'An `Object` was expected here; this is a `TypeError`.');
fetch(url, {'body': 'An `Object` was expected here; this is a `TypeError`.'});
},
});
const error = await expect(connection.getVersion()).to.be.rejected;
Expand Down Expand Up @@ -1334,7 +1341,8 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function(result: SignatureResult): void {};

await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand All @@ -1344,7 +1352,7 @@ describe('Connection', function () {
});

// Simulate a failure to fetch the block height.
let rejectBlockheightPromise: () => void;
let rejectBlockheightPromise = function(): void {};
await mockRpcResponse({
method: 'getBlockHeight',
params: [],
Expand Down Expand Up @@ -1375,7 +1383,7 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function(result: SignatureResult): void {};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand Down Expand Up @@ -1455,7 +1463,7 @@ describe('Connection', function () {
const mockSignature =
'4oCEqwGrMdBeMxpzuWiukCYqSfV4DsSKXSiVVCh1iJ6pS772X7y219JZP3mgqBz5PhsvprpKyhzChjYc3VSBQXzG';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise= function(result: SignatureResult): void {};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand Down Expand Up @@ -1666,7 +1674,7 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function(result: SignatureResult): void {};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand All @@ -1676,7 +1684,7 @@ describe('Connection', function () {
});

// Simulate a failure to fetch the nonce account.
let rejectNonceAccountFetchPromise: () => void;
let rejectNonceAccountFetchPromise = function(): void {};
await mockRpcResponse({
method: 'getAccountInfo',
params: [],
Expand Down Expand Up @@ -1762,7 +1770,7 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function(result: SignatureResult): void {};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand Down Expand Up @@ -5391,9 +5399,9 @@ describe('Connection', function () {
);

await connection.confirmTransaction({
blockhash: transaction.recentBlockhash,
lastValidBlockHeight: transaction.lastValidBlockHeight,
signature,
blockhash: transaction.recentBlockhash!,
lastValidBlockHeight: transaction.lastValidBlockHeight!,
signature: signature,
});

const response = (await connection.getSignatureStatus(signature)).value;
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/epoch-schedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {EpochSchedule} from '../src';

Expand Down
1 change: 1 addition & 0 deletions web3.js/test/keypair.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from 'chai';
import {Buffer} from 'buffer';
import { describe } from 'mocha';

import {Keypair} from '../src';

Expand Down
1 change: 1 addition & 0 deletions web3.js/test/makeWebsocketUrl.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {makeWebsocketUrl} from '../src/utils/makeWebsocketUrl';

Expand Down
1 change: 1 addition & 0 deletions web3.js/test/message-tests/account-keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {
MessageAccountKeys,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/message-tests/compiled-keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {CompiledKeyMeta, CompiledKeys} from '../../src/message/compiled-keys';
import {AddressLookupTableAccount} from '../../src/programs';
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/message-tests/legacy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bs58 from 'bs58';
import {expect} from 'chai';
import { describe } from 'mocha';
import {sha256} from '@noble/hashes/sha256';

import {Message} from '../../src/message';
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/message-tests/v0.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bs58 from 'bs58';
import {expect} from 'chai';
import { describe } from 'mocha';
import {sha256} from '@noble/hashes/sha256';

import {
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/message-tests/versioned.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {VersionedMessage} from '../../src/message';

Expand Down
4 changes: 2 additions & 2 deletions web3.js/test/mocks/rpc-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const latestBlockhash = async ({
commitment?: Commitment;
}) => {
const blockhash = uniqueBlockhash();
const params = [];
const params: Array<Object> = [];
if (commitment) {
params.push({commitment});
}
Expand All @@ -154,7 +154,7 @@ const recentBlockhash = async ({
commitment?: Commitment;
}) => {
const blockhash = uniqueBlockhash();
const params = [];
const params: Array<Object> = [];
if (commitment) {
params.push({commitment});
}
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/nonce.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {expect} from 'chai';
import { describe } from 'mocha';

import {
Connection,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/address-lookup-table.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';

import {
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/compute-budget.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';

import {
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/ed25519.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Buffer} from 'buffer';
import { describe } from 'mocha';

import {
Connection,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/secp256k1.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Buffer} from 'buffer';
import {keccak_256} from '@noble/hashes/sha3';
import { describe } from 'mocha';

import {
ecdsaSign,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/stake.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { describe } from 'mocha';

import {
Keypair,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/system.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Buffer} from 'buffer';
import {expect} from 'chai';
import { describe } from 'mocha';

import {
Keypair,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/program-tests/vote.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';

import {
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/publickey.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BN from 'bn.js';
import {Buffer} from 'buffer';
import {expect, use} from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';

import {Keypair} from '../src/keypair';
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/shortvec-encoding.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import { describe } from 'mocha';

import {decodeLength, encodeLength} from '../src/utils/shortvec-encoding';

Expand Down
1 change: 1 addition & 0 deletions web3.js/test/transaction-payer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base58 from 'bs58';
import {expect} from 'chai';
import { describe } from 'mocha';

import {
Keypair,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/transaction-tests/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bs58 from 'bs58';
import {expect} from 'chai';
import {sha256} from '@noble/hashes/sha256';
import { describe } from 'mocha';

import {
Transaction,
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/transaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {expect} from 'chai';
import { describe } from 'mocha';

import {Connection} from '../src/connection';
import {Keypair} from '../src/keypair';
Expand Down
8 changes: 7 additions & 1 deletion web3.js/test/url.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* The connection url to use when running unit tests against a live cluster
*/

export const MOCK_PORT = 9999;

declare var process: {
env: {
TEST_LIVE: string
}
}

export const url = process.env.TEST_LIVE
? 'http://localhost:8899/'
: 'http://localhost:9999/';
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/validator-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Buffer} from 'buffer';
import {expect} from 'chai';
import { describe } from 'mocha';

import {Keypair} from '../src/keypair';
import {PublicKey} from '../src/publickey';
Expand Down
1 change: 1 addition & 0 deletions web3.js/test/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import bs58 from 'bs58';
import {Buffer} from 'buffer';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { describe } from 'mocha';

import {Connection} from '../src';
import {url, wsUrl} from './url';
Expand Down

0 comments on commit 13a9f75

Please sign in to comment.