snow monster
snow monster

Reputation: 91

Terminal Error: TypeError: Cannot destructure property 'interface' of 'require(...)' as it is undefined

I have no clue to why I get this error when I type 'npm run test' in terminal. It looks like it generated from the file lottery.test.js. I double checked syntax and it seems like I exported the modules from the compile file correctly. However, I still get an error in the test file.The name of the solidity file is Lottery.sol for anyone who wants to know.

Lottery.test.js:

// Modules 
const assert = require('assert'); // used for ganache assertion
const ganache = require('ganache-cli'); // local ethereum testing netwrok 
const Web3 = require('web3'); // Web3 is a constructor function (that's why it is capatalized)

 // creating an instance of Web3
 const provider = ganache.provider();
 const web3 = new Web3(provider);
 
const { interface, bytecode } = require('../compile'); // descructors - going up the directory tree 



let lottery; 
let accounts; 

beforeEach( async () => {
    accounts = await web3.eth.getAccounts();

    lottery = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode })
    .send({ from: accounts[0], gas: '1000000' });

});

describe('Lottery Contract', () => {

    // General Test - Contact has been deployed
    it('deployes a contract', ()=> { 
        assert.ok(lottery.options.address);
    });

    // Test One - Allows one account to be enter
    it('allows one account to enter', async () => {
        await lottery.methods.enter().send({
            from: accounts[0],
            value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
        });

        const players = await lottery.methods.getPlayers().call({from: accounts[0]});

        assert.equal(accounts[0], players[0]);
        assert.equal(1, players.length); 
    }); 

    // Test Two - Allows more than one account to be enter
    it('allows multiple accounts to enter', async () => {

        await lottery.methods.enter().send({
            from: accounts[0],
            value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
        });

        await lottery.methods.enter().send({
            from: accounts[1],
            value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
        });

        await lottery.methods.enter().send({
            from: accounts[2],
            value: web3.utils.toWei('0.02', 'ether') // converts value from ether to wei since value is measured in wei
        });

        const players = await lottery.methods.getPlayers().call({from: accounts[0]});

        assert.equal(accounts[0], players[0]);
        assert.equal(accounts[1], players[1]);
        assert.equal(accounts[2], players[2]);

        assert.equal(1, players.length); 
    }); 

    // Test Three - Only accounts that send more than 1 Ether can enter
    // Done by sending an entery fee LESS than what is expected and anticipating the error 
    // Using a Try-Catch structure 
    it('requires a minimum amount of ether to enter', async () => {

        try {  // trying to push an error to happen 
        await lottery.methods.enter().send({
            from: accounts[0],
            value: 0
        }); 
        assert(false); // pushing the function to generate an error
    }
        catch(err){ // triggered when an error is triggered
            
         assert(err);

        }
    });

    // Test Four - Only the manager can pick the winner 
    // By letting someone else BESIDE the manager choose the winner causing an error 
    it ('Only manager can call pickWinner', async () => {

        try {
            await lottery.methods.pickWinner().send({
                from: accounts[1] // someone else - not the manager
            });
            assert(false); 

        } catch(err){
            assert(err); 
        }

    });
 
    // Test Five - Resets the lottery when done 
    it('Sends money to the winner and resets the players array', async () => {
        // 1. Entering the lottery means the player sends an amount of Ether (2 Ether here)
        await lottery.methods.enter().send({
            from: accounts[0],
            value: web3.untils.toWei('2', 'ether')
        });

        // 2. Inital balance has to be less by 2 Ether
        const initialBalance = await web3.eth.getBalance(accounts[0]); // amount of Ether in units of wei in an account

        // 3. After picking the winner, the amountd are reset
        await lottery.methods.pickWinner().send({from: accounts[0]});

        // 4. Final balance should be more by 2 Ehter (normal before entering)
        const finalBalance = await web3.eth.getBalance(accounts[0]);

        // 5. Difference has to be less than 2 Ether - its not exactly 2 because we have to pay for gas 

        const difference = finalBalance - initialBalance; 

        assert(difference > web3.untils.toWei('1.8', 'ether'));

    });


});

compile.js:

// Modules 
const path = require ('path'); // module used to help build a path from compile.js to inbox.sol - guaranteed to get compatibility with OS used  
const fs = require ('fs');
const solc = require ('solc'); 

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8'); // to read the content of the inbox.sol file
module.exports = solc.compile(source, 1).contracts[':Lottery']; // compile statement 

Upvotes: 2

Views: 1023

Answers (1)

Arthur Dias
Arthur Dias

Reputation: 21

As i can't comment ngambinos post. This also worked for me here in 2021. (thx to) ngambino0192:

The issue is with this is likely the versioning of the solc compiler. Bumping the version's dependency to one that is more recent may solve your issue.

Try the following:

  1. rm -rf node_modules // remove node_modules
  2. rm -rf package-lock.json // remove package-lock.json
  3. Change the .sol file's import statement to: pragma solidity ^0.4.25
  4. Change the .sol file's import statement to: pragma solidity ^0.4.25
  5. Change the dependency in package.json to "solc": "^0.4.25"
  6. npm install // install new dependencies

Then you can run your tests

Upvotes: 2

Related Questions