【问题标题】:Invalid or unexpected token "��" when trying to test with mocha尝试使用 mocha 进行测试时出现无效或意外的令牌“��”
【发布时间】:2021-01-26 21:19:11
【问题描述】:

我正在尝试进行单元测试,但是当我尝试测试我的代码时,我在终端中得到了这个:

 D:\VS Code\VS Code\tests\test\test.js:1
(function (exports, require, module, __filename, __dirname) { ��'
                                                              ^
SyntaxError: Invalid or unexpected token
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:230:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:227:14)
    at Mocha.run (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:495:10)
    at Object.<anonymous> (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:469:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

我按照 mocha 文档中给出的步骤操作: https://mochajs.org/#installation

  1. npm install mocha
  2. mkdir 测试
  3. $null > test/test.js

然后我复制并粘贴了示例

    var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});

在 package.json 中设置测试:

"scripts": {
"test": "mocha"

}

然后运行命令npm test 无论我采用什么测试示例,错误都是一样的。我也尝试过卸载并再次安装 mocha,我也尝试过 chai。同样的错误。

【问题讨论】:

  • VSCode 也有这个问题。我认为文件编码以某种方式搞砸了。我最终将文本内容复制到一个新文件中,之后就很好了。

标签: javascript node.js unit-testing mocha.js


【解决方案1】:

我刚刚删除了de文件math.js,复制他的内容并创建新文件,如果名称相同并再次粘贴相同的程序,它就可以工作了。

示例:

const assert = require('assert');
const Math = require('../scr/math.js');


describe('Math class', function() {
    it('Sum two numbers', function() {
        const math = new Math();
        assert.equal(math.sum(5, 5), 10);
    });
});

file math.js: 

class Math {
    sum = function sum(a,b) { };
}

module.exports = Math;

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。 实际上,我尝试使用 windows cmd 而不是 git bash 中的 touch cmd 创建测试 js 文件,然后我粘贴了本教程中提到的代码https://buddy.works/guides/how-automate-nodejs-unit-tests-with-mocha-chai。

    只需删除 test.js 文件并使用 touch cmd 创建即可。之后它应该可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多