mardi 22 décembre 2020

Using a custom chai method assertion with chai-as-promised

I want to add a custom assertion/method like this:

chai.use(function (chai, utils) {
  var Assertion = chai.Assertion;
  Assertion.addMethod("convertToStringEqual", function (input) {
    new Assertion(this._obj.toString()).to.equal(input.toString());
  });
});

However I want to be able to use it with chai-as-promised like so:

Promise.resolve(2 + 2).should.eventually.convertToStringEqual(4);

But when I run this example, I see this error:

AssertionError: expected '[object Promise]' to equal '4'

This is because chai-as-promised is not resolving that promise with eventually before passing it to convertToStringEqual.

How can I get chai-as-promised await that promise before passing it to my custom assertion method?

Aucun commentaire:

Enregistrer un commentaire