Rohan
Rohan

Reputation: 593

NodeJS : Unexpected String Error using the Describe function in Mocha

I am currently learning Solidity through a Udemy course and I am currently covering a section that explains Testing with Mocha. I am using the describe function with syntax that is covered in the example and trying to run the test using 'npm run test'.

Below is the code for Inbox.test.js

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

//Provider is a communication layer between web3 lib and some ETH network

const web3 = new Web3(ganache.provider());


class Car{
  park (){
    return 'stopped';
  }

  drive (){
    return 'vroom';
  }

  describe('Car', () => {
      it('park function', () => {
        const car = new Car();
        assert.equal(car.park(),'stopped');
      });
    });

  }
}

However, when I try to run this, I get the following error that I am unable to figure out post doing some research online and looking at other stackoverflow threads. In the documentation and examples online(even on the Mocha main site), I see the first argument is a string, yet I get this error.

/Users/rohanmahale/Documents/Solidity/inbox/test/Inbox.test.js:25
  describe('Car', () => {
           ^^

SyntaxError: Unexpected string
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/Users/rohanmahale/Documents/Solidity/inbox/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/rohanmahale/Documents/Solidity/inbox/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (/Users/rohanmahale/Documents/Solidity/inbox/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at async singleRun (/Users/rohanmahale/Documents/Solidity/inbox/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/rohanmahale/Documents/Solidity/inbox/node_modules/mocha/lib/cli/run.js:374:5)

Upvotes: 1

Views: 324

Answers (1)

JeffRSon
JeffRSon

Reputation: 11166

describe shouldn't be inside your Car class.

As it stands, nodejs would expect some class method which would be describe(text, callback). Instead of "text" you're supplying a string.

So this is how it should look like:

class Car{
  park (){
    return 'stopped';
  }

  drive (){
    return 'vroom';
  }
}

describe('Car', () => {
  it('park function', () => {
    const car = new Car();
    assert.equal(car.park(),'stopped');
  });
});

Upvotes: 2

Related Questions