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", () => {
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.AssertBalances;
const mints: Array<MintAccounts> = [];
let mints: Array<MintAccounts>;
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;
}