摩卡不显示chai期望错误消息

时间:2018-04-22 11:06:32

标签: node.js testing mocha chai ecmascript-2017

我正在使用Mocha和Chai + ChaiHttp来测试我的API。我遇到的问题是,当expect语句失败时,我没有看到任何错误消息。

这是我的代码:

it('POST /signup : should create a new user', async () => {
  const user = {
    name: 'Felix',
    email: 'felix@gmail.com',
    password: 'test',
  };

  const res = await chai.request(app).post('/signup').send(user);

  expect(res).to.be.json;
  expect(res).to.have.status(200);
  expect(res.body).to.be.an('object');
  expect(res.body.success).to.equal(true);
  expect(res.body.user).to.have.property('falseAttribute'); // <- test passes with "name"
  expect(res.body.user).to.have.property('token');
});

当它失败因为返回的用法预计会有falseAttribute时,它只会显示红色突出显示的'it'文字:

  Authentication tests
    1) POST /signup : should create a new user`)

在我的package.json脚本中,我放了"test": "NODE_ENV=test ./node_modules/.bin/mocha test"

我已经在互联网上搜索过类似的问题,但我还没有找到任何东西。谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我发现了问题。我有一个函数可以捕获uncaughtExceptions

process.on('uncaughtException', function (err) {
  logger.fatal(`${(new Date()).toUTCString()}: uncaughtException: '${err.message}'. Stack Trace To Follow.`);
  logger.fatal(err.stack);
  process.exit(1);
});

那东西正在杀死节点。需要检查您是否在测试中,以及是否在测试中,请不要误解。