NodeJS:在 Mocha 中使用 Describe 函数时出现意外的字符串错误

NodeJS : Unexpected String Error using the Describe function in Mocha

我目前正在通过 Udemy 课程学习 Solidity,目前我正在学习解释使用 Mocha 进行测试的部分。我正在使用具有示例中涵盖的语法的 describe 函数,并尝试使用 'npm run test'.

运行 测试

下面是 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');
      });
    });

  }
}

但是,当我尝试 运行 时,我收到以下错误,我无法弄清楚 post 在网上进行一些研究并查看其他 Whosebug 线程。在在线文档和示例中(甚至在 Mocha 主站点上),我看到第一个参数是一个字符串,但我收到此错误。

/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)

describe 不应该在你的车内 class。

就目前而言,nodejs 会期望一些 class 方法,即 describe(text, callback)。您提供的不是“文本”,而是字符串。

所以它应该是这样的:

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

  drive (){
    return 'vroom';
  }
}

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