I am trying to use Chai Promise test but it show an error I am using docker.
Here a simple function
let funcPromise = (n) => {
return new Promise((resolve, reject) =>{
if(n=="a") {
resolve("success");
}else {
reject("Fail")
}
})
}
simple test
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
})
Error on terminal
FileTest.spec.ts:43:25 - error TS2339: Property 'eventually' does not exist on type 'Assertion'.
storage/database/MongooseEngine.spec.ts:44:35 - error TS2339: Property 'rejected' does not exist on type 'Assertion'.
The error is TypeScript complaining that it doesn't know about the additions that chai-as-promised
makes to the chai
assertions.
Add the types for chai-as-promised
and that should take care of the TypeScript errors:
npm install --save-dev @types/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!
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With