testHelpers.ts.md•3.39 kB
/**
* @file Test Helper Utilities
* @version 1.0.0
* @status STABLE - COMPLETE TEST COVERAGE
* @lastModified 2024-06-07
*
* Helper utilities for testing Ethereum functionality
*
* IMPORTANT:
* - Keep tests consistent
* - Maintain isolation between tests
*
* Functionality:
* - Mock data generation
* - Test environment setup
* - Test assertions
*/
import { ethers } from 'ethers';
import { EthersService } from '../../services/ethersService.js';
// Test networks for testing
export const TEST_NETWORKS = {
hardhat: {
name: 'hardhat',
chainId: 31337,
provider: new ethers.JsonRpcProvider('http://localhost:8545'),
},
mainnetFork: {
name: 'mainnet-fork',
chainId: 1,
provider: new ethers.JsonRpcProvider('http://localhost:8545'),
}
};
// Test accounts from Hardhat
export const TEST_ACCOUNTS = {
// Hardhat default account #0, has 10000 ETH
deployer: {
address: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
privateKey: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
},
// Hardhat default account #1
user: {
address: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
privateKey: '0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d',
}
};
// Test tokens for various scenarios
export const TEST_TOKENS = {
// Mock ERC20 token information
mockERC20: {
name: 'Mock Token',
symbol: 'MOCK',
decimals: 18,
totalSupply: ethers.parseEther('1000000'),
}
};
// Helper to create a test service instance
export function createTestEthersService(): EthersService {
const provider = TEST_NETWORKS.hardhat.provider;
const signer = new ethers.Wallet(TEST_ACCOUNTS.user.privateKey, provider);
return new EthersService(provider, signer);
}
// Helper to create a random wallet for testing
export function createRandomWallet(): ethers.Wallet {
return ethers.Wallet.createRandom();
}
// Helper to mock a transaction for testing
export function createMockTransaction(overrides: Partial<ethers.TransactionRequest> = {}): ethers.TransactionRequest {
return {
to: TEST_ACCOUNTS.user.address,
value: ethers.parseEther('1.0'),
gasLimit: 21000,
...overrides,
};
}
// Helper to wait for a transaction to be mined
export async function waitForTransaction(txHash: string, provider = TEST_NETWORKS.hardhat.provider): Promise<ethers.TransactionReceipt | null> {
return provider.waitForTransaction(txHash);
}
// Helper to deploy a test contract
export async function deployTestContract(
signer: ethers.Signer,
abi: any[],
bytecode: string,
args: any[] = []
): Promise<ethers.Contract> {
const factory = new ethers.ContractFactory(abi, bytecode, signer);
const contract = await factory.deploy(...args);
await contract.deploymentTransaction()?.wait();
return contract;
}
// Helper to assert that a transaction failed with a specific error
export async function expectTransactionToFail(
txPromise: Promise<any>,
errorMessage?: string | RegExp
): Promise<void> {
try {
await txPromise;
throw new Error('Transaction did not fail as expected');
} catch (error) {
if (errorMessage) {
if (error instanceof Error) {
if (typeof errorMessage === 'string') {
expect(error.message).toContain(errorMessage);
} else {
expect(error.message).toMatch(errorMessage);
}
}
}
}
}