Testing if a promise throws/rejected with Mocha + Chai as promised doesn't seem to be working

834 views Asked by At

I have an async function that depends on another async function and i'm testing if it'll throw an error when the url is wrong, it does throw the error and it is on the console.

But the test keeps failing, however I write the syntax. I'm completely lost at how I'm supposed to write this and I've wasted too many hours on this crap...

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;

These are a few ways I've tried them out but the same result every single time. I've also tried to await GetSecret(''... in every possible combination with these, but nonetheless, same result.

My versions:

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

Then importing:

import { expect } from 'chai';
1

There are 1 answers

2
AudioBubble On BEST ANSWER

Using a plugin requires you to tell chai to use() it, which you can do like this:

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

Now you can use the additional functionality, in this case eventually:

const expect = chai.expect;

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