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 inbox;
beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
// Use one of those accounts to deploy
// the contract
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({
data: bytecode,
arguments: ['Hi there!']
})
.send({ from: accounts[0], gas: '1000000' });
});
describe('Inbox', () => {
it('deploys a contract', () => {
assert.ok(inbox.options.address);
});
it('has a default message', async () => {
const message = await inbox.methods.message().call();
assert.equal(message, 'Hi there!');
});
it('can change the message', async () => {
await inbox.methods.setMessage('bye').send({ from: accounts[0] });
const message = await inbox.methods.message().call();
assert.equal(message, 'bye');
});
});
after running the above code i keep getting the following error
[email protected] test C:\Users\user\Documents\inbox
mocha
Inbox 1) "before each" hook for "deploys a contract"
0 passing (98ms) 1 failing
1) "before each" hook for "deploys a contract": SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at Context.beforeEach (test\inbox.test.js:16:44) at
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: mocha
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\user\AppData\Roaming\npm-cache_logs\2018-07-03T13_17_54_895Z-debug.log
C:\Users\user\Documents\inbox\test>
When I changed my encoding from 'UTF-8' to 'utf8' in my compile.js file it worked.
My compile.js file looks like this
const path = require('path'); //for crossplatform
const fs = require('fs'); //file system
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8'); //change to utf8 to UTF-8
module.exports = solc.compile(source, 1).contracts[':Inbox'];