Skip to content

Is this type of chain feasible? #155

thedeeno opened this Issue Apr 24, 2013 · 4 comments

3 participants


Thanks for this lib! Using it has been very fun so far.

Curious, is it possible to create a chain like this?


I have this assertion helper working:


but I'd LOVE to extend it with a more narrow assertion when I continue to chain .exaclty(count).times. Does someone more experienced know if this is possible? This additional chaining is a little different than the typical semantics. I'm not sure if I'm breaking convention.

domenic commented May 7, 2013

This is definitely possible. Consider:


The thing to realize is that each part of the chain is just Chai evaluating some code. Some pieces evaluate no real code, e.g. to, have, and. Some pieces set flags for later assertions to deal with, e.g. deep. And some pieces perform assertions, i.e. test something about the object and maybe throw an assertion error.

So in your case, I'd make render set a flag saying "when exactly is called, test the number of times rendered." And then exactly would check for that flag, and if it's set, do the number-of-times-rendered assertion. Then times could be a no-op.

Alternately render and exactly could both set flags that times uses to do the assertion.

thedeeno commented May 7, 2013

Nice. Thanks for the reassurance.

It looks like helpers are designed to be either 'asserters' or 'flaggers' and not both. Am I wrong?

In other words, it looks like

// I can't make this #render an assertion

// while also making this #render set a flag

I was hoping there was some chain context and delayed evaluation. Is there?

Now I'm using the following as a work around

// assert template was rendered at least once
// asserts template was rendered 3 times
render(my_template, { exactly: 3 }).times 

but the original design would be my ideal.

domenic commented May 7, 2013

They can definitely be both. Just set a flag, and do an assertion, inside the function.

Chai.js Assertion Library member

Hey @thedeeno, hopefully you're happy with the answers @domenic has given you. I'm going to close this issue now but if you have any other questions don't hesitate to open a new issue, or alternatively you can ask on our Google Group:!forum/chaijs

@keithamus keithamus closed this Oct 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.