Chai Mocha 测试 Promise 属性“最终"不存在 [英] Chai Mocha test Promise Property 'eventually' does not exist
问题描述
我正在尝试使用 Chai Promise 测试,但它显示错误我正在使用 docker.
I am trying to use Chai Promise test but it show an error I am using docker.
这里是一个简单的函数
let funcPromise = (n) => {
return new Promise((resolve, reject) =>{
if(n=="a") {
resolve("success");
}else {
reject("Fail")
}
})
}
简单测试
import chai from 'chai';
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
let expect = chai.expect;
let assert = chai.assert;
it('connect: test promise', (done) => {
let func = funcPromise("a");
expect(func).to.eventually.equal("success"); // dont work
expect(func).to.be.rejected; // dont work
})
终端错误FileTest.spec.ts:43:25 - 错误 TS2339:类型断言"上不存在最终"属性.
storage/database/MongooseEngine.spec.ts:44:35 - 错误 TS2339:Assertion"类型上不存在rejected"属性.
推荐答案
错误是 TypeScript 抱怨它不知道 chai-as-promised
对 所做的添加chai
断言.
The error is TypeScript complaining that it doesn't know about the additions that chai-as-promised
makes to the chai
assertions.
为chai-as-promised
类型> 并且应该处理 TypeScript 错误:
Add the types for chai-as-promised
and that should take care of the TypeScript errors:
npm install --save-dev @types/chai-as-promised
您还需要 await
使用 chai-as-promised
做出的任何断言:
You will also want to await
any assertions made with chai-as-promised
:
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);
const expect = chai.expect;
const funcPromise = (n) => new Promise((resolve, reject) => {
if (n === 'a') { resolve('success'); }
else { reject('fail'); }
});
it('connect: test promise', async () => {
await expect(funcPromise('a')).to.eventually.equal('success'); // Success!
await expect(funcPromise('b')).to.be.rejected; // Success!
});
这篇关于Chai Mocha 测试 Promise 属性“最终"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!