Ubuntu中的摩卡测试问题:ReferenceError:未定义ert

时间:2019-06-13 13:39:16

标签: node.js unit-testing npm mocha ubuntu-16.04

我正在使用mocha测试我的节点js代码。在Windows计算机上运行时,我能够成功测试我的应用程序。但是,当我尝试在Ubuntu中测试相同的应用程序项目时,却出现了ReferenceError:ert not defined。所以我又做了“ rm -rf node_modes”和“ npm i”。但是问题仍然存在。

终端错误

$ npm test

> myapplication@1.0.0 test /NodeProject/MyApplication
> mocha Test/* --require @babel/register

/NodeProject/MyApplication/node_modules/yargs/yargs.js:1163
  else throw err
       ^

ReferenceError: ert is not defined
at Object.<anonymous> (/NodeProject/MyApplication/Test/app.test.js:1:4)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Module._compile (/NodeProject/MyApplication/node_modules/pirates/lib/in                                                                             dex.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Object.newLoader [as .js] (/NodeProject/MyApplication/node_modules/pira                                                                             tes/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /NodeProject/MyApplication/node_modules/mocha/lib/mocha.js:330:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/NodeProject/MyApplication/node_modules/mocha/lib/moch                                                                             a.js:327:14)
at Mocha.run (/NodeProject/MyApplication/node_modules/mocha/lib/mocha.js:8                                                                             04:10)
at Object.exports.singleRun (/NodeProject/MyApplication/node_modules/mocha                                                                             /lib/cli/run-helpers.js:207:16)
at exports.runMocha (/NodeProject/MyApplication/node_modules/mocha/lib/cli                                                                             /run-helpers.js:300:13)
at Object.exports.handler.argv [as handler] (/NodeProject/MyApplication/no                                                                             de_modules/mocha/lib/cli/run.js:296:3)
at Object.runCommand (/NodeProject/MyApplication/node_modules/yargs/lib/co                                                                             mmand.js:242:26)
at Object.parseArgs [as _parseArgs] (/NodeProject/MyApplication/node_modul                                                                             es/yargs/yargs.js:1087:28)
at Object.parse (/NodeProject/MyApplication/node_modules/yargs/yargs.js:56                                                                             6:25)
at Object.exports.main (/NodeProject/MyApplication/node_modules/mocha/lib/                                                                             cli/cli.js:63:6)
at Object.<anonymous> (/NodeProject/MyApplication/node_modules/mocha/bin/_                                                                             mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
npm ERR! Test failed.  See above for more details.

这是我的app.test.js文件

var assert = require('assert')
, expect = require('expect.js'),
W3CWebSocket = require('websocket').w3cwebsocket;

describe('Test Suite', function() {
var client;
beforeEach(function(done) {
client = new W3CWebSocket('ws://localhost:8091/', 'echo-protocol');

client.onopen = function() {
  console.log('WebSocket Client Connected');
  done();
};

client.onclose = function() {
  console.log('echo-protocol Client Closed');
};


});
afterEach(function(done) {
if(client!=null){
  client.close();
}
done();
});


it('My App result', (done) => {
if (client.readyState === client.OPEN) {
  client.send(`"data":"dummy"}`);
}
client.onmessage = function(e) {
  let data = e.data;
  expect(data).to.not.equal(null);
  expect(data).to.not.equal(undefined);
  done();
};
});
});

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。这是因为package.json中的babel版本与.babelrc文件

中设置了特定设置的版本不同

尝试删除.babelrc文件,看看它是否有效。

该专家可能来自

  

... / node_modules / babel-core / lib / transformation / file / options / option-manager.js:128

log.error或log.ert

如果不在整个目录中搜索ert,那应该给您一些提示