1

I am trying to test my contract in Mocha. and I am running into this issue where npm run test is giving the following result.

PiggyBank
    1) "before each" hook for "is deployed"
 
 
  0 passing (247ms)
  1 failing
 
  1) "before each" hook for "is deployed":
     SyntaxError: Unexpected token u in JSON at position 0
      at JSON.parse (<anonymous>)
      at Context.<anonymous> (test\PiggyBank.test.js:15:50)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

I tried searching on the web and making some tweaks but it didn't work.

Here is the contract PiggyBank.sol

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
 
contract PiggyBank{
 
    uint private pot;
    //uint public input;
 
    constructor (uint contribution){
       // input = contribution;
        pot = pot+contribution;
    }
 
    function peep () public view returns (uint) {
        return pot;
    }
 
    function addToPot(uint contribution) public {
       // input = contribution;
        pot = pot + contribution;
    }
 
}

Compile.js file given below

const path = require('path');
const fs = require('fs');
const solc = require('solc');
 
const piggyBank = path.resolve(__dirname,'Contracts','PiggyBank.sol');
const source = fs.readFileSync(piggyBank,'utf8');
 
//console.log(solc.compile(source,1));
 
var input = {
    language: 'Solidity',
    sources: {
        'PiggyBank.sol' : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}; 
console.log(JSON.parse(solc.compile(JSON.stringify(input))));
let output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output.contracts['PiggyBank.sol']['PiggyBank'].abi);
console.log(output.contracts['PiggyBank.sol']['PiggyBank'].evm.bytecode.object);
 
module.exports = solc.compile(JSON.stringify(input));

package.json given below

{
  "name": "piggybank",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^10.0.0",
    "solc": "^0.8.17",
    "web3": "^1.8.0"
  }
}

PiggyBank.test.js file is given below

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 accounts;
let piggyBank;
 
beforeEach(async function(){
 
    accounts = await web3.eth.getAccounts();
    
 
    piggyBank = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:bytecode,arguments:[20]}).
    send({from: accounts[1], gas:10000})
});
 
describe('PiggyBank',function(){
 
    it('is deployed',function(){
        console.log(piggyBank);
    });
});

Will deeply appreciate help on this issue. Thanks in advance.

Update

I updated the code to only export the interface and bytecode following code update in Compile.js

const interface = output.contracts['PiggyBank.sol']['PiggyBank'].abi;
const bytecode = output.contracts['PiggyBank.sol']['PiggyBank'].evm.bytecode.object;
console.log(JSON.stringify(input))
module.exports = {interface,bytecode};

But now I am getting following error

 1) "before each" hook for "is deployed"


  0 passing (44ms)
  1 failing

  1) "before each" hook for "is deployed":
     SyntaxError: Unexpected token o in JSON at position 1
      at JSON.parse (<anonymous>)
      at Context.<anonymous> (test\PiggyBank.test.js:15:22)
      at processImmediate (node:internal/timers:466:21)

It's having an issue parsing this part of code JSON.parse(interface)

piggyBank = await new web3.eth.Contract(JSON.parse(interface)).deploy({data:bytecode,arguments:[20]}).
    send({from: accounts[1], gas:10000})

Thanks in advance

Sid111Math
  • 167
  • 9
  • Seems like an error response from your node (some string response starting with "u" instead of a structured JSON) when you're deploying the contract... Based on the setup, I'm assuming you're using Ganache? Is there anything in its log? – Petr Hejda Oct 09 '22 at 09:34
  • I did some digging and found that `const {interface, bytecode} = require('../compile');` in `PiggyBank.Test.js` are coming in as `undefined`. – Sid111Math Oct 10 '22 at 04:37

0 Answers0