Sunday, 16 July 2023

Convert existing mocha tests to livedoc mocha

I've been able to integrate livedoc-mocha into an existing repo from the steps here: Add livedoc-mocha to existing project

Now the issue I am having is converting the tests to use the livedoc-mocha format. For example, the deposit-pool-tests.js file looks like this:

export default function() {
contract('RocketDepositPool', async (accounts) => {

    // 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);
    });

    //
    // Deposit
    //

    it(printTitle('staker', 'can make a deposit'), 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,
        });
    });

My attempt of doing this with livedoc-mocha looks like this:

feature(`Deposit Pool Tests`, () => {
background(`Background steps`, () => {
    given(`Account setup`, async () => {
        contract('RocketDepositPool', async (accounts) => {
                // Accounts
                const [
                    owner,
                    node,
                    trustedNode,
                    staker,
                    random,
                ] = accounts;

            // Register node
            await registerNode({from: node});

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

    scenario("Staker Can Make a Deposit", () => {
        given("Staker deposits 10 Ether", async () => {
            // Deposit
            await deposit({
                from: staker,
                value: '10'.ether,
            });
        });
    });
});

This generates the following results:

Feature: Deposit Pool Tests Background: Background steps √ Given Account setup (624ms) Scenario: Staker Can Make a Deposit

  1. Given Staker deposits 10 Ether

    1 passing (644ms) 1 failing

    1. Feature: Deposit Pool Tests Scenario: Staker Can Make a Deposit Given Staker deposits 10 Ether: ReferenceError: staker is not defined

I can't figure out how to wrap the whole feature file in the contract and run the setup steps only once. Background is supposed to run once for each scenario which isn't exactly the behavior I want.



from Convert existing mocha tests to livedoc mocha

No comments:

Post a Comment