test: Minor test performance boost + before() hook (#5)

This commit is contained in:
John Rees 2021-10-09 18:34:40 +01:00 committed by GitHub
parent b2875811a4
commit d831ff1374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 41 deletions

View File

@ -12,58 +12,57 @@ import BN from "bn.js";
describe("assert-balances", () => { describe("assert-balances", () => {
anchor.setProvider(anchor.Provider.env()); anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.AssertBalances; const program = anchor.workspace.AssertBalances;
const mints: Array<MintAccounts> = []; let mints: Array<MintAccounts>;
let ctx; let ctx;
it("BOILERPLATE: Setup token accounts", async () => { before(async () => {
// Iniitialize each mint and token for the test suite. // Initialize each mint and token for the test suite.
for (let k = 0; k < 5; k += 1) { mints = await Promise.all(
// Create mint. [...Array(5)].map(async () => {
const client = await Token.createMint( const client = await Token.createMint(
program.provider.connection, program.provider.connection,
program.provider.wallet.payer, program.provider.wallet.payer,
program.provider.wallet.publicKey, program.provider.wallet.publicKey,
null, null,
6, 6,
TOKEN_PROGRAM_ID TOKEN_PROGRAM_ID
); );
// Create token account. // Create token account.
const tokenAddress = await client.createAccount( const tokenAddress = await client.createAccount(
program.provider.wallet.publicKey program.provider.wallet.publicKey
); );
// Mint to the account. // Mint to the account.
await client.mintTo( await client.mintTo(
tokenAddress, tokenAddress,
program.provider.wallet.payer, program.provider.wallet.payer,
[], [],
1000000 1000000
); );
// Get the account data. // Get the account data.
const tokenAccount = await client.getAccountInfo(tokenAddress); const tokenAccount = await client.getAccountInfo(tokenAddress);
// Save the mints. // Save the mints.
mints.push({ return {
client, client,
tokenAddress, tokenAddress,
tokenAccount, tokenAccount,
}); };
} })
);
// Shared context for subsequent instructions. // Shared context for subsequent instructions.
ctx = { ctx = {
accounts: { accounts: {
user: program.provider.wallet.publicKey, user: program.provider.wallet.publicKey,
}, },
remainingAccounts: mints.map((m) => { remainingAccounts: mints.map((m) => ({
return { pubkey: m.tokenAddress,
pubkey: m.tokenAddress, isWritable: false,
isWritable: false, isSigner: false,
isSigner: false, })),
};
}),
}; };
}); });
@ -116,7 +115,7 @@ const assertRejectsWithCode = (expectedCode: number) => async (rpcInvocation) =>
async () => { async () => {
await rpcInvocation(); await rpcInvocation();
}, },
(err: { code: number }) => { (err: { code: number; msg: string }) => {
assert.equal(err.code, expectedCode); assert.equal(err.code, expectedCode);
return true; return true;
} }