0

While testing the solidity contract, I'm getting the error

`const { interface, bytecode } = require('../compile');
        ^
TypeError: Cannot destructure property 'interface' of 'require(...)' as it is undefined.
    at Object.<anonymous> (C:\Users\91960\lottery\test\Lottery.test.js:6:9)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at C:\Users\91960\lottery\node_modules\mocha\lib\mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (C:\Users\91960\lottery\node_modules\mocha\lib\mocha.js:331:14)
    at Mocha.run (C:\Users\91960\lottery\node_modules\mocha\lib\mocha.js:809:10)
    at Object.exports.singleRun (C:\Users\91960\lottery\node_modules\mocha\lib\cli\run-helpers.js:108:16)
    at exports.runMocha (C:\Users\91960\lottery\node_modules\mocha\lib\cli\run-helpers.js:142:13)
    at Object.exports.handler (C:\Users\91960\lottery\node_modules\mocha\lib\cli\run.js:292:3)
    at Object.runCommand (C:\Users\91960\lottery\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
    at Object.parseArgs [as _parseArgs] (C:\Users\91960\lottery\node_modules\mocha\node_modules\yargs\yargs.js:1113:24)
    at Object.parse (C:\Users\91960\lottery\node_modules\mocha\node_modules\yargs\yargs.js:575:25)
    at Object.exports.main (C:\Users\91960\lottery\node_modules\mocha\lib\cli\cli.js:68:6)
    at Object.<anonymous> (C:\Users\91960\lottery\node_modules\mocha\bin\mocha:162:29)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47 `

Here is the compile.js file:

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8');

module.exports = solc.compile(source, 1).contracts[':Lottery'];

Here is the Lottery.test.js file:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const { interface, bytecode } = require('../compile');

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', () => {
  it('deploys a contract', () => {
    assert.ok(lottery.options.address);
  });

  it('allows one account to enter', async () => {
    await lottery.methods.enter().send({
      from: accounts[0],
      value: web3.utils.toWei('0.02', 'ether')
    });

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

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

  it('allows multiple accounts to enter', async () => {
    await lottery.methods.enter().send({
      from: accounts[0],
      value: web3.utils.toWei('0.02', 'ether')
    });
    await lottery.methods.enter().send({
      from: accounts[1],
      value: web3.utils.toWei('0.02', 'ether')
    });
    await lottery.methods.enter().send({
      from: accounts[2],
      value: web3.utils.toWei('0.02', 'ether')
    });

    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(3, players.length);
  });

  it('requires a minimum amount of ether to enter', async () => {
    try {
      await lottery.methods.enter().send({
        from: accounts[0],
        value: 0
      });
      assert(false);
    } catch (err) {
      assert(err);
    }
  });

  it('only manager can call pickWinner', async () => {
    try {
      await lottery.methods.pickWinner().send({
        from: accounts[1]
      });
      assert(false);
    } catch (err) {
      assert(err);
    }
  });

  it('sends money to the winner and resets the players array', async () => {
    await lottery.methods.enter().send({
      from: accounts[0],
      value: web3.utils.toWei('2', 'ether')
    });

    const initialBalance = await web3.eth.getBalance(accounts[0]);
    await lottery.methods.pickWinner().send({ from: accounts[0] });
    const finalBalance = await web3.eth.getBalance(accounts[0]);
    const difference = finalBalance - initialBalance;

    assert(difference > web3.utils.toWei('1.8', 'ether'));
  });
});
3
  • Could you show your solc / solcjs versions ? Your example doesn't follow the current API : npmjs.com/package/solc
    – hroussille
    Nov 22, 2021 at 8:30
  • My current solidity compiler is 0.8.10
    – Anay
    Nov 22, 2021 at 11:22
  • @AnayDongre The compile.js file is too old, it should look like the one in this question ethereum.stackexchange.com/q/114000 for solc 0.8.
    – Ismael
    Nov 23, 2021 at 21:08

2 Answers 2

0

check the solidity version in the contract file and package-json file both versions should be the same, if not then download the specific version by the following command.

npm install solc@your_version_of_solidity
0

You should give solc.compile a JSON input as in the github documentation for solcjs v0.8.10.

Also solc.version() will give you your version of solcjs in the node runtime to make sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.