Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chai Mocha test Promise Property 'eventually' does not exist

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'.

like image 648
Rolly Avatar asked Apr 25 '19 16:04

Rolly


1 Answers

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!
});
like image 178
Brian Adams Avatar answered Nov 09 '22 07:11

Brian Adams