Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: web3.js test typescript errors #29422

Merged
merged 5 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,6 +1,8 @@
import fs from 'mz/fs';
import {expect, use} from 'chai';
import {describe} from 'mocha';
import chaiAsPromised from 'chai-as-promised';
import {fail} from 'assert';

import {
Connection,
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
51 changes: 35 additions & 16 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ 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';
import {fail} from 'assert';

import {
Authorized,
Expand Down Expand Up @@ -171,10 +172,15 @@ 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 +260,9 @@ 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 +1342,10 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

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

await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand All @@ -1344,7 +1355,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 +1386,9 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

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

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

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function (result: SignatureResult): any {
return result;
};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand All @@ -1676,7 +1693,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 +1779,9 @@ describe('Connection', function () {
const mockSignature =
'LPJ18iiyfz3G1LpNNbcBnBtaS4dVBdPHKrnELqikjER2DcvB4iyTgz43nKQJH3JQAJHuZdM1xVh5Cnc5Hc7LrqC';

let resolveResultPromise: (result: SignatureResult) => void;
let resolveResultPromise = function (result: SignatureResult): any {
return result;
};
await mockRpcMessage({
method: 'signatureSubscribe',
params: [mockSignature, {commitment: 'finalized'}],
Expand Down Expand Up @@ -5391,9 +5410,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