简体   繁体   English

当Mocha Test失败时,它将对象记录到控制台

[英]When Mocha Test fails it logs an object to console

When all my tests pass, everything works as expected. 当我所有的测试通过时,一切都会按预期进行。 However, when a test fails with any reporter active, the runner prints an object representing the strack trace rather than the pretty formatted error messages I see in screen shots 但是,如果在任何报告程序都处于活动状态的情况下测试失败,则运行程序将打印一个表示strack跟踪的对象,而不是我在屏幕快照中看到的格式错误的错误消息

> mocha --reporter spec --ui bdd --ignore-leaks
Site Routes

GET /sites
  ✓ should return a 200 OK 
  ✓ should be a JSON response 
  ◦ should be an array: {
"date": "Fri Apr 19 2013 16:09:20 GMT-0400 (EDT)",
"process": {
  "pid": 34270,
  "uid": 1032026306,
  "gid": 116266774,
  "cwd": "/Users/squinn/Projects/armada",
  "execPath": "/usr/local/Cellar/node/0.10.4/bin/node",
  "version": "v0.10.4",
  "argv": [
    "node",
    "/Users/squinn/Projects/armada/node_modules/mocha/bin/_mocha",
    "--reporter",
    "spec",
    "--ui",
    "bdd",
    "--ignore-leaks"
  ],
  "memoryUsage": {
    "rss": 55717888,
    "heapTotal": 51000320,
    "heapUsed": 22281672
  }
},
"os": {
  "loadavg": [
    1.7001953125,
    1.66162109375,
    1.58984375
  ],
  "uptime": 374244
},
"trace": [
  {
    "column": 10,
    "file": "/Users/squinn/Projects/armada/node_modules/should/lib/should.js",
    "function": "Object.Assertion",
    "line": 368,
    "method": "Assertion",
    "native": false
  },
  {
    "column": 43,
    "file": "/Users/squinn/Projects/armada/test/route.sites.js",
    "function": "null.callback",
    "line": 47,
    "method": "callback",
    "native": false
  },
  {
    "column": 21,
    "file": "/Users/squinn/Projects/armada/node_modules/request/main.js",
    "function": "",
    "line": 290,
    "method": null,
    "native": false
  },
  {
    "column": 17,
    "file": "events.js",
    "function": "EventEmitter.emit",
    "line": 95,
    "method": "emit",
    "native": false
  },
  {
    "column": 54,
    "file": "/Users/squinn/Projects/armada/node_modules/request/main.js",
    "function": "",
    "line": 278,
    "method": null,
    "native": false
  },
  {
    "column": 20,
    "file": "events.js",
    "function": "IncomingMessage.EventEmitter.emit",
    "line": 117,
    "method": "EventEmitter.emit",
    "native": false
  },
  {
    "column": 14,
    "file": "_stream_readable.js",
    "function": null,
    "line": 883,
    "method": null,
    "native": false
  },
  {
    "column": 13,
    "file": "node.js",
    "function": "process._tickCallback",
    "line": 415,
    "method": "_tickCallback",
    "native": false
  }
],
"stack": [
  "AssertionError: expected [ { user: 'test',",
  "    group: 'group',",
  "    name: 'post-test',",
  "    domain: 'localhost:9000',",
  "    _id: '51718b214805d8fe83000003',",
  "    __v: 0,",
  "    modules: [],",
  "    labels: [ 'Test' ] } ] not to be an instance of Array: expected [ { user: 'test',",
  "    group: 'group',",
  "    name: 'post-test',",
  "    domain: 'localhost:9000',",
  "    _id: '51718b214805d8fe83000003',",
  "    __v: 0,",
  "    modules: [],",
  "    labels: [ 'Test' ] } ] not to be an instance of Array",
  "    at Object.Assertion (/Users/squinn/Projects/armada/node_modules/should/lib/should.js:368:10)",
  "    at null.callback (/Users/squinn/Projects/armada/test/route.sites.js:47:43)",
  "    at null.<anonymous> (/Users/squinn/Projects/armada/node_modules/request/main.js:290:21)",
  "    at EventEmitter.emit (events.js:95:17)",
  "    at IncomingMessage.<anonymous> (/Users/squinn/Projects/armada/node_modules/request/main.js:278:54)",
  "    at IncomingMessage.EventEmitter.emit (events.js:117:20)",
  "    at _stream_readable.js:883:14",
  "    at process._tickCallback (node.js:415:13)"
],
"level": "error",
"message": "uncaughtException"
}
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

And here is the test 这是测试

describe('GET /sites', function (){

  var url = 'http://localhost:' + app.get('api').port + '/sites';

  it('should return a 200 OK', function (done){
    request(url, function (err, res){
      res.should.have.status(200);
      done();
    });
  });

  it('should be a JSON response', function (done){
    request(url, function (err, res, body){
      res.should.be.json;
      done();
    })
  });

  it('should be an array', function (done){
    request(url, function (err, res, body){
      JSON.parse(body).should.not.be.an.instanceOf(Array);
      done();
    });
  });

});

Any clues as to why I'm not getting the pretty view? 关于为什么我看不到漂亮景色的任何线索?

The object is logged to the console because of the async behavior. 由于异步行为,该对象已记录到控制台。 should.js throws errors correctly but not as a parameter of the done() callback. should.js正确抛出错误,但不能作为done()回调的参数。 I fixed by wrapping inside a try/catch statement so I could pass the error to done() . 我通过在try / catch语句中包装来解决问题,因此可以将错误传递给done()

it('should return a 200 OK', function (done){
  request(url, function (err, res){
    try {
      res.should.have.status(200);
    }
    catch (e){
      done(e);
    }
  });
});

Your use of --ignore-leaks shows that you're using an outdated version of mocha. 您对--ignore-leaks表明您使用的是摩卡咖啡的过时版本。 (It's since changed to --check-leaks .) (此后更改为--check-leaks 。)

Install the latest version with npm install -g mocha . 使用npm install -g mocha安装最新版本。

Also, you seem to be getting the JSON reporter. 另外,您似乎正在获取JSON报告程序。 Check if you have a test/mocha.opts file, though I can't see why it wouldn't be overridden by your command line argument. 检查您是否有一个test/mocha.opts文件,尽管我看不到为什么您的命令行参数不会覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM