I am puzzled by the error I encounter when running 'npm run test' in the terminal. The error seems to originate from the file called lottery.test.js. Despite double-checking the syntax and ensuring that I exported the modules correctly from the compile file, I still face an issue in the test file. For those curious, the name of the solidity file is Lottery.sol.
Lottery.test.js:
// Modules
const assert = require('assert'); // used for ganache assertion
const ganache = require('ganache-cli'); // local ethereum testing network
const Web3 = require('web3'); // Web3 is a constructor function (that's why it is capitalized)
// creating an instance of Web3
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require('../compile'); // destructors - 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 - Contract has been deployed
it('deploys a contract', () => {
assert.ok(lottery.options.address);
});
// Test One - Allows one account to 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 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 entry 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 trigger an error
await lottery.methods.enter().send({
from: accounts[0],
value: 0
});
assert(false); // forcing the function to generate an error
}
catch(err){ // triggered when an error occurs
assert(err);
}
});
// Test Four - Only the manager can pick the winner
// By allowing someone else BESIDES 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. Initial balance should 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 amounts are reset
await lottery.methods.pickWinner().send({from: accounts[0]});
// 4. Final balance should be more by 2 Ether (normal before entering)
const finalBalance = await web3.eth.getBalance(accounts[0]);
// 5. Difference should be less than 2 Ether - it's 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 be compatible 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']; // compilation statement