Tuesday, 25 July 2023

Break up async mocha test into multiple tests

To provide better test reporting, I am trying to break up some tests which contain a lot of steps to smaller steps. https://github.com/rocket-pool/rocketpool/blob/master/test/deposit/deposit-pool-tests.js

For example,

it(printTitle('staker', 'deposits 10 eth'), async () => {
        // Deposit
        await deposit({
            from: staker,
            value: '10'.ether,
        });

        // Get current rETH exchange rate
        let exchangeRate1 = await getRethExchangeRate();

        // Update network ETH total to 130% to alter rETH exchange rate
        let totalBalance = '13'.ether;
        let rethSupply = await getRethTotalSupply();
        await submitBalances(1, totalBalance, 0, rethSupply, {from: trustedNode});

        // Get & check updated rETH exchange rate
        let exchangeRate2 = await getRethExchangeRate();
        assertBN.notEqual(exchangeRate1, exchangeRate2, 'rETH exchange rate has not changed');

        // Deposit again with updated rETH exchange rate
        await deposit({
            from: staker,
            value: '10'.ether,
        });
    });

I would like to break this up into 5 it steps.

1. The staker deposits 10 ETH
2. The rETH exchange rate is retreived
3. The network ETH total is updated to 130% to alter rETH exchange rate
4. The rETH exchange rate is checked
5. The staker can deposit again with the updated rETH exchange rate

The problem is when I do this, the results of step 3 do not make it to step 4, and thus it fails. How can I properly use promises to accomplish this? Here is what I am trying using mocha-cakes, getting an error that exchangeRate1 is equal to exchangeRate2

 Scenario('Staker can make a deposit before and after updating the rETH exchange rate',  () => { 
          let exchangeRate1;

            // Accounts
            const [
                owner,
                node,
                trustedNode,
                staker,
                random,
            ] = accounts;

                // Setup
            before(async () => {
                // Register node
                await registerNode({from: node});

                // Register trusted node
                await registerNode({from: trustedNode});
                await setNodeTrusted(trustedNode, 'saas_1', 'node@home.com', owner);
            });

            when('the staker deposits 10 ETH', async () => {
                await deposit({
                    from: staker,
                    value: '10'.ether,
                });
            });
    
            then('the staker should have successfully deposited 10 ETH', () => {
                // Add any additional checks you might need here
            });
    
            when('the rETH exchange rate is obtained', async () => {
                exchangeRate1 = await getRethExchangeRate();
            });
    
            then('the initial rETH exchange rate should be obtained', () => {
                // Add any additional checks you might need here
            });
    
            when('the network ETH total is updated to alter the rETH exchange rate', async () => {
                let totalBalance = '13'.ether;
                let rethSupply = await getRethTotalSupply();
                await submitBalances(1, totalBalance, 0, rethSupply, { from: trustedNode });
    
                // Delay to allow time for the update to take effect, if necessary
                await new Promise((resolve) => setTimeout(resolve, 1000));
            });
    
            then('the rETH exchange rate should be updated', async () => {
                let exchangeRate2 = await getRethExchangeRate();
                assertBN.notEqual(exchangeRate1, exchangeRate2, 'rETH exchange rate has not changed');
            });
    
            when('the staker deposits 10 ETH again with the updated rETH exchange rate', async () => {
                await deposit({
                    from: staker,
                    value: '10'.ether,
                });
            });
    
            then('the staker should have successfully deposited 10 ETH again', () => {
                // Add any additional checks you might need here
            });
          });
    });


from Break up async mocha test into multiple tests

No comments:

Post a Comment