4

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';
SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • 1
    Did you set up chai to use the plugin? `chai.use(require('chai-as-promised'));` –  Nov 26 '19 at 15:11
  • can you edit your question with the code of `GetSecret` function? – Freez Nov 26 '19 at 15:26
  • @ChrisG I have indeed not imported that, idk how I glanced over that bit of documentation, if you post it as an answer I'll mark it as the correct. – SebastianG Nov 26 '19 at 15:39

1 Answers1

1

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);
SebastianG
  • 8,563
  • 8
  • 47
  • 111