断言错误导致Mocha崩溃

6

我正在使用Mocha运行单元测试,但是在报告中不显示所有抛出的AssertionErrors,而是在第一个错误处崩溃。有什么建议吗?

崩溃时显示的错误如下:

/Users/Robert/Code/JRJ/Server/node_modules/chai/lib/chai/assertion.js:106
      throw new AssertionError(msg, {
            ^
AssertionError: expected 200 to equal 202
npm ERR! weird error 8
npm ERR! not ok code 0

无论我使用 Chai 还是内置的 assert 库,结果都是相同的。我使用以下命令运行 Mocha(使用 npm test 命令):
mocha --reporter 'spec' --recursive

以下是我使用的库版本:

  • node: 0.10.18
  • mocha: 1.12.0
  • chai: 1.8.0
  • hapi: 1.10.0

测试代码如下:

    var hapi = require('hapi'),
        expect = require('chai').expect,
        assert = require('assert');

    describe("Customer API", function(){
      var server = require('../../../../src/apis/customer');

      //works as expected 
      describe('simpleExample', function(){
        it("should cause a test failure", function(done){
            expect(200).to.equal(202);
            done();
        });
      });

      //crashes Mocha
      describe('Authentication', function(){
        it('Should get user token', function(done){
          server.inject("/auth?username=test@test.com&password=testa", function(res){
            expect(res.statusCode).to.equal(202); //returns 200, crashes Mocha (the expected 202 is intentional to cause an assertion error)
            //assert.ok(res.statusCode === 202);
            expect(res.payload).to.be.a('string');
            expect(res.payload).to.have.length(16);
            done();
          });
        });
      });
    });
1个回答

3

这是由于 Mocha 的工作方式决定的。在异步调用中,异常需要被捕获并传递到 done 回调函数中,这甚至包括 AssertionErrors。Mocha 文档中存在错误,我已经打开了一个 GitHub 问题来解决这个问题(https://github.com/visionmedia/mocha/issues/982)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接