Chai-As-Promised即使错了也会通过

lai*_*onh 4 javascript mocha.js chai chai-as-promised

const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const expect = chai.expect;
chai.use(chaiAsPromised);

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});
Run Code Online (Sandbox Code Playgroud)

这个测试通过??? 我正在使用"chai":"3.5.0","chai-as-promised":"5.2.0",

rob*_*lep 6

expect(...) 返回一个promise本身,它将被解析或拒绝,具体取决于测试.

为了让Mocha测试该promise的结果,你需要从测试用例中明确地返回它(这是因为Mocha有内置的promise支持):

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});
Run Code Online (Sandbox Code Playgroud)

或者,你可以使用Mocha的"常规"回调式异步设置和chai-as-promised .notify():

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function(done) {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh").notify(done);
  });
});
Run Code Online (Sandbox Code Playgroud)