Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Assertion.prototype properties configurable #14

Merged
merged 1 commit into from
Feb 12, 2012

Conversation

jfirebaugh
Copy link
Member

This allows plugins to install object-specific behavior for built-in
assertions.

I'm working on an assertion library for jQuery (inspired by https://github.com/velesin/jasmine-jquery).
The goal is to write assertions like:

$('#foo').should.exist;
$('#foo').should.contain('#bar');

These would be implemented by overriding the existing properties, e.g.:

chai.use(function (chai) {
  var chaiExist = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, 'exist');
  Object.defineProperty(chai.Assertion.prototype, 'exist', {
    get : function () {
      if (this.obj instanceof jQuery) {
        this.assert(
            this.obj.length > 0
          , 'expected ' + inspect(this.obj.selector) + ' to exist'
          , 'expected ' + inspect(this.obj.selector) + ' not to exist');
        return this;
      } else {
        return chaiExist.get.call(this);
      }
    }
  });
});

But in order for this to work, the original Object.defineProperty needs to have
been passed configurable: true.

I'm also amenable to other suggestions as to how to accomplish this.

This allows plugins to install object-specific behavior
for built-in assertions.
@logicalparadox
Copy link
Member

I am quite fine with this... and excited to see what you come up with!

logicalparadox added a commit that referenced this pull request Feb 12, 2012
Make Assertion.prototype properties configurable
@logicalparadox logicalparadox merged commit 5a18f7b into chaijs:master Feb 12, 2012
@logicalparadox
Copy link
Member

In release 0.3.3.

Please keep me apprised of your progress :)

@jfirebaugh
Copy link
Member Author

Thanks!

Here's the library: https://github.com/jfirebaugh/chai-jquery

@logicalparadox
Copy link
Member

This is awesome!! Great stuff!!

I will set up a plugin section in the readme and docs today.

koddsson pushed a commit to koddsson/chai that referenced this pull request May 20, 2024
v1.1.0 assert style of test, rm jshint, add jscs eslint, better coverage, fixes chaijs#13
koddsson pushed a commit to koddsson/chai that referenced this pull request May 20, 2024
v1.1.0 assert style of test, rm jshint, add jscs eslint, better coverage, fixes chaijs#13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants