question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make assertions (such as .exist) callable

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:5
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
mgolcommented, Mar 19, 2015

One problem with this approach is that many editors/linters warns against using expressions as statements. It’s quite useful to not have to disable such settings but sometimes they can only be set per project so one can’t just disable them for tests.

0reactions
mgolcommented, Mar 23, 2015

ES6 Proxies are a suitable idea. But we should discuss this in a new issue, rather than continuing discussion here.

Sure. I’ve just created #407 for that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make assertions (such as .exist) callable · Issue #56 · chaijs/chai
1. An assertion will be a method when parameter(s) are needed, a property when a paramater(s) are not needed. · 2. All assertions...
Read more >
callable statement problem - help — oracle-tech
hi i'm using odbc with ms sql server... i get this error when trying to do callablestatement: java.sql.SQLException:
Read more >
Package pijamas version ~develop - DUB - The D package registry
Asserts whether a callable object wrapped around the assertion throws an exception of type T. ... void throwing() { throw new Exception("I throw...
Read more >
java - Where in the Callable Statement stays ... - Stack Overflow
I'm trying to make a integration test for a method that a call a procedure, the values are coming, but I just want...
Read more >
Diff - 66c91e5046..ad5c9b9b07 - chromium/src - Git at Google
+ """Checks to make sure DCHECK_IS_ON() does not skip the parentheses. ... if no browser window exists then create one with no tabs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found