【问题标题】:Mocha not showing chai expect error messagesMocha 未显示 chai 错误消息
【发布时间】:2018-10-02 13:31:00
【问题描述】:

我正在使用 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"

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

【问题讨论】:

  • 我感受到了你的痛苦。你有没有解决这个问题??!??!?!?! xkcd.com/979

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


【解决方案1】:

我发现了这个问题。我有一个函数可以捕获 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);
});

那个东西正在杀死节点。需要检查您是否在测试中,如果您在测试中,请不要捕获错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多