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

Compatible with chai-jquery? #16

Closed
Turbo87 opened this issue Nov 23, 2016 · 5 comments
Closed

Compatible with chai-jquery? #16

Turbo87 opened this issue Nov 23, 2016 · 5 comments

Comments

@Turbo87
Copy link
Contributor

Turbo87 commented Nov 23, 2016

Is it possible to use chai-jquery and chai-dom at the same time or do they overwrite each other?

@nathanboktae
Copy link
Owner

I believe they overwrite each other because it gets nonsensical if text has multiple assertion paths. Well it would if chai assertions could be coupled to a type, but it doesn't and rightly so in a dynamic language.

You could fork one project and prefix it's assertions, though. A good regex search and replace or 2 could make that happen.

@Turbo87
Copy link
Contributor Author

Turbo87 commented Nov 24, 2016

@nathanboktae well, with a proper plugin design it would probably be possible to couple assertions to types, but that doesn't seem to be the case yet...

@keithamus can you tell us if there is anything in the pipeline that would let these two plugin live next to each other?

@keithamus
Copy link

keithamus commented Nov 24, 2016

This isn't the first time we've seen this issue come up. In fact, we have an issue discussing how plugins can interface with chai, and each other, in a safe way; chaijs/chai#585.

The issue is quite a read, but the TL;DR of it - for this scenario - is that assertions will be able to duck-type on the expected/actual values - and decide to pass on unknown values that don't pass the duck-typing. e.g.:

// chai-jquery's version of .text() would be:
export default {
  assertions: {
    'text': {
      'predicate': [ (actual) => actual instanceof $, (expected) => typeof expected === 'string' ],
      'assert': (actual, expected) => actual.text() === expected
    },
  }
}
// chai-dom's version of .text() would be:
export default {
  assertions: {
    'text': {
      'predicate': [ (actual) => actual instanceof HTMLElement, (expected) => typeof expected === 'string' ],
      'assert': (actual, expected) => actual.textContent === expected
    },
  }
}

Neither plugin knows anything about the other plugins in play (or about chai actually) - but by specifying predicates can "opt-out" of asserting on actual's they cannot reasonably assert on. Chai will then also have a fallback assertions of text which will automatically fail if all the other plugins pass on the actual or expected. Say, maybe something like:

Error: you wrote `expect({foo: 'bar'}).to.have.text('bar')` but no assertions can satisfy this.
The plugins: chai-dom and chai-jquery didn't understand expected value `{foo: 'bar'}`

@nathanboktae I'd love it if you could comment on this issue (chaijs/chai#585) - we're looking for feedback from plugin maintainers to see if what we're doing is right, and hoping to get an active discussion going - so your feedback would be hugely useful! Constructive criticism fully welcome!

@keithamus
Copy link

BTW forgot to mention, thanks @Turbo87 for pinging me on this one! Always good to be bought in on issues like this so we in the chai team can see how the community is working. Speaking of which - I'm going to cc @lucasfcosta and @meeber; you don't need to comment here - but it's something to think on 😄

@Turbo87
Copy link
Contributor Author

Turbo87 commented Dec 2, 2016

I'll close this issue as my question was answered. Thanks @nathanboktae and also thanks @keithamus for looking into this and taking care of Chai!

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

No branches or pull requests

3 participants