chai-as-promiseised.eventually.equal not not passing

时间:2018-03-18 10:32:17

标签: mocha chai chai-as-promised

我正在尝试编写chai-as-promised的最小工作示例,以便在测试返回promise的函数时理解它是如何工作的。

我有以下功能:

simple.test = async (input) => {
    return input;
};

以及以下测试功能:

chai.use(sinonChai);
chai.use(chaiAsPromised);
const { expect } = chai;
const should = chai.should();

describe('#Test', () => {
    it('test', () => {
        expect(simple.test(1)).should.eventually.equal(1);
    });
});

然而,测试这会导致测试没有通过,但是在很长的错误中,粘贴在这里:https://pastebin.com/fppecStx

问题:代码有什么问题,或者这里似乎有问题吗?

1 个答案:

答案 0 :(得分:2)

首先:你的混合expectshould。如果你想使用should for assertion,你不需要期望。

第二:要告诉mocha测试是异步的,您必须致电donereturn承诺或使用async/await

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const sinonChai = require('sinon-chai');

const should = chai.should();
chai.use(sinonChai);
chai.use(chaiAsPromised);

// Function to test
const simple = {
  test: async (input) => {
    return input;
  }
}

// Test
describe('#Test', () => {
  it('test', () => {
    return simple.test(1).should.eventually.equal(1);
  });
});