2

I have an issue with testing my simple function using chai assertion in my typescript code

I have:

    public async test1(){
     throw (new Error(COUCH_CONNECTION_ERROR.message));
    }

In which couch connection error is defined this way:

export const COUCH_CONNECTION_ERROR: IErrorModel = {
  code: "couch_connection_error",
  message: "Unable to connect to Couchdb.",
};

Now I wrote a test this way:

    it("test", ()=>{

    console.log(obj.test1());
    expect(obj.test1()).to.throw(Error, COUCH_CONNECTION_ERROR.message)
    console.log(`ccccccccccccccccc`);
})

So when when I run the test I get

AssertionError: expected {} to be a function

Can anyone help to understand what is wrong with my test?

AVANISH RAJBHAR
  • 527
  • 3
  • 9
Learner
  • 1,686
  • 4
  • 19
  • 38
  • Try via: `expect(() => obj.test1()).to.throw(Error, COUCH_CONNECTION_ERROR.message)` – k0pernikus Feb 21 '20 at 16:18
  • @k0pernikus now I get this: AssertionError: expected [Function] to throw Error – Learner Feb 21 '20 at 16:20
  • 1
    Since you are testing promises, you should use `chai-as-promised` over `chai` and use it via: `return promise.should.be.rejectedWith("Expected message")` – k0pernikus Feb 21 '20 at 16:20
  • See: https://www.npmjs.com/package/chai-as-promised – k0pernikus Feb 21 '20 at 16:21
  • @k0pernikus Thanks so I should not use expect here because I am using promise? – Learner Feb 21 '20 at 16:22
  • Related or possible duplicate: https://stackoverflow.com/q/26571328/457268 – k0pernikus Feb 21 '20 at 16:24
  • It's not about not using `expect`. There are ways to make it work via expect. See [this answer](https://stackoverflow.com/a/40842060/457268), esp. at the end. – k0pernikus Feb 21 '20 at 16:28
  • Pick a style of writing your assertions and run with it. One big downside of chai is that it allows a set of syntaxes. – k0pernikus Feb 21 '20 at 16:30
  • @k0pernikus better now I chnaged it to expect(Promise.reject(() => obj.test1())).to.be.rejectedWith(Error, COUCH_CONNECTION_ERROR.message) and it passed but it is wierd that even this will pass expect(Promise.reject(() => obj.test1())).to.be.rejectedWith(Error, "aaaa"). Do you have any idea why this is not failing? – Learner Feb 21 '20 at 16:49
  • @k0pernikus I also tried expect(Promise.reject(() => obj.test1())).to.be.rejectedWith(new Error("sssss")) and expect(Promise.reject(() => obj.test1())).to.be.rejectedWith(new Error(COUCH_CONNECTION_ERROR.message)) and weirdly both are passing – Learner Feb 21 '20 at 16:57

1 Answers1

6

Using mocha and chai async / await style:

import {expect} from "chai";

const test1 = async () => {
    throw new Error("I AM THE ERROR");
};

describe("My test case", async () => {
    it("should assert", async () => {

        try {
            await test1();
            expect(true, "promise should fail").eq(false)
        } catch (e) {
            expect(e.message).to.eq("I AM THE EXPECTED ERROR");
        }
    });
});

Using chai-as-promised:

import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";

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

const test1 = async () => {
    throw new Error("I AM THE ERROR");
};

describe("My test case", async () => {
    it("should assert", async () => {
        await expect(test1()).to.eventually.be.rejectedWith("I AM THE EXPECTED ERROR");
    });
});

Using chai-as-promised, you can also return the expect promise:

it("should assert", async () => {
    return expect(test1()).to.eventually.be.rejectedWith("I AM THE EXPECTED ERROR");
});

In every case you should get a test error stating:

  1) My test case
       should assert:

      AssertionError: expected promise to be rejected with an error including 'I AM THE EXPECTED ERROR' but got 'I AM THE ERROR'      
      actual expected

      I AM THE EXPECTED ERROR
k0pernikus
  • 60,309
  • 67
  • 216
  • 347