与Mocha + Chai一起测试是否兑现了承诺,似乎没有用

时间:2019-11-26 14:49:18

标签: javascript typescript mocha chai chai-as-promised

我有一个异步函数,该函数依赖于另一个异步函数,并且我正在测试是否在url错误时会引发错误,它确实会引发错误并且在控制台上。

但是测试一直失败,但是我编写了语法。我完全不知道该怎么写,我在这种胡扯上浪费了太多时间...

expect(GetSecret('', 'test-secret')).to.eventually.throw(
      'TypeError: Only absolute URLs are supported',
    ); 

expect(GetSecret('', 'test-secret')).to.eventually.throw;
expect(GetSecret('', 'test-secret')).to.eventually.be.rejected;

这些是我尝试过的几种方法,但每次都得到相同的结果。 我还尝试过await GetSecret('' ...与它们的所有可能组合,但结果相同。

我的版本:

    "@types/mocha": "^5.2.7",
    "@types/chai-as-promised": "^7.1.2",
    "chai": "2.1.2",
    "chai-as-promised": "^7.1.1", 

然后导入:

import { expect } from 'chai';

1 个答案:

答案 0 :(得分:1)

使用插件需要您告诉chai use(),您可以这样操作:

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

现在您可以使用其他功能,在这种情况下为eventually

const expect = chai.expect;

return expect(returnsAPromise()).to.eventually.equal(someValue);