From d831ff1374b1069fbd63d94718ca87c9a15882b5 Mon Sep 17 00:00:00 2001 From: John Rees Date: Sat, 9 Oct 2021 18:34:40 +0100 Subject: [PATCH] test: Minor test performance boost + before() hook (#5) --- tests/assert-balances.ts | 81 ++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/tests/assert-balances.ts b/tests/assert-balances.ts index 9171fcb..cfae1ef 100644 --- a/tests/assert-balances.ts +++ b/tests/assert-balances.ts @@ -12,58 +12,57 @@ import BN from "bn.js"; describe("assert-balances", () => { anchor.setProvider(anchor.Provider.env()); const program = anchor.workspace.AssertBalances; - const mints: Array = []; + let mints: Array; let ctx; - it("BOILERPLATE: Setup token accounts", async () => { - // Iniitialize each mint and token for the test suite. - for (let k = 0; k < 5; k += 1) { - // Create mint. - const client = await Token.createMint( - program.provider.connection, - program.provider.wallet.payer, - program.provider.wallet.publicKey, - null, - 6, - TOKEN_PROGRAM_ID - ); + before(async () => { + // Initialize each mint and token for the test suite. + mints = await Promise.all( + [...Array(5)].map(async () => { + const client = await Token.createMint( + program.provider.connection, + program.provider.wallet.payer, + program.provider.wallet.publicKey, + null, + 6, + TOKEN_PROGRAM_ID + ); - // Create token account. - const tokenAddress = await client.createAccount( - program.provider.wallet.publicKey - ); + // Create token account. + const tokenAddress = await client.createAccount( + program.provider.wallet.publicKey + ); - // Mint to the account. - await client.mintTo( - tokenAddress, - program.provider.wallet.payer, - [], - 1000000 - ); + // Mint to the account. + await client.mintTo( + tokenAddress, + program.provider.wallet.payer, + [], + 1000000 + ); - // Get the account data. - const tokenAccount = await client.getAccountInfo(tokenAddress); + // Get the account data. + const tokenAccount = await client.getAccountInfo(tokenAddress); - // Save the mints. - mints.push({ - client, - tokenAddress, - tokenAccount, - }); - } + // Save the mints. + return { + client, + tokenAddress, + tokenAccount, + }; + }) + ); // Shared context for subsequent instructions. ctx = { accounts: { user: program.provider.wallet.publicKey, }, - remainingAccounts: mints.map((m) => { - return { - pubkey: m.tokenAddress, - isWritable: false, - isSigner: false, - }; - }), + remainingAccounts: mints.map((m) => ({ + pubkey: m.tokenAddress, + isWritable: false, + isSigner: false, + })), }; }); @@ -116,7 +115,7 @@ const assertRejectsWithCode = (expectedCode: number) => async (rpcInvocation) => async () => { await rpcInvocation(); }, - (err: { code: number }) => { + (err: { code: number; msg: string }) => { assert.equal(err.code, expectedCode); return true; }