node.js – AssertionError崩溃了Mocha

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

我在崩溃时遇到的错误是这样的:

/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还是内置断言库,它都是一样的.我用这个命令运行Mocha(我用npm测试运行它):

mocha --reporter 'spec' --recursive

我正在使用的库版本是:

>节点:0.10.18
>摩卡:1.12.0
>柴: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();
          });
        });
      });
    });

解决方法

这是因为它是摩卡的工作方式.需要捕获异步调用中的异常并将其传递给done回调,这甚至包括AssertionErrors. Mocha文档中有一个错误,我打开了一个GitHub问题来解决这个问题( https://github.com/visionmedia/mocha/issues/982).

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...