Skip to content

Make assertions (such as .exist) callable #56

Closed
@fizker

Description

@fizker

I am currently writing some tests using the expect-version of chai. And it is going fine, except there is a readability issue.

Take the following code:

expect(someVariable).to.exist;

This is perfectly valid, and will test as expected, since the assertion is done inside the getter .exist.

But to my eye, there is no action taken here. I would much prefer the line to be:

expect(someVariable).to.exist();

This can be achieved by simply changing the .exist definition from:

Object.defineProperty(Assertion.prototype, 'exist',
  { get: function () {
      this.assert(
          null != flag(this, 'object')
        , 'expected #{this} to exist'
        , 'expected #{this} to not exist'
      );

      return this;
    }
  , configurable: true
});

to:

Object.defineProperty(Assertion.prototype, 'exist',
  { get: function () {
      this.assert(
          null != flag(this, 'object')
        , 'expected #{this} to exist'
        , 'expected #{this} to not exist'
      );

      var self = this;
      return function() { return self; };
    }
  , configurable: true
});

The only drawback is that you cannot chain directly on .exist (like expect(a).to.exist.ok), but since there is no .and and the tests does not break after doing the change above, I don't see that as an issue.

So the question is, is this only an issue in my mind? And should I spend time on making a proper pull request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions